Java Operator details

Source: Internet
Author: User
I. Similarities and Differences between m + = n and m = m + n

In general, the two are the same, but there is a difference in the following example.

Public class Test {public static void main (String [] args) {short width = 10; width + = 2; // okSystem. out. println (width); width = width + 1; // error. If the type with a large range is converted to a type with a small range, the precision will be lost. The System must be forcibly converted. out. println (width );}}

The following error is reported:

D: \ workspace \ Java \ project261 \ + => javac Test. javaTest. java: 9: possible loss of precision found: int required: short width = width + 1; ^ 1 error

The reason is that the + = Operator will automatically perform forced type Conversion Based on the variable type, which is the fundamental difference between the two.

Ii. Differences between & and &

If you have used C ++, you will naturally use the & operator, which is called short circuit and. If the first condition is false, the result is returned without executing the second condition. There are two advantages: one is higher efficiency than the & operator, and the other advantage is that the second condition can be a dangerous operation, but the first condition is not, then the result is returned directly, this dangerous operation is not performed, for example, the object is null.

3. The + operator is a magic function in string operations.

1. String string = "" + 123; // the principle is the same as the first one. 123 is directly completed without the need for forced conversion. Such code is often seen in projects. Are we very lazy?

2. String tmp1 = "I lo ";

String tmp2 = "ve you ";

String string = tmp1 + tmp2;

The internal principle is that the compiler uses the append method of the StringBuilder class to perform this operation. For details, see when to use String and StringBuilder in Java.

3. Replay, = and equals ()

= Is a relational operator that compares the value of a basic type variable or the address referenced by a reference type variable.

The equals () method is a method of the Object class. It compares the stored content pointed to by reference type variables.

Do not mix.

4. Two Methods of String Construction

1. String string = new String ("I love you ");

2. String string = "I love you ";

The String type provides a mechanism called a buffer (concept). When the second method is used to construct a String, the compiler (runtime ?) Searches for strings with the same content in the buffer. If yes, you do not need to create a new one. If not, you can create one in the buffer. Improved efficiency. The first method constructs a new string each time. In contrast, it is better to use the second method.

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.