java字符串常量池,作用类似缓存,节省heap空间和加快对象生成:
测试代码:
public class Start { public static void main(String[] args) { String s1 = "abc"; String s2 = "abc"; String s3 = new String("abc"); String s4 = new String("abc").intern(); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); System.out.println("========="); System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println("========="); System.out.println(s1 == s4); System.out.println(s1.equals(s4)); } }
测试结果:
true
true
=========
false
true
=========
true
true
为何s1 == s2 为true?
http://www.journaldev.com/797/what-is-java-string-pool
As the name suggests, String Pool is a pool of Strings stored in Java Heap Memory. We know that String is special class in java and we can create String object using new operator as well as providing values in double quotes.
Here is a diagram which clearly explains how String Pool is maintained in java heap space and what happens when we use different ways to create Strings.
String Pool is possible only because String is immutable in Java and it’s implementation of String interningconcept. String pool is also example of Flyweight design pattern.
String pool helps in saving a lot of space for Java Runtime although it takes more time to create the String.
When we use double quotes to create a String, it first looks for String with same value in the String pool, if found it just returns the reference else it creates a new String in the pool and then returns the reference.
However using new operator, we force String class to create a new String object and then we can useintern()
method to put it into the pool or refer to other String object from pool having same value.
Here is the java program for the String Pool image:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.journaldev.util;
public class StringPool {
/**
* Java String Pool example
* @param args
*/
public static void main(String[] args) {
String s1 = "Cat" ;
String s2 = "Cat" ;
String s3 = new String( "Cat" );
System.out.println( "s1 == s2 :" +(s1==s2));
System.out.println( "s1 == s3 :" +(s1==s3));
}
} |
Output of the above program is:
1
2
|
s1 == s2 : true
s1 == s3 : false
|
相关推荐
The term business analyst is still synonymous with a career in the IT industry, but the most successful and valuable analysts are those who understand the “business” rather than those who understand...
什么是神经网络进化__What_is_Neuro-Evolution_
本汇编程序显示what's your name?并输入你的名字,要不要循环
压缩包内的"Whatis an object.pdf"可能是关于面向对象编程的详细理论介绍,或者与项目中使用的特定对象模型相关的技术文档。而"下载.txt"可能是下载代码或额外资源的说明,包括如何获取和安装依赖项,以及如何配置...
格式PDF Written by Hal R. Varian University of California at Berkeley August, 1989 Keywords. methodology, economic theory, neoclassical economics
【接力版三年级下册小学英语 Lesson 6 "What colour is it?" 教案】主要围绕小学英语中的颜色和物品名称的教学展开,旨在帮助学生掌握基本的英语词汇和句型,提高他们的听说能力和语言运用能力。 一、教材内容分析 ...
1. 问题 "What is the tiger doing?" 要求学生回答老虎正在做什么。答案是 "The tiger is running.",这表明老虎正在奔跑。 2. 同样的结构用于其他动物,例如 "What is the elephant doing?" 回答是 "It’s ...
What is smali2java? Smali2java is an utility for converting .smali to .java files without bytecode compiling/decompiling. Why not to use dex2jar? I have never told about it. Dex2jar is a good utility...
本书详细介绍了数学的各个分支。对于有需要了解数学方法的程序员有很大的帮助。
What is a Java Virtual Machine?. 32 Developing software. 39 What is an integrated development environment?. 40 CHAPTER 3: Using the Basic Building Blocks. 43 Speaking the Java Language . 43 The ...
这篇文档是针对小学五年级英语课程的一课时教案,主题为“Lesson 6 What's an Insect?”。教案旨在帮助学生学习与昆虫相关的词汇、句型,并通过多种教学方法提高他们的听说读写能力。 首先,教学目标明确,学生需要...
36 What is deflation?
What? It’s a chair. It’s a chair. It’s a chair. Chair. Chair." 通过以上步骤,孩子们不仅学会了英语对话,还通过各种互动活动体验了英语学习的乐趣,为他们的英语基础打下了坚实的基础。此外,教师应时刻...
它们通常由疑问词开头,如when、who、whose、where、which、why、what、what time、what colour、what about、what day、what date、what for、how、how many、how much、how about、how far等。这些疑问词引导的...
接力版四年级下册小学英语 Lesson 6 What is it doing?说课稿.doc
Lesson 3 What time is it?教案(教学设计) 本教案旨在帮助四年级下册小学英语学生学习询问和回答时间的句型“What time is it? It’s …o’clock. It’s half past …”,并教学单词time、half、hour、late等。...
【接力版四年级下册小学英语 Lesson 6 "What is it doing?" 教案】主要围绕现在进行时态展开教学,旨在让学生掌握如何询问及回答动物正在进行的动作。本课时的教学内容丰富,包括词汇学习、句型训练以及多种形式的...
- **When**:询问时间,例如 "What time is it?" 或 "When is your birthday?" - **Who**:询问人物,例如 "Who is that man?" 或 "Who did you meet?" - **Whose**:询问所有者,例如 "Whose book is this?" 或 ...