The String Constant Pool
2016-04-15 10:27
423 查看
In the previous post, we have learned about string immutability. Another interesting thing about strings in java is The String Constant Pool. So what is it exactly? It is a special place where the collection of references
to string objects are placed. What makes this so special? we will try to understand it now.
In our tutorial about String immutability, we have learned that string literals cannot be modified and multiple reference variable can refer to the same string literal. Let us first write a program to understand object comparison and references
The output of the above code is
Now lets know what happens here step by step
The class is loaded when JVM is invoked.
JVM will look for all the string literals in the program
First, it finds the variable
and it will be created in the memory
A reference for the literal “
Then it finds another variable
Now that JVM has already found a string literal “
refer to the same object i.e. “
The diagram below demonstrates this
String literals referred
Now we have looked into the case when string literals are created without using the
As we are invoking the
will be created when the new String(“prasad”) is invoked. This is unlike the string literal “
Now the values of objects referenced by variable
“prasad” but those are not the same objects. They refer to different objects. We will verify this with a program
This outputs,
The contents of both objects are the same so
The objects referred by both variables are different so
This is elaborated in this diagram
String with new keyword
Points to be remembered
String literals with same values will always refer to the same String object
String objects created using new operator will be different from literals
Hope this helped clarify doubts regarding string literals.
from: http://www.thejavageek.com/2013/06/19/the-string-constant-pool/
to string objects are placed. What makes this so special? we will try to understand it now.
In our tutorial about String immutability, we have learned that string literals cannot be modified and multiple reference variable can refer to the same string literal. Let us first write a program to understand object comparison and references
123456789 | public class StringConstantPool { public static void main(String[] args) { String s = "prasad"; String s2 = "prasad"; System.out.println(s.equals(s2)); System.out.println(s == s2); }} |
1 2 | true true |
The class is loaded when JVM is invoked.
JVM will look for all the string literals in the program
First, it finds the variable
swhich refers to the literal “
prasad”
and it will be created in the memory
A reference for the literal “
prasad” will be placed in the string constant pool memory.
Then it finds another variable
s2which is referring to the same string literal “
prasad“.
Now that JVM has already found a string literal “
prasad“, both the variables
sand
s2wil
refer to the same object i.e. “
prasad“.
The diagram below demonstrates this
String literals referred
Now we have looked into the case when string literals are created without using the
newoperator. What happens if
String s2 = new ("prasad");
As we are invoking the
newkeyword, The object “
prasad”
will be created when the new String(“prasad”) is invoked. This is unlike the string literal “
prasad” which is created when class is loaded.
Now the values of objects referenced by variable
sand variable
s2are the same i.e.
“prasad” but those are not the same objects. They refer to different objects. We will verify this with a program
123456789 | public class StringConstantPool { public static void main(String[] args) { String s = "prasad"; String s2 = new String("prasad"); System.out.println(s.equals(s2)); System.out.println(s == s2); }} |
1 2 | true false |
equalsmethod returns
true
The objects referred by both variables are different so
==operator returns
false
This is elaborated in this diagram
String with new keyword
Points to be remembered
String literals with same values will always refer to the same String object
String objects created using new operator will be different from literals
Hope this helped clarify doubts regarding string literals.
from: http://www.thejavageek.com/2013/06/19/the-string-constant-pool/
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- android上改变listView的选中颜色
- String.intern
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- 电脑自动重启提示 Verifying DMI pool Data的解决方法
- PostgreSQL ERROR: invalid escape string 解决办法
- 硬盘启动提示verifying DMI Pool Data错误的解决方法
- 揭秘SQL Server 2014有哪些新特性(2)-固态硬盘 Buffer Pool(缓冲池) 扩展
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- C#中string和StingBuilder内存中的区别实例分析
- PHP STRING 陷阱原理说明
- c#中 String和string的区别介绍
- C#实现的图片、string相互转换类分享
- asp.net String.IsNullOrEmpty 方法
- JavaScript中字符串(string)转json的2种方法