1, Type of operator
-
Arithmetic operators: +, -, *, /,%, + +, –
-
Assignment operator:=
-
Displacement operators: <, > >, > >, < < =, > > =, > > >=
-
Bitwise operators: &, |, ~^
-
Relational operators: >, > =, <, < ===
-
Logical operators: & &, ||,!
-
Ternary operator:?:
-
String operation operators: ++=
2, Use of operators
1. Assignment (=)
The assignment operator is "=", which means that the value on the right is copied or passed to the value on the left. The value on the right can be any constant, variable or expression (as long as it can generate a value). But the value to the left of the foot must be an explicit, named variable. But you can't assign anything to a constant.
The code is as follows (example):
public class Tank { float a = 0; public static void main(String[] args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.a = 3; t2.a = 2; t1.a = t2.a; System.out.println("t1.a:"+t1.a); System.out.println("t2.a:"+t2.a); } }
2. Arithmetic operator
- Java arithmetic operators include plus sign (+), minus sign (-), division sign (/), multiplication sign (*), and modulus (%). Integer division removes the decimal places of the result directly, rather than rounding the result.
- Self increase (+ +) and self decrease (–): increase or decrease by one unit. When the current infix is increasing or decreasing, it is calculated first and then assigned; When the suffix increases or decreases, copy first and then operate.
The code is as follows (example):
```java public class Tank { public static void main(String[] args) { int a = 1,b= 1; System.out.println("a+b:"+(a+b)); System.out.println("a-b:"+(a-b)); System.out.println("a*b:"+(a*b)); System.out.println("a/b:"+(a/b)); System.out.println("a++:"+(a++)); System.out.println("++a:"+(++a)); System.out.println("b--:"+(b--)); System.out.println("--b:"+(--b)); System.out.println("a:"+a); System.out.println("b"+b); // a+b:2 // a-b:0 // a*b:1 // a/b:1 // a++:1 // ++a:3 // b--:1 // --b:-1 // a:3 // b-1 } }
3. Relationship budget discrepancy
Relational operators generate the result of a Boolean value and compare the relationship between operands. True if true; False if false; Where equal and not equal apply to all basic data types, while other comparators do not apply to Boolean types. Because the Boolean value itself is true and false, the size has no practical significance for it.
- ==And= The comparison is the reference of the object. Because when an object is created, a space is allocated in the stack.
- If you want to compare the contents of two objects, use the equals() method.
public class Tank { public static void main(String[] args) { Integer a = new Integer(47); Integer b = new Integer(47); System.out.println(a==b); System.out.println(a.equals(b)); // false // true } }
4. Logical operator
In the logical budget symbol, "and (& &)," or (|), "not (!)" The operation is adapted with Boolean values. A non Boolean value cannot be used in a logical expression as a Boolean value.
If a Boolean value is used where a String value is used, the Boolean value is automatically converted to the appropriate text form.
Short circuit phenomenon:
&&(and) is also called short-circuit and, because as long as the current term is false, it will not judge later and directly think that the expression is false
||(or) is also called short-circuit or, because as long as the current term is true, it will not judge later and directly think that the expression is true
public class Tank { static boolean test1(int val) { System.out.println("test1(" + val + ")"); System.out.println("result:" + (val < 1)); return val < 1; } static boolean test2(int val) { System.out.println("test2(" + val + ")"); System.out.println("result:" + (val < 2)); return val < 2; } static boolean test3(int val) { System.out.println("test3(" + val + ")"); System.out.println("result:" + (val < 3)); return val < 3; } public static void main(String[] args) { System.out.println("----------&&Short circuit test----------"); boolean a = test1(0) && test2(2) &&test3(1); System.out.println("a:" + a); System.out.println("----------||Short circuit test----------"); boolean b = test1(1) || test2(1) || test3(4); System.out.println("b:" + b); } // ----------&&Short circuit test---------- // test1(0) // result:true // test2(2) // result:false // a:false // ----------||Short circuit test---------- // test1(1) // result:false // test2(1) // result:true // b:true }
5. Bitwise operator
The bitwise operator is used to operate a single "bit" in the basic data type of integers, that is, binary bits. Perform Boolean algebra operation on the corresponding of the two parameters, and finally generate a result.
- If both input numbers are 1, the test generates an output bit 1 according to the bit "and (&)", otherwise an output bit 0 is generated;
- If the input bit has only one 1, an output bit 1 is generated according to the bit "or (|). An output bit 0 is generated only when both input bits are 0;
- If one of the input bits is 1, an output bit 1 is generated by bit XOR (^).
- Bitwise "not (~)", also known as the negation operator. It generates a value opposite to the input bit - if you enter 0, it generates 1; If 1 is entered, 0 will be generated;
6. Shift operator
The shift operation bit is used to process the basic data type of integer, and the operation object is also binary "bit".
- Left displacement operator (< <): move the operand on the left of the operator to the left according to the number of bits specified on the right of the operator (fill 0 in the low order).
- Right displacement operator (> >): moves the operand on the left of the operator to the right according to the number of bits specified on the right of the operator.
- The "signed" right displacement operator uses "symbol extension": if the symbol is positive, insert 0 in the high position; If the weak sign is negative, insert 1 in the high order. Unsigned right displacement operator (> > >), using zero extension. That is, no matter positive or negative, insert 0 in the high position;
- If the values of char, byte and short are displaced, they will be converted into int type before displacement, and the result is also int type.
- Displacement can be combined with the equal sign (< =, > > =, > > > =), which means that the value on the left of the operator will move the number of digits specified by the value on the right. Then assign the result to the variable on the left.
7. Ternary operator
Ternary operators are also called conditional operators. If the Boolean expression is true, value1 is taken. If the expression is false, value2 is taken;
The expression is:
Boolean-exp ?value1 : value2;
8. String operators + and+=
This operator is used to connect different strings. If the expression starts with a string, all subsequent operands must be string.
9. Type conversion operator
Java automatically converts one data type to another.
- Boolean types are not allowed to be converted into any type, and other basic types can be converted into other basic data types.
- When converting float and double to integer values, truncation is always performed on the number. If you want to get the result of rounding, you need to use Java The round() method in lang. math.
10. Java has no sizeof() operator
In C and C + +, the sizeof() operator can tell you the number of bytes allocated for data items. The biggest reason for using this operator is to perform some operations related to storage space, so that the program can be "transplanted" on different platforms.
Java does not need the sizeof() operator to meet this need, because all data types have the same size in all machines. We don't have to think about migration - it's already designed in the language.