java basic syntax

Comments, identifiers, keywords

notes

Comments are not executed. Comments are for programmers

Writing notes is a very good habit

Single-Line Comments

Only one line of text can be annotated

//Note content

multiline comment

You can annotate a paragraph of text

/*

Note Content

*/

JavaDoc

That is, document comments, which can be recognized.

/**

Note Content

*/

identifier

All components of Java need names. Class names, variable names, and method names are all called identifiers

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-qrnayd0a-1632381793769) (C: \ users \ 22698 \ appdata \ roaming \ typora \ typora user images \ image-20210919165013414. PNG)]

keyword

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-guf1vxm3-1632381793774) (C: \ users \ 22698 \ appdata \ roaming \ typora \ typora user images \ image-20210919164653574. PNG)]

data type

Strongly typed language

It is required that the use of variables should strictly comply with the regulations, and all variables must be defined before they can be used

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-jpxzq3zn-1632381793776) (C: \ users \ 22698 \ appdata \ roaming \ typora user images \ image-20210919170050879. PNG)]

variable

That is, the amount that can be changed

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-uud3ey6e-1632381793782) (C: \ users \ 22698 \ appdata \ roaming \ typora \ typora user images \ image-20210920162723851. PNG)]

Scope of variable

1. Class variable

2. Instance variables

3. Local variables

public class Demo3 {
    //Attribute: that is, method

    //Class variable plus static
    static int all=0;

    //Instance variable: subordinate to the object. If it is not initialized, it is the default value
    // (the Boolean value is false by default. Except for the basic type, the other default values are null)
    int age;
    String str="hello";

    public static void main(String[] args) {
        //Local variables: values must be declared and initialized
        int i=0;

        //Variable type variable name = new Demo3();
        Demo3 demo3=new Demo3();
        System.out.println(demo3.age);
    }
    
        //Other methods
        public void method(){

        }

}

Type conversion

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-oh8drnqo-1632381793787) (C: \ users \ 22698 \ appdata \ roaming \ typora \ typora user images \ image-20210920160221670. PNG)]

public class Demo2 {
    public static void main(String[] args) {
        int i=128;
        byte b=(byte)i;//The maximum value of byte is 127, i is 128, memory overflow, and an error occurred
        //Cast: high to low (type) variable names
        //Automatic conversion: low to high
        /*
        1.Boolean values cannot be converted
        2.Cannot convert an object type to an unrelated type
        3.Forced conversion is required when converting high capacity to low capacity
        4.There may be memory overflow or accuracy problems during conversion
         */
        System.out.println(i);
        System.out.println(b);
        System.out.println("================================");
        char c='a';
        int d=c+1;
        System.out.println(d);
        System.out.println((char)d);


        //When the operands are large, pay attention to the overflow problem. The numbers can be separated by underscores
        int m=10_0000_0000;
        System.out.println(m);
        int year=20;
        int total=m*year;
        System.out.println(total);//-1474836480, overflow during calculation
        long t2=m*year;
        System.out.println(t2);//The default is int. there was a problem before the conversion
        long t3=m*((long)year);
        System.out.println(t3);

    }
}

constant

Keyword: final

Naming conventions for variables

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-be9wa4hy-1632381793792) (C: \ users \ 22698 \ appdata \ roaming \ typora \ typera user images \ image-20210921111339802. PNG)]

operator

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-0sy7vfbz-1632381793794) (C: \ users \ 22698 \ appdata \ roaming \ typora \ user images \ image-20210922133415603. PNG)]

package operator;

public class Demo2 {
    public static void main(String[] args) {
        long a=1212123332122222L;
        int b=125;
        short c=10;
        byte d =8;
        System.out.println(a+b+c+d);//Long type, long when there is a long
        System.out.println(b+c+d);//When there is no long, it is all int
        System.out.println(c+d);//When there is no long, it is all int
    }
}
package operator;

public class Demo3 {
    public static void main(String[] args) {
        //The result returned by the relational operator is: true or false (Boolean)
        int a=10;
        int b=20;
        int c=21;

        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
        System.out.println(c%a);//Remainder is also called modular operation

        //++Self increasing -- self decreasing (it doesn't need two numbers, so it's called unary operator)
        int d=6;

        int e=d++;//After executing this code, first assign a value to e, and then add it automatically
        // A + + is a=a+1

        // ++A is a=a+1
        int f=++d;//Before this code is executed, it is self incremented and then assigned to f
        System.out.println(d);
        System.out.println(e);
        System.out.println(f);


        //Power operation 2 ^ 3 many operations we will use some tool classes to implement
        double m=Math.pow(2,3);
        System.out.println(m);

    }
}
package operator;

public class Demo4 {
    public static void main(String[] args) {
        //Logical budget character
        //And (and) or (or) not (negative)
        boolean a=false;
        boolean b=true;
        System.out.println("a && b:"+(a && b));//Logic and operation: two truths are true
        System.out.println("a || b:"+(a || b));//Logical or operation: one true is true and all false is false
        System.out.println("!(a && b):"+!(a && b));//Logical non operation: true is false, false is true

        //Short circuit operation: when judging the authenticity of a & & b, if a is false, the code behind a will not be executed, that is, the authenticity of b will not be controlled
        int c=5;
        boolean d=(c<4)&&(c++<10);//If (c + + < 10) is executed, the value of C will increase to 6
        System.out.println(d);
        System.out.println(c);



        //Bit operation
        /*
        A = 0101 1101
        B = 0000 1001

        A&B 0000 1001(Similar to and operation (one for one)
        A|B 0101 1101(Similar to or operation (two zeros are zero and one is one)
        A^B 0101 0100(Or the two bits of the operation are compared correspondingly (the same is zero and the different is one)
        ~B  1111 0110((reverse)

        Interview questions:
        2*8 How to calculate fastest in a computer:
        Bit operation is a direct operation two-level system, so it is very efficient
        <<  It is equivalent to the current number * 2
        >>  Equivalent to the current number / 2

        The decimal number represented in the binary system
        0000 0000       0
        0000 0001       1
        0000 0010       2
        0000 0100       4
        0000 1000       8
        0001 0000       16
        << Move left is to move the 1 of the two-level system to the left by one position, and the corresponding decimal number is equivalent to * 2
        >>Similarly

         */

        System.out.println(2<<3);

        //Extended assignment operator
        int e=10;
        int f=20;
        e+=f;//e=e+f (* = / = same)
        e-=f;//e=e-f

        //String connector+
        System.out.println(e+f);
        System.out.println(""+e+f);
        //On both sides of the + operator, as long as the String type appears, it will convert other operands to String type and connect them

        System.out.println(e+f+"");//e+f is calculated first, so the result is 30


        //Conditional operator
        // x ? y : z
        //If x==true, the result is y; otherwise, the result is z

        int score =61;
        String type=score>60 ? "pass" : "fail,";
        System.out.println(type);







    }
}

Package mechanism

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-d7bbeyib-1632381793796) (C: \ users \ 22698 \ appdata \ roaming \ typora \ user images \ image-20210922160645629. PNG)]

JavaDoc

The javadoc command is used to generate its own API documents

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-agzydlt-1632381793798) (C: \ users \ 22698 \ appdata \ roaming \ typora \ user images \ image-202109231439386. PNG)]

package com.chen.operator;

/**
 * @author chen
 * @version 1.0
 * @since 1.8
 */

public class Doc {
    String name;

    /**
     * 
     * @param name
     * @return
     * @throws Exception
     */
    public String test(String name) throws Exception{
        return name;

    }
}

Generate documents from the command prompt:

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-j5zqtvhj-1632381793800) (C: \ users \ 22698 \ appdata \ roaming \ typora user images \ image-20210923151419629. PNG)]

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-cfvmdvmo-1632381793802) (C: \ users \ 22698 \ appdata \ roaming \ typora \ user images \ image-20210923151747645. PNG)]

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-jxy9wtki-1632381793804) (C: \ users \ 22698 \ appdata \ roaming \ typora user images \ image-20210923151902474. PNG)]

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-uvlize6t-1632381793805) (C: \ users \ 22698 \ appdata \ roaming \ typora \ user images \ image-20210923152000377. PNG)]

Generate JavaDoc documents using IDEA: https://zhinan.sogou.com/guide/detail/?id=316513277579

ring test(String name) throws Exception{
return name;

}

}

Generate documents from the command prompt:

[External chain picture transfer...(img-j5zQtVHj-1632381793800)]

[External chain picture transfer...(img-cFvMDVmo-1632381793802)]

[External chain picture transfer...(img-JXy9wtki-1632381793804)]

[External chain picture transfer...(img-uVlIze6t-1632381793805)]

use IDEA generate JavaDoc file: https://zhinan.sogou.com/guide/detail/?id=316513277579

Tags: Java R Language

Posted on Thu, 23 Sep 2021 02:37:26 -0400 by Spreegem