On Java operators and their priorities

Almost all operators can only operate on 'primitives'. The exceptions are' = ',' = ', and'! = ', which can operate on all objects. In addition, the string class supports' +' and '+ ='.

The base type stores the actual value. Not a reference to an object. Therefore, when assigning values to them, the contents of one place are directly copied to another place. For example, if a = B is used for the basic data type, the contents of B are copied to a. If you then modify a, and B will not be affected by this modification at all. (in the previous article, Java programming ideas (2) In, we know that basic types are stored in the stack. Assuming a = 4, if there is no 4 in the stack, a space will be opened up to make the value 4. After a = B, B also points to the 4 in the stack. At this time, a is again equal to 5. Then we will look for 5 in the stack again. If there is no 4, it will be opened up to make it 5, and then a points to 5. Therefore, it will not affect b)

However, when the object is "assigned", the situation will change. First of all, we know that for the operation of objects, what we really operate is the reference to objects. So if "assigning an object to another object" is actually copying the "reference" from one place to another. This means that if C = D is used for the object, both C and D will point to the object that only D points to (both remote controls (references) can operate one TV (object)).

Priority:

@H_ 943_ 301 @ (1) assignment

If the main type uses "a = B", the content at B is copied to a. If you modify a, B will not be affected by the modification at all.

When the object is "assigned", the situation changes. When we operate on an object, what we really operate on is its handle. Therefore, if "from one object to another" is assigned, it is actually copying the handle from one place to another. This means that if you use "C = D" for an object, both C and D will eventually point to the object that only D originally pointed to.

short s1 = 1; s1 = s1 + 1; (the result of S1 + 1 operation is int type, which needs to be cast)

short s1 = 1; s1 += 1; (can be compiled correctly) + = operator has no type conversion problem

@H_ 943_ 301 @ (2) arithmetic operator

Java's arithmetic operators: plus sign (+), minus sign (-), division sign (/), multiplier sign (*), and modulus (%), get the remainder from integer division. Integer division cuts decimals directly, not carry.

@H_ 943_ 301 @ (3) automatic increment and decrement

For pre increment and pre decrement (such as + + A or -- a), the operation will be performed first and regenerated into a value.

For post increment and post decrement (such as a + + or a --), the value is generated and the operation is performed.

@H_ 943_ 301 @ (4) relational operator

Relational operators include <, >, < =, > =, = ==

Equal and not equal apply to all built-in data types, but other comparisons do not apply to Boolean types.

To compare whether the actual contents of two objects are the same, you must use the special method equals() that applies to all objects.

The equals() method is not applicable to "main types", which use = = and directly= Just.

The default for equals() is the comparison handle. So unless you change equals () in your new class, it's impossible to behave as we want

Most Java class libraries implement equals (), so it actually compares the contents of objects, not their handles

== and= The object handle is compared, not the actual content of the object

@H_ 943_ 301 @ (5) logical operator

Logical operators & &, |! Can generate a Boolean value

&Both and & & can be used as logical operators "and", but & & is "short circuit and". During operation, judge the value of the expression in front of the symbol first. If the value of the whole expression can be determined, the expression behind the symbol will not be operated.

In addition, & can be used as a bitwise operator

@H_ 943_ 301 @ (6) bitwise operator

Bitwise and operator (&)

Bitwise OR operator (|)

Bitwise XOR (^, XOR)

Bitwise not (~, also known as the "not" operator) is a unary operator that generates the opposite value of the input bit

@H_ 943_ 301 @ (7) shift operator

The left shift operator (< <) can move the operand to the left by the specified number of bits to the right of the operator (fill 0 in the low order).

The signed right shift operator (> >) moves the operand to the right by the specified number of bits to the right of the operator. The signed right shift operator uses symbolic extension: if the value is positive, insert 0 in the high order; if the value is negative, insert 1 in the high order

Unsigned right shift operator (> > >), which uses "zero extension": 0 is inserted in the high order, whether positive or negative

@H_ 943_ 301 @ (8) ternary if-else operator

Boolean expression? Value 0: if the result of Boolean expression for value 1 is true, value 0 will be evaluated; otherwise, value 1 will be evaluated

@H_ 943_ 301 @ (9) string operator +

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>