<>1. Take a brief look first JVM Memory structure

Method area : This area is shared by each thread , Used to store class information that has been loaded by the virtual machine , constant , Static variable , Data such as code compiled by real-time compiler .
The constant pool is in this area

heap : Heap The area is shared by all threads , Created when the virtual machine starts . The function of this area is to store object instances , Almost all object instances allocate content here .Heap Main area managed by garbage collector .

<>2. Create string object
public class TestDemo { public static void main(String[] args) { String a = "1"
; String b = "1"; String c = b; System.out.println(a == b); System.out.println(c
== b); String d = new String("test"); String e = new String("1"); String f = new
String(a); System.out.println(d == a); System.out.println(e == d); String g =
"hello" + "tomorrow"; String h = new String("hello") + new String("world"); } }
first , to glance at
String a = "1"; String b = "1"; String c = b; System.out.println(a == b);
System.out.println(c == b);

Console return true

Analyze the output of the above code : Above code , Only one object was created

* first ,jvm At the compilation stage, it will judge whether there is a constant in the constant pool of the method area “1” This constant object (String a = "1";)
If so ,a A reference directly to this constant
without , Create this constant object in the constant pool
* This procedure does not create objects in the heap
* String b = "1"; Directly const object "1" I gave you my address b,String c = b; take b directive const object "1" I gave you my address c
* When used == Judgment time , It's all about comparison In constant pool const object "1" Address of , Therefore the same , return true
Then take a look
String d = new String("test");
analysis : Above code , Two objects were created

* first ,jvm At the compilation stage, it will judge whether there is a constant in the constant pool of the method area “test” This constant object , Create without
* secondly , adopt new stay heap Create in String object ,d That's it String Address of the object
Keep looking
String e = new String("1"); String f = new String(a); System.out.println(d == a
); System.out.println(e == d); System.out.println(e == f);

Console return false

Analyze the output of the above code : Above code , Created 2 Objects

* first , There is already in the constant pool “1” , And a It also points to “1” Address of
* therefore , This procedure is only used in the heap new Create two String object
* Although their string constant values are 1, however e and f It points to two different String Address of the object , So the return values are false
Last look :
String g = "hello" + "tomorrow"; String h = new String("hello") + new String(
"world");
First, let's analyze String g = "hello" + "tomorrow"; Only created 1 Objects

* jvm After compiler optimization in the compilation stage, string constants will be directly merged into "hellotomorrow", So only one is created in the constant pool “hellotomorrow”
const object
Let's go on String h = new String("hello") + new String("world"); Created 6 Objects

* first new Created a StringBuilder() object
* next h = new String("hello") + new String("world") Created 4 Objects ( The creation process is the same as above )
* final new Created an object String(“ab”)
<>3. supplement
Integer m = 3;// Create a constant object in the constant pool "3" String s = m.toString(m);
be careful : This procedure call Object.toString(), No new object was created in constant pool .

Technology
©2019-2020 Toolsou All rights reserved,
【 Explain in detail 】 Interview must ask :SpringBoot Automatic configuration principle 【 Hyper detail 】Java Realization of student information management system 2020 The 11th National Blue Bridge Cup Python group JavaScript note ( five )---- Branch statement MYSQL database SQL Sentence practice experiment EXERCISES. 1 SIMPLE COMMANDSMySQL Basics Commonly used sentence ( Add / delete / modify query )C++ of string of compare usage vue limit input Only positive numbers can be entered C Language of a Gobang game implementation Programmer refused due to low salary offer,HR become shame , Netizens instantly blew up ..