Java Basics Trap (i)

Source: Internet
Author: User
Tags true true

This article is published in my blog.

Things separated for many years, to take up the language of Java, seemingly familiar with the fact that it is also very unfamiliar, think should be combed under the way to improve their own. Let's take a look at the string object in Java this time.

The classic first look at the following section of code, I would like to finally create a few objects, where is the difference?

    String s0 = new String ("luoliang.me");    String S1 = "luoliang.me";    String s2 = new String ("luoliang.me");

  

To answer this question, we need to figure out the new string () as well as the string pool, and let me take a slow start.

All strings in Java are stored in a pool of strings called string pools, where strings are maintained independently of the string class, and the process is to determine the existence of the string, if there is no need to create, otherwise create a.

That new string ("Luoliang.me") this, see the New keyword description will create an object, go back to the string pool to find out if there is no creation, after completion will be created in the heap this object, remember that this object is also the string "Luoliang.me", We all know about it here, so look at the following:

String s0 = new String ("Luoliang.me"); After executing this sentence, there will be 2 objects in the system, one is the "luoliang.me" of the string pool, and the other is the new string ("Luoliang.me") in the heap. Finally point the S0 to this object.

String S1 = "luoliang.me"; After executing this sentence, the object is not incremented in the system because S1 directly points to the "luoliang.me" of the string pool.

String s2 = new String ("Luoliang.me"); After executing this sentence, an object is added to the system because there is already a "luoliang.me" in the string pool that is not created, creates an object directly in the heap, and points the S2 to the object.

After concluding the execution of 3 sentences: There are 3 objects in the system, one in the string pool and the other 2 in the heap.

Now let's take a look.

    System.out.println (S0 = = S1);    System.out.println (S0 = = s2);    System.out.println (S1 = = s2);

  

This should be relatively simple, in the basic type = = Direct comparison is its value, but the object is compared to the reference, that is, the meaning of the address. Below I draw a picture representing the previous code in the memory case:

See this picture, I believe you know that 3 output is what! It's all false.

There may be people who don't believe that, so we can use the string's Intern () method (which represents the address of the string in its string pool) to compare, see below:

    System.out.println (S0 = = S0.intern ());       System.out.println (s0.intern () = = S1);    System.out.println (s0.intern () = = S2.intern ());    System.out.println (S1 = = S2.intern ());

  

The output is:

    False true to True true    

  

Let's take a look at some of the more interesting things that many people might not have imagined would be the result.

    String Luoliang = "Luoliang";    String Luo = "Luo";    String Liang = "Liang";    System.out.println (Hello = = "Luo" + "Liang");    System.out.println (Hello = = "Luo" + Liang);

  

Ask what the result of the output is, and the answer is:

    True    false

  

As for why, the literal constants are added equal to the direct addition and then stored in the string pool, and the literal value with the variable add heap in the heap to generate an object, so imagine how the address is!

Java Basics Trap (i)

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.