Java Expression Traps

Source: Internet
Author: User

String str1 = "Length of Hello Java: 10";

String str2 = "Length of Hello Java: 10"; String STR3 = "Hello java length:" + "Hello java". length;
String STR4 = "Hello" + "Length of Java: 10";    System.out.print (str1 = = str2); Output: True
System.out.print (str1 = = STR3); Output: false
System.out.print (str1 = = STR4); Output: True

This can be obtained from the above examples. For the direct amount of characters in Java, the JVM uses a string pool to hold them, and in general the string objects in the string pool are not garbage collected, and when the program needs to use the string again, it does not need to recreate a new string, but rather a direct reference to the existing string. Creates a string object through an expression, if the value of the string join expression can be determined at compile time. The JVM then computes the value of the string variable at compile time and points it to the corresponding string in the string pool.
The string may never be used in the future, but the string is not garbage collected because it will always exist in the string pool---this is one of the reasons for a Java memory leak.
Note: System.identityhashcode (str); You can get a unique hashcode value for an object, and the return value of this indentityhashcode () is independent of whether the class overrides the Hashcode () method. Only if two objects are the same, their values will be the same.

It is recommended that string strings be created with Stringbuiler.

StringBuilder str = new StringBuilder ("Hello");
System.out.println (str);
SYSTEM.OUT.PRINTLN (System.identityhashcode (str));
Str.append ("Java");
System.out.println (str);
SYSTEM.OUT.PRINTLN (System.identityhashcode (str));
Str.append (", crazyit.org");
System.out.println (str);
SYSTEM.OUT.PRINTLN (System.identityhashcode (str));

Output Result:
Hello
2046136590
Hello Java
2046136590
Hello Java, crazyit.org
2046136590

Traps for compound assignment operations:
Short svalue = 5;
svalue = sValue-2;
Compile pass. Because the type of the SVALUE-2 expression is automatically promoted to the int type, the program causes a compilation error when assigning a value of type int to the short type.
However, the above code revision is changed to
Short svalue = 5;
Svalue-= 2;
The error is not compiled.
Compound assignment operators contain automatic coercion of type conversions.
The above code is: svalue = (short) sValue-2;
The problem is that it may be in the casting process. There is a high intercept, resulting in loss of data.
The variable on the left of the + = operator of the string type can only be of type string, not the parent type of string (for example, Object or charsequence)

Java Expression Traps

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.