Java operator auto-increment and auto-subtraction are obfuscated points

Source: Internet
Author: User
Tags integer division

Almost all operators can only operate on Primitives ). The exceptions are "=", "=", and "! = ", They can operate on all objects. In addition, the String class supports "+" and "+ = ".

(1) Assignment

If "A = B" is used for the primary type, the content at "B" is copied to ". If A is modified, B is not affected by the modification.

The situation changes when the object is assigned a value. When operating on an object, what we actually operate on is its handle. Therefore, if a value is assigned "from one object to another", the handle is actually copied from one place to another. This means that if "C = D" is used for the object, C and D will eventually point to the object that was initially directed only by D.

Short s1 = 1; s1 = s1 + 1; (the s1 + 1 operation results in int type, which requires forced conversion)

Short s1 = 1; s1 + = 1; (correctly compiled) + = Operator no type conversion problem

(2) Arithmetic Operators

Java Arithmetic Operators: plus sign (+), minus sign (-), division sign (/), multiplication sign (*), and modulus (%, get the remainder from integer division ). Integer division directly removes decimal places rather than carry places.

(3) Auto increment and decrease

For the ascending and descending values (for example, ++ A or -- A), the operation is performed first and then the value is generated.

For post-incrementing and post-incrementing (such as A ++ or A --), the post-incrementing value is converted into A value and then executed.

(4) Relational operators

Relational operators include <,>, <=, >=, =, and ,! =

Equals and not equals 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 () applicable to all objects ().

The equals () method is not applicable to the "primary type". For those types, use = and! =.

By default, equals () is a comparison handle. So unless equals () is changed in our new class, it is impossible to show the expected behavior.

Most Java class libraries implement equals (), so they actually compare the content of objects rather than their handles.

= And! = Compare the object handle, not the actual content of the object

(5) logical operators

Logical operators &, | ,! Generate a Boolean value.

& Can be used as the logical operator "and", but & is "short circuit and". When calculating, you must first determine the value of the expression before the symbol. If you can determine the value of the entire expression, the operation of the expression after the symbol is not performed.

In addition, & can be used as bitwise operators

(6) bitwise operators

Bitwise AND operator (&)

Bitwise OR operator (|)

Bitwise XOR (^, exclusive or)

By bit NOT (~, It is also called the "Non" operator). It belongs to the unary operator and generates a value opposite to the input bit.

(7) Shift Operators

The Left shift operator (<) can move the operation object to the right of the operator (0 in the low position ).

The signed right shift operator (>) moves the operator object to the right to the specified number of digits on the right of the operator. The signed right shift operator uses symbol 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": no matter whether it is positive or negative, 0 is inserted at a high position.

(8) ternary IF-ELSE Operator

Boolean expression? Value 0: if the value of 1 "Boolean expression" is true, the value 0 is calculated. Otherwise, the value 1 is calculated"

(9) string operator + www.2cto.com

Int x = 0, y = 1, z = 2;
System. out. println ("out:" + x + y + z );
Here, the Java compiler converts x, y, and z into their string forms, instead of adding them together first.

When "String +" is used, if the expression starts with a String, all subsequent computation objects will be converted to the String.

To connect a string using the plus sign (using an earlier Java version), make sure that the first element is a string.

(10) Cast operator

For a Narrowing Conversion (data type that can accommodate more information, convert it to a smaller type, such as int to short ), in this case, information may be lost. At this time, the compiler will force us to make a clear shape

For "Widening conversion", you do not need to make a clear shape, because the new type will certainly accommodate the original type of information and will not cause any information loss.

A boolean value (bollean) does not allow any styling at all, and any other primary type can shape each other.

After the float or double value is modeled as an integer, the fractional part is always cut off without any carry processing.

Math. round (11.5) and so on? Math. round (-11.5) and so on?

Math. round (11.5) = 12 Math. round (-11.5) =-11

The round method returns a long integer closest to the parameter. After the parameter is added to 1/2, the floor is obtained.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.