The String class in Java

Source: Internet
Author: User

  1. string s = new string ("abc"); After this sentence executes, there are two objects in memory, and a "hello" in the string pool generated by the new string ("Hello") is an object with a value of "Hello";
  2. The second one, created by new in the heap, is a "Hello" object, which is exactly a copy of the "Hello" in the string pool. The variable s finally points to the "Hello" object generated in the heap;
  3. String S1 = "Hello"; When this sentence executes, S1 goes to string pool to find out if there is an object with a value of "Hello", it is obvious that in the previous step the JVM has generated a "hello" object in the string pool, so S1 directly points to this "hello" in string pool;
  4. String s2 = new string ("Hello");//This sentence has another new, in Java where whenever new is encountered, the heap will be created with a fresh object. As a result, there is an "ABC" object in the heap after the execution of the sentence, which is different from the "ABC" generated after the first sentence, and the S2 finally points to the newly generated object.
  5. System.out.println (s = = S1); Therefore, the result of executing the subsequent print statement is three false
  6. System.out.println (s = = s2);
  7. System.out.println (S1 = = s2);

Question 2:
Java code

    1. System.out.println (s = = S.intern ());
    2. System.out.println (S1 = = S1.intern ());
    3. System.out.println (s1.intern () = = S2.intern ());

Set S is a variable of type string, when executing S.intern (), the JVM first in the string pool to find the string variable s equal (with the Equals () method) of the string, if there is a reference to return; The pool creates a string object that is equal to the value of S and returns its reference. From here we can conclude that the intern () method will return a reference to the string object in the string pool anyway.
Therefore, the result of the above program execution is false,true,true.
PS: set S and T for two string variables, if there is s.equals (t), there must be s.intern () = = T.intern ();
PS: "= =" will always compare the address of both sides of the object is equal.

Question 3:
Java code

    1. String Hello = "Hello";
    2. String hel = "hel";
    3. String lo = "Lo";
    4. System.out.println (Hello = = "hel" + "lo");
    5. System.out.println (Hello = = "hel" + lo);

The first three sentences produce "Hello", "hel", "Lo" three constant string objects in string pool
As the first addition connection, both sides of the + sign are constant strings, and Java will combine the two into a string pool to find equal (equals) strings, if any exist then return the address; A new constant object is created in the string pool. Its value equals the stitched string and returns its address.
There is a variable on either side of the second +, at which point Java creates a new object in the heap whose value is the value of the concatenation of the two strings, at which point the address returned is the address of the new object in the heap.
So, the first sentence after the + connection returns the address of "Hello" in string pool, apparently equal to the address of the variable hello;
The second sentence returns the address in the heap, which obviously varies from the address of the variable hello;

The String class in Java

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.