Re-interpretation of JAVA basics string-Create several objects __java

Source: Internet
Author: User

Change jobs, when changing jobs or looking for a job, often will inspire people's learning power-learn a little more can be more points of pay (hehe).

I am a mortal, although usually have the habit of learning, but the investment resume interview or good preparation for a turn, learning is the basic knowledge, but also a deeper layer of learning, in-depth to the source code, the principle level. More profound and basic than the previous study. The next series of articles will be related to these basics. The first Foundation is: string-to create several examples of objects.

string s = new String ("abc"); I also turned over an article many years ago (string s1= "abc"; with string S2=new string ("abc");-thinking about Java stacks and heaps), the problem, though based, But there are still places to learn:

/** * @author Ding Chengyun * 2014-2-9/public class Stringtest {public static void main (string[] args) {//test
		1 ();
	Test3 ();
public static void Test1 () {//Issue: 1, execute code the following three lines of code, create several objects separately. A: Execute line 1th, create two objects: First look for "ABC" Object in spring Pool, no, then put "ABC" object into string pool,//new process will create a content of '
The ABC ' object is placed in the heap, and the variable s points to ABC in the heap.
Executes the 2nd line of code without creating an object because the string already exists in string pool.
		Executing line 3rd creates 1 objects, because there are values in the string pool, so only one object is created in the heap, and the variable S2 points to the object in the heap.
		string s = new String ("abc");
		String S1 = "abc";
		
String s2 = new String ("abc");
Question: 2, execute the following three lines of code results.
		A: = = To determine whether the variable refers to the same address, all false System.out.println (s = = S1);
		System.out.println (S1 = = s2);
		
System.out.println (s = = s2);
Question: 3, execute the following three lines of code, the result is what. A: The Intern () method returns the result: When the Intern method is invoked, if the string pool already contains a string equal to this string object (the object is determined by the Equals method), the word in string pool is returned
Character string.
Otherwise, this string object is added to the pool and a reference to this string object is returned. It follows that for any two strings s and T, when and only if S.equals (t) is true, s.intern () = = T.intern () is true//solution: At this point, the reference address of the variable s is the object "ABC" in the Heap and S.intern () returns the"ABC" of String pool and therefore false System.out.println (s = = S.intern ());
		Solution: The following code executes, the return is "ABC" in string pool, and therefore all true System.out.println (s.intern () = = S1.intern ());
		System.out.println (s.intern () = = S2.intern ());
		
	System.out.println (s1.intern () = = S2.intern ());
		The public static void Test2 () {String hello = "Hello";
		String hel = "hel";
		
String lo = "Lo";
		Explanation: + Both sides are constants, first go to string pool to see if there is a concatenation of the string, if there is a direct return to the object in string pool.
System.out.println (Hello = = "hel" + "lo");
		Explanation: + There is not a constant on both sides, then first to create a new object into the heap System.out.println (hello = = "hel" + lo);
	System.out.println (Hello = = hel + lo);
public static void Test3 () {//issue: What the following code will output. Answer: TRUE.
		Parsing: "abc" in string pool, both variables A and B point to this ABC in string pool, so return true string a = "abc";
		String B = "abc";
	System.out.println (A==B);
 }
}


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.