Java String two different assignment comparisons

Source: Internet
Author: User

Original: Http://blog.163.com/[email protected]/blog/static/1271436362012101214031911/

First, make it clear that string is a class. Let's focus on two issues

A) Two ways to assign objects of the string class

b) Why the object of the string class can be directly assigned a value

a) 1 resembles a normal object, creating a String object from new . String str = new String ("Hello"); Memory graph as shown, the system first creates an anonymous object "Hello" into the heap memory (we call it a), and then the new keyword in the heap in memory to open up another piece of space, and then put "Hello" in, and return the address to the stack in the memory of STR, when a object becomes a garbage object , because it is not pointed to by any of the variables in the stack, it is automatically reclaimed by the GC.

a) 2 direct assignment .  such as String str = "Hello"; First go to the buffer pool to find whether there is a "hello" object, if not, then create a new, and into the pool, so this assignment has a benefit, the next time if there is a string object is also defined as "Hello" with a direct assignment, you do not need to open up new heap space, but still point to the pool " Hello "

The following code can test this conclusion and the final output is true,We use the = = Test, output True, indicating that both addresses are the same a) 3 manual entry PoolEven if you use the New keyword, the first way to assign a value, you can also use a manual pool instruction in Java, so that the created object into the pool, can still be reused later, using the following two pieces of code can be tested, such as the result is obviously false, because the address is different.

In the following procedure, we use the Intern () method, manually into the pool, so the result is True.public class teststring
{
public static void Main (String args[]) {
String str1 = new String ("Hello"). Intern ();
String str2 = "Hello";
System.out.println (STR1==STR2);
}
} in the following program, STR2 does not use direct assignment, so the result becomes Falsepublic class teststring
{
public static void Main (String args[]) {
String str1 = new String ("Hello"). Intern ();
String str2 = new String ("Hello");
System.out.println (STR1==STR2);
}
} in summary, in development, the use of direct assignment in the way, obviously more efficient. b) Why the object of the string class can be directly assigned a value


Opened the String.class, there is an introduction:

[Java]View Plaincopy
  1. /**
  2. * The <code>String</code> class represents character strings. All
  3. * string literals in Java programs, such as <code> "ABC" </CODE>
  4. * Implemented as instances of this class.
  5. * <p>
  6. * Strings is constant; Their values cannot be changed after they
  7. * is created. String buffers support mutable strings.
  8. * Because String objects is immutable they can be shared. For example:
  9. * <p><blockquote><pre>
  10. * String str = "ABC";
  11. * </pre></blockquote><p>
  12. * is equivalent to:
  13. * <p><blockquote><pre>
  14. * Char data[] = {' A ', ' B ', ' C '};
  15. * String str = new string (data);
  16. * </pre></blockquote><p>
  17. * Here is some more examples of what strings can be used:
  18. * <p><blockquote><pre>
  19. * SYSTEM.OUT.PRINTLN ("abc");
  20. * String CDE = "CDE";
  21. * SYSTEM.OUT.PRINTLN ("abc" + CDE);
  22. * String c = "abc". substring (2,3);
  23. * String d = cde.substring (1, 2);
  24. * </pre></blockquote>
  25. * <p>
  26. */

Through the above introduction, we can clearly, direct assignment, is through the compiler in effect, when you do not "ABC" is created by new, he will automatically default to you call the constructor new String (Char value[]). The constructor of string is not explicitly invoked (called explicitly by new), but the JDK compiler will automatically add it to you.

Java String two different assignment comparisons

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.