Almost all operators can only operate on "primitives". Exceptions are "=", "==" and "!=", which operate all objects. In addition, the String class supports "+" and "+=".
The basic type stores the actual numerical value. Instead of referring to an object. So when assigning values to it, the content of one place is directly copied to another place. For example, if a=b is used for the basic data type, then the content of b is copied to a. If a is modified, b will not be affected by this modification at all. (In the previous Java programming idea (2), we know that the basic type is stored in the stack. Assuming a=4, if there is no 4 in the stack, a space will be opened to make the value 4. After a=b, b also points to the 4 in the stack. At this time, a is equal to 5. Then 5 will be searched again in the stack. If not, it will be opened to make it 5, and then a points to 5. Therefore, it will not affect b)
However, when "assigning values" to an object, the situation will change. First of all, we know that what we really operate on when operating an object is a reference to the object. So if "assigning an object to another object", it is actually copying the "reference" from one place to another. This means that if c=d is used on the object, both c and d will point to the object that was originally only pointed to by d (both remote controls (references) can operate and press a TV set (object)).
Priority:
| Priority | Bonding | |
1 | Suffix operator | [ ] . ( ) (Function Call) | From left to right |
2 | Monologic operator | ! ~ ++ -- +(single operand) (single operand) | From right to left |
3 | create | new | From left to right |
4 | Multiplication and division | */ % | From left to right |
5 | Addition and subtraction | + - | From left to right |
6 | Shift | << >> >>>> | From left to right |
7 | relation | < <= > >= instanceof | From left to right |
8 | equal | == != | From left to right |
9 | bitwise and | & | From left to right |
10 | bitwise xor | ^ | From left to right |
11 | bitwise or | | | From left to right |
12 | Logic and | && | From left to right |
13 | Logical or | || | From left to right |
14 | condition | ? : | From right to left |
15 | Assignment | = += -= *= /= %= ^= <<= >>= >>>= | From right to left |
(1) Assignment
The main type uses "A=B", and the content at B is copied to A. If A is modified, then B will not be affected by the modification at all.
The situation changes when the object is "assigned". When operating an object, what we really operate on is its handle. So if you assign a value from one object to another, it is actually copying the handle from one place to another. This means that if "C=D" is used for the object, then C and D will eventually point to the object that only D points to in the first place.
short s1 = 1; s1 = s1 + 1; (The result of s1+1 operation is int type, and a cast type is required)
short s1 = 1; s1 += 1; (can be compiled correctly) += operator without type conversion problem
(2) Arithmetic operator
Java's arithmetic operators: plus sign (+), minus sign (-), division sign (/), multiplication sign (*) and modulus (%, obtain the remainder from integer division). Integer division will cut the decimals directly, rather than carry.
(3) Automatic increment and decrease
For pre-increment and pre-increment (such as ++A or --A), the operation will be performed first and the value will be generated.
For post-increment and post-increment (such as A++ or A--), the value will be generated and the operation will be performed.
(4) Relational operator
Relational operators include <, >, <=, >=, = =, !=
Equal and not equal to apply to all built-in data types, but other comparisons do not apply to boolean types.
To compare whether the actual content of the two objects is the same, you must use the special method equals() that applies to all objects.
The equals() method does not apply to "main types". Those types can be used directly with == and !=.
The default of equals() is the comparison handle. So unless you change equals() in your new class, it is impossible to show the behavior we want
Most Java class libraries implement equals(), so it actually compares the contents of objects rather than their handles.
= = and ! = compares the object handle, not the actual content of the object
(5) Logical operator
Logical operators &&,||,! can generate a Boolean value
& and && can be used as logical operators "AND", but && is "short-circuit AND". When calculating, the value of the expression before the symbol is first judged. If the value of the entire expression can be determined, the operation of the expression after the symbol is not performed.
In addition, & can be used as a bit operator
(6) Bitwise operator
Bitwise AND operator (&)
Bitwise OR operator (|)
bitwise XOR (^, XOR)
Bitwise NOT (~, also called the "non" operator) belongs to a unary operator, generating a value opposite to the input bit.
(7) Shift operator
The left shift operator (<<) can move the operation object to the left to the number of bits specified to the right of the operator (complement 0 at the low bit).
The signed right shift operator (>>) moves the operation object to the right the number of bits specified to the right of the operator. The signed right shift operator uses sign extension: if the value is positive, 0 is inserted at the high position; if the value is negative, 1 is inserted at the high position
Unsigned right shift operator (>>>), which uses "zero extension": insert 0 at high position regardless of positive or negative
(8) Triple IF-ELSE operator
Boolean expression? Value 0: Value 1 The result of "Bolean expression" is true, and "value 0" is calculated, otherwise "value 1" is calculated
(9) String operator +
int x = 0, y = 1, z = 2;System.out.println("out:" + x + y + z);Here, Java compiler converts x, y, and z into their string form instead of adding them together first
When using "String +", if the expression starts with a String, then all subsequent operation objects will be converted to a string.
To connect a string with a "plus" sign (using earlier versions of Java), be sure to make sure the first element is a string
(10) Modeling (Cast) operator
For operations such as "Narrowing Conversion" (data types that can accommodate more information and convert them into smaller types, such as int to short), you may face the risk of information loss. At this point, the compiler will force us to make clear shapes
For "Widening conversion", there is no need to be explicitly shaped, because the new type can definitely accommodate the original type of information and will not cause any loss of information
Boolean values (bolleans) do not allow any styling processing at all, and any other main types can be shaped together.
After shaping the float or double value into an integer value, the decimal part is always "cut off" without any carry.
How much does Math.round(11.5) equal? How much does Math.round(-11.5) equal?
Math.round(11.5)==12 Math.round(-11.5)==-11
The round method returns the long integer closest to the parameter. After adding 1/2 to the parameter, find its floor.
Summarize
The above is all about this article discussing Java operators and their priorities, and I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Multimode string matching algorithm principle and Java implementation code
Detailed explanation of how to use Rxjava function operators
A brief discussion on the conversion between string arrays, strings and shaping in Java
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!