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
|
相关推荐
(2) What is storage pool in IBM i? (3) What is shared pool? (4) Explain all types of shared pool? (5) How to configure a shared pool? (6) What is Private pool? (7) What is page fault? (8) What are the...
2014届高考英语一轮复习 课时作业(十七) 模块6 Unit 2 What is happiness to you? 译林牛津版
【学海导航】2014版高考英语一轮总复习 Module6 Unit2 What is happiness to you?同步训练 新人教版
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_
The Multi-Protocol Label Switching - Transport Profile (MPLS-TP) is the result of a joint effort by the Internet Engineering Task Force (IETF) and the International Telecommunication Union (ITU-T) ...
What is Nucleus C++ FILE? Nucleus C++ FILE is a C++ class interface into Nucleus FILE, a FAT16/FAT32 TM compatible file system specifically designed to work in embedded systems in conjunction with the...
有关遗传算法的论文,是英文的,可以作为翻译用
What bugs in the cloud?A Study of 3000+ Issues in Cloud Systems
本汇编程序显示what's your name?并输入你的名字,要不要循环
21. What is Jensen’s Inequality and what is its role in finance? 97 22. What is Itô’s lemma? 100 23. Why does risk-neutral valuation work? 103 24. What is Girsanov’s theorem and why is it ...
格式PDF Written by Hal R. Varian University of California at Berkeley August, 1989 Keywords. methodology, economic theory, neoclassical economics
33 What is green energy? 什么是绿色能源?.docx
Dart is a new language developed by Google that’s getting attention in web app circles.We asked Kathy Walrath and Seth Ladd, members of Google’s developer relations team,to explain Dart’s purpose ...
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...
代码文件过大,文档里给出代码网盘地址,在windows环境下已经编译好,运行demo.m文件即可,内附论文。
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 ...
what is seen and what is not seen 一部很不错的书。
what time is it?