Introduction to Java Basics-Operators

At the lowest level, data in Java is manipulated by using operators.

1.1 Simpler print statements

System.out.print("Hello World");
打印输出:Hello World

1.2 Using Java Operators

    The operator accepts one or more parameters and generates a new value. Common operators, plus sign (+), minus sign (-), multiplication sign (*), division sign (/) and assignment sign (=).

1.3 Priority

    When there are multiple operators in an expression, multiply and divide first and then add and subtract. Those with parentheses are counted first. However, in the program, you need to pay attention to the "+" operator.

System.out.println(1 + "1");
System.out.println(1 + 1);
System.out.println("hello" + "world");
输出值:
11
2
helloworld

"+" means "string concatenation", and if necessary, he also performs "string conversion". When the compiler observes that a String object is connected to this "+", and this "+" is connected to an element of a non-String object, it will try to convert the non-String type element to a String. As seen in the first line of code above, the Integer type 1 is connected to a "+" and then a String type "1" is spliced, and the result is the output string "11".

1.4 Assignment

        The thing operator "=" is used for assignment. Contrary to our usual understanding, we usually assign the value on the left side of the "=" equal sign to the right; but the "=" equal sign in the program means "take the value on the right side of the equal sign and assign it to the left" . The value on the right can be any constant, variable or expression (as long as it can generate a value). But the one on the left must be a clearly named variable. For example: a=1

       You cannot assign anything to a constant, and a constant cannot be the value on the left side of the equal sign (counter example: 1=a)

 

1.5 Arithmetic operators

    Java's basic arithmetic operators, plus (+), minus (-), multiplication (*), division (/), and modulus (%).

    Note: The difference between the division sign in the Java program and the actual arithmetic method is that when the division of the program uses integer division, there will be no remainder if the division is inexhaustible, only integers are taken, and they are not rounded. The result; if it is a double or float type value, the division can be calculated to the decimal point.

         Modulus (%) is to take the remainder from integer division

System.out.println(10/3);
System.out.println(10%3);
System.out.println(10.0/3.0);
System.out.println(10f/3);
输出结果:
3
1
3.3333333333333333
3.3333333

Unary operators : unary plus (+), unary minus (-), unary multiplication (*), unary division (/) and the corresponding binary addition, subtraction, multiplication, and division are all the same number. According to the written form of the expression, the compiler will automatically determine which one to use.

For example: x -= a; can be converted into x = x-a; x += b; can be converted into x = x + b;

             x *= c; can be converted into x = x * c; x /= d; can be converted into x = x / d;

1.6 Auto increment and decrement

      Increment and decrement are two pretty good shortcut operations. The decrement operator is "--" and the increment operator is "++". --a and a-- both express the same meaning, that is, a=a-1, the same is true for increasing. But there is something to pay attention to, the difference between the operator at the front and at the back. Usually called "prefix" in the front and "suffix" in the back. The difference is that the symbol in the front is calculated before the value is generated, and the symbol in the back is first assigned in the calculation. example:

int a = 1;
int b = 1;
int c = 1;
int d = 1;
System.out.println("a   " + ++a);
System.out.println("b   " + b++);
System.out.println("c   " + --c);
System.out.println("d   " + d--);

输出:
a   2
b   1
c   0
d   1

1.7 Relational operators

        Relational operators generate a boolean result, and they calculate the relationship between the value of the operand. If the relationship is true, the expression will generate true; if Guangxi is not true, then false. Relational operators include .63. Greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), equal to (==), not equal to (!=).

        Note: Equality and inequality apply to all basic data types, while other comparison operators are not applicable to boolean types. Should be a boolean value can only be true and false, greater than, less than, greater than or equal to, and less than or equal to the boolean type has no practical meaning.

    Equivalence of test objects

The relational operators == and != apply to all objects, but you need to pay attention if you want to compare two objects or whether the values ​​are the same.

String str1 = "a";
String str2 = "a";
String str3 = new String("a");
String str4 = new String("a");

System.out.println("str1==str2    " + (str1==str2));
System.out.println("str1==str3    " + (str1==str3));
System.out.println("str3==str4    " + (str3==str4));
System.out.println("str1.equals(str3)    " + (str1.equals(str3)));

输出:
str1==str2    true
str1==str3    false
str3==str4    false
str1.equals(str3)    true

       First of all, the values ​​of the four variables are all a. The reason why the output result is false is that the str1 and str2 variables are stored in the application of the constant pool in the memory, and the a value corresponding to these two variables is in the constant pool. In, it refers to the address in the same constant pool, so when str1==str2, the output is true; the a value in str3 and str4 is stored in the heap and is still in two different areas, str3 and str4 are stored It is a reference to the heap address, so the output is false when str3==str4. Therefore, when comparing the actual content of the object, it is recommended to use the equals() method; but it should be noted that if the equals() method is called in a custom class, the equals() method needs to be rewritten, otherwise the use of this method will not achieve To the effect we want, there will be problems.

1.8 Logical operators

The logical operators "and (&&)", "or (||)", and "not (!)" can generate a Boolean value (true or false) according to the logical relationship of the parameters.

System.out.println((1<2) && (1>3));
System.out.println((1<2) && (1<3));
System.out.println((1<2) || (1>3));
System.out.println((1<2) || (1<3));
System.out.println(1 != 2);

输出:
false
true
true
true
true

Note: If a boolean value is used where a String value should be used, the boolean value will be automatically replaced with the appropriate text form.

Also need to pay attention to the difference between bitwise and (&), bitwise or (|) and and (&&), or (||)

        And (&&) in the execution process, if any of the multiple conditions is false, it returns false, and the judgment logic condition will not be executed backward; or (||) in the execution process, if any of the multiple conditions is true, it returns true , And the judgment logic condition will not be executed backward. The difference between bitwise AND (&) and bitwise OR (|) is that after the conditions are met, they will also execute the judgment logic condition backwards, and the subsequent steps will be executed after all the logic conditions are executed. example:

List<String> lists = null;
if(lists == null || lists.size() == 0){
   System.out.println("集合为空");
}

输出:
集合为空

The same judgment logic, but one uses the OR (||), and the other uses the bitwise OR (|), and there are different results. Therefore, it is necessary to use bitwise or (|) and bitwise and (&) with caution in the development process. It is recommended to use and (&&) or (||) to save resources compared to it.

Please correct me if there are any errors, thank you.

Guess you like

Origin blog.csdn.net/qq_33369215/article/details/53454364