"Thinking in Java-chapter 3" operator

Source: Internet
Author: User

Priority assignment

objects are using C=d, and both C and D point to the object that was originally directed to D.

Alias problem in method invocation: When an object is passed as a parameter to a function, the function changes the parameter, then the object passed in is changed;

Self-increment and self-reduction

Floating-point variables are also available with + + or--.

Relational operators
double x=0.1; double y=0.1; SYSTEM.OUT.PRINTLN (x==y);

Output: True

Integer x=new integer,integer y=new integer (n); SYSTEM.OUT.PRINTLN (x==y);
System.out.println (X.equals (y));

Output: false

True

The basic data type directly uses = = and! = can, but in addition to the data type, if you want to compare equivalence, you need to use the Equals method;

logical operators
Integer x=new integer,integer y=new integer (n);  SYSTEM.OUT.PRINTLN (x 
&&y); Error, this is different from C + +

Short Circuit phenomenon:

Shift operator: >>,<<,>>>>>:

>> 2, the number 11 is shifted to the right 2 bits
The calculation process:11 of the binary form is: 0000 0000 0000 0000 0000 0000 0000 1011, and then the last two digits of the low position out, because the number is positive, so at the high level of 0. The final result is 0000 0000 0000 0000 0000 0000 0000 0010. Converting to decimal is 3. Mathematical Significance: Move right one is equivalent to 2, and the right shift n is equal to the n-th square divided by 2.

<<:

3 << 2, the number 3 is shifted to the left 2 bits:

Calculation process:
3 << 2
First convert 3 to binary number 0000 0000 0000 0000 0000 0000 0000 0011, then move the number high (left) two 0, the other numbers are shifted to the left 2 bits, and finally on the low (right) two empty fill 0. The resulting result is 0000 0000 0000 0000 0000 0000 0000 1100, then the conversion to decimal is 12. Mathematical Significance:
On the premise that the number is not overflow, for positive and negative numbers, the left one is the equivalent of multiplying by 2 by 1, and the left shift n is the equivalent of multiplying by 2 of the n-th square.

>>>:

In binary form, all numbers are shifted to the right by the corresponding number of digits, the low position is shifted out (discarded), and the upper slot is 0. For positive numbers, it is the same as a signed right shift, which is different for negative numbers.

Type conversions

When you convert a float or double to an integral type, the number is always truncated. If you want to get a rounding operation, you need to use the round () method in Java.lang.Math:

double x=0.5; System.out.println ((int) x);             // 0 System.out.println (Math.Round (x));         // 1

"Thinking in Java-chapter 3" operator

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.