Crazy God said java study notes --- operator

JAVA basic grammar

  1. Operator
  2. Package mechanism, JavaDoc

Operator

  • The Java language supports the following operators:Priority: best use ()

    • Arithmetic operators: +, -, *, /,% (remainder), ++ (increment), – (decrement);

    • Assignment operator: =;

    • Relational operators:>, <, >=, <=, ==, !=, instanceof;

      The calculation result must be true or false; it will be used with if in the future

    • Logical operators: && (and), || (or),! (non);

      The calculation result must be true or false

    • Bit operators: &, |, ^, ~, >>, <<, >>>;

    • Conditional operator:? ,:;

    • Extended assignment operators: +=, -=, *=, /=;

    Arithmetic Operator

    ++ (increment)

    a++: When ++ is behind, assign value first, then increment;

    ++a: ++ is first incremented and then assigned when it comes before;

    int a = 5;
    int b = a++;//a++加在后,先给b赋值,再自增;
    System.out.println(a);//a=6
    System.out.println(b);//b=5
    ================================================
    int c = ++a;//++a加在前,先自增,再赋值给c;
    System.out.println(a);//a=6;
    System.out.println(c);//c=6;
    

    – (Decrement) the same

  • Precautions:

    • One of the arithmetic operators is of type long or double, and the result is of type long or double, otherwise it is of type int.
    • Many operations in Java will use many tools to operate
    Logical Operators
    • && (and): logical AND operation, both variables are true, the result is true
    • || (or): logical OR operation, one of the two variables is true, the result is true
    • ! (Not): If it is true, it becomes false, if it is false, it becomes true
    int q = 10;
    int  w = 20;
    System.out.println(q<w && w>30);//(false),q<w正确,w>30错误,
    // 可解释为:q<w与w>30同时两条成立,结果为true,所以最后结果为"false"
    System.out.println(q<w || w>30);//(true),q<w正确,w>30错误,
    // 可以解释为:q<w或w>30其中一条成立,结果就为true,所以最后结果为"true"
    System.out.println(!(q<w));//(false),q<w正确。
    // 可以解释为:q应该不(非)小于w,但是实际上q<w,所以为”false"
    
    Short-circuit calculation

    When the former is "false", the latter will not be executed, and only "true" will run. This is called short-circuit operation.

    int z = 5;
    boolean x = (z<4)&&(z++<4);//由于(z<4)不成立为false,(z++<4)就不运行
    System.out.println(x);//(false)
    System.out.println(z);//(5)由于(z++<4)没有运算,所以z没有自增
    
    Bitwise operator

    & (1): All 1s are 1, otherwise 0

    | (0): All 0 is 0, otherwise it is 1

    ^ (Same as 0): the same is 0, otherwise it is 1

    ~ (Inverted): take the opposite number in binary

     A = 1100 0011
     B = 1001 0110
    ---------------
    A&B= 1000 0010
    A|B= 1101 0111
    A^B= 0101 0101
    ~B = 0110 1001
    

    "<<"Binary left shift=represent *2

    ">>" Binary Right Shift = Representation/2

    Spread operator

    +=,-=,*=,/=;()

    int a = 5, b = 10;
    a+=b;//a=a+b(15)
    a-=b;//a=a-b(-5)
    a*=b;//a=a*b(50)
    a/=b;//a=a/b(0.5)
    
    String concatenation

    When the string type appears before the "+" sign, he will convert an operand after the "+" into a steing type for splicing.

     int a = 5, b = 20;
    System.out.println("yes"+a+b);//(yes520)
    System.out.println(a+b+"yes");//(25yes)
    System.out.println("yes"+(a+b));//(yes25)
    System.out.println(""+a+b+"yes");//(520yes)
    System.out.println(""+a+b+(a+b)+"yes");//(52025yes)
    
    Conditional operator

    x? y: z //If the value of x is true = y, if it is false = z;

    int score = 60;
    int xm = 70;
    int xg = 49;
    int xh = 60;
    System.out.println("xm:"+(xm>=score ?"及格":"不及格"));//(xm:及格)
    System.out.println("xg:"+(xg>=score ?"及格":"不及格"));//(xg:及格)
    System.out.println("xh:"+(xh>=score ?"及格":"不及格"));//(xh:及格)
    

    You can use "if" to do it later

    Packet mechanism

"The essence of a package is a folder"

  • In order to organize classes better, Java provides a package mechanism for distinguishing the namespace of class names.

  • The syntax format of the package statement is:

    package pkg1[.pkg2[.pkg3...]]//定义包
    
  • Generally use the inversion of the company domain name as the package name;

  • In order to be able to use a member of a package, we need to explicitly import the package in the Java program. Use "import" to complete this function

import package1[.package2...].(classname|*);//导入包,*导入这个包下所以的类

Try not to repeat the package name

JavaDoc

  • The javadoc command is used to generate your own API documentation

  • Parameter information

    • @author author name
    • @version version number
    • @since indicates the jdk version that needs to be used earliest
    • @param parameter name
    • @return Return value situation
    • @throws exception throw situation
    /**
     * @author ssxxz
     * @version 1.0
     * @since 1.8
     */
    

    Production documents: javadic+parameters+java files

    javadoc -encoding UTF-8 -charset UTF-8 Doc.java

Guess you like

Origin blog.csdn.net/rzz65452064/article/details/107281013