Thinking in Java [Java programming mechanism] learning notes-Operator

Source: Internet
Author: User
Document directory
  • Note 1:
  • NOTE 2:
Thinking in Java [Java programming mechanism] learning notes -- operator operator1. the first operator to be explained is "=" NOTE 1:
If = is used between two references, two references are compared, regardless of whether the value of the referenced object is the same. Therefore, when comparing integer and string values,Strongly recommendedTo use equals.
But when can I use = to compare strings? Here is an example reference:
// These two have the same valuenew String("test").equals("test") ==> true // ... but they are not the same objectnew String("test") == "test" ==> false // ... neither are thesenew String("test") == new String("test") ==> false // ... but these are because literals are interned by // the compiler and thus refer to the same object"test" == "test" ==> true // concatenation of string literals happens at compile time resulting in same objects"test" == "te" + "st"  ==> true// but .substring() is invoked at runtime, generating distinct objects"test" == "!test".substring(1) ==> false
NOTE 2: double and float size comparison. When comparing double and float, avoid using = and! =. The reason is that floating point numbers have precision, and the precision is the smallest number that double or float can represent. For example, double d1 = 0.1f; double D2 = 0.1; D1 and D2 are not equal because 0.1 cannot be divisible by Double precision. Therefore, we usually use a small floating point number as the precision unit. the shift operator ">>"," <"and" >>> "in the bitwise operator indicates that" 0xffffffff is-1; 0x0000000 is the largest negative number "Left shift: <, signed shift operation
During the Left shift operation, the binary code of the number of operations is shifted to the specified number of digits, and the empty space after the Left shift is supplemented with 0. In Java, the specific operation to move left has no relationship with the sign bit "1" and "0". Whether it is a positive number or a negative number, it will be multiplied by 2 after moving left. therefore, if the smallest negative number is shifted to 1, it will overflow to 0. for example
int n1 = 0x80000000;int n2 = n1 << 2;n1 : 0x00000000

Right Shift:>, signed shift operation

The right-shift operation shifts the binary code of the number of operations to the specified number of digits. the empty space after the right-shift is supplemented by the symbol bit. That is, if the positive number is supplemented by 0, the negative number is supplemented by 1.
int n1 = 0x80000000;int n2 = n1 >> 2;n1 : 0xE0000000
The value of the unsigned shift is only shifted to the right, and the value of ">>>" is not shifted to the left. 0 is added.
int n1 = 0x80000000;int n2 = n1 >> 2;n1 : 0x20000000

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.