JAVA basic grammar review

1, Operator

Arithmetic operators: common in mathematics +,, -, *, /,% and special + +, -. Different types of operations will take the maximum type as the output type-- It is to + 1 and - 1 by itself. Note that i + + is to + 1 after the end of operation i, and + + i is to + 1 i before operating i.

public static void main(String[] args) {
        long a = 121212121212L;
        int b = 123;
        short c = 10;
        byte d = 8;
        int i = 0;
        int j = 0;

        System.out.println(a+b+c+d);//long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int
        System.out.println(i++);//0
        System.out.println(++j);//1
    }

Assignment operator: =. Note that x=1 assigns 1 to x, x==1 equals x and compares 1 with X.

int a=b=c=100;//continuous assignment 

Relational operators: >, <, > =, < =, = == These common mathematical operators and the special instanceof are used to compare whether there is a parent-child relationship. The result returned by the relational operator is a boolean value. The premise of the code example is that I first define the parent class Person and the child classes Student and Teacher. Object is a large class defined by Java.

 //   Object > Person > Student
//    Object > Person > Teacher
//    Object > String
        Object object = new Student();
        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Person);//true
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Teacher);//false the other line has nothing to do with students
        System.out.println(object instanceof String);//false

Logical operators: & &, |, |,! And or not&& True if true, false if false|| If one is true, then true;! The operation is to convert the output Boolean value, not & & and |. The short-circuit operation will not go back when the previous result can be obtained. For example, the output of c in the example is 5 instead of 6

public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
    
        System.out.println("a && b:"+(a&&b));
        System.out.println("a || b:"+(a||b));
        System.out.println("! (a && b):"+!(a&&b));
//Short circuit operation
        int c = 5;
        boolean d = (c<4)&&(++c<4);

        System.out.println(d);//false
        System.out.println(c);//5
        

    }

Bitwise operators: &, |, ^, ~, > >, < <. And, or, not,, negate, shift right, shift left

/*
     * A = 0011 1100
     * B = 0000 1101
     * A&B = 0000 1100 All 1 is 1, and all others are 0
     * A|B = 0011 1101 All 0 is 0, and all others are 1
     * A^B = 0011 0001 Same as 0 but different from 1
     * ~B = 1111 0010 Reverse
     *
     * 2*8 = 16 2*2*2*2
     * << >>Where does the arrow go
     * <<   *2
     * >>   /2
     * */
    
    System.out.println(2<<3);//2*8

Conditional operator (ternary operator): x? y: z = judge x, if true, output y, and if false, output z

Extension operators: + =, - =, * =, / =. a+=b can be approximately regarded as a=a+b. When using the extension operator, the variable will cast the result to the type of the current variable when participating in the operation, and the result type of the operation will not be changed in the end. Assuming that the previous variable is of byte type, the result of the following expression operation is still of byte type. So in fact, i * = 0.1 is equivalent to: i = (int)(i * 0.1); Be careful in use, otherwise it is easy to cause accuracy loss.

int a = 10;
int b = 20;
int i = 1;

a+=b;
i *= 0.1;

System.out.println(a);//30
System.out.println(i);//0

2, Package mechanism

Generally, the inverted company domain name is used as the package name. The name is in the same package. To prevent naming conflict, if you want to use the members in a package, you need to use import.

import java.io.IOException;

Keywords: Java Back-end

Added by deception54 on Thu, 20 Jan 2022 02:09:29 +0200