Java中String类型的不可变性和驻留池
一 基本概念
可变类和不可变类(Mutable and Immutable Objects)的初步定义:
可变类:当获得这个类的一个实例引用时,可以改变这个实例的内容。
不可变类:不可变类的实例一但创建,其内在成员变量的值就不能被修改。其中String类就是不可变类的经典应用。
二 例子
package cn.xy.test;
public class StringTest
{
/**
* a的值在编译时就被确定下来,故其值"xy"被放入String的驻留池(驻留池在堆中)并被a指向。
* b的值在编译时也被确定,那么b的值在String的驻留池中找是否有等于"xy"的值,有的话也被b指向。
* 故两个对象地址一致
* @return true
*/
public static Boolean testString1()
{
String a = "xy";
String b = "xy";
return a == b;
}
/**
* b的值在是两个常量相加,编译时也被确定。
* @return true
*/
public static Boolean testString2()
{
String a = "xyy";
String b = "xy" + "y";
return a == b;
}
/**
* b的值为一个变量和一个常量相加,无法编译时被确定,而是会在堆里新生成一个值为"abc"的对象
* @return false
*/
public static Boolean testString3()
{
String a = "xyy";
String b = "xy";
b = b + "y";
return a == b;
}
/**
* b的值都无法编译时被确定,而是会在堆里分别新生成一个对象叫"xyy"。
* @return false
*/
public static Boolean testString4()
{
String a = "xyy";
String b = "xy".concat("y");
return a == b;
}
/**
* new String()创建的字符串不是常量,不能在编译期就确定,所以new String() 创建的字符串不放入常量池中,它们有自己的地址空间。
* a,b的值都无法编译时被确定,会在堆里分别新生成一个值为"xy"的对象。
* @return fasle
*/
public static Boolean testString5()
{
String a = new String("xy");
String b = new String("xy");
return a == b;
}
/**
* intern()把驻留池中"xy"的引用赋给b。
* @return true
*/
public static Boolean testString6()
{
String a = "xy";
String b = new String("xy");
b = b.intern();
return a == b.intern();
}
/**
* char的toString方法返回的是一个char对象的字符串,而不是我们想象的"xy"
* @return false
*/
public static Boolean testString7()
{
String b = "xy";
char[] a = new char[]{'x','y'};
return a.toString().equals(b);
}
/**
* char是一种新的类型,不存在驻留池的概念。
* @return fasle
*/
public static Boolean testString8()
{
String b = "xy";
char[] a = new char[]{'x','y'};
return a.toString() == b;
}
/**
* String不可变性的体现
*/
String str = "xy";
public String chage(String str)
{
str = "xyy";
return str;
}
/**
* 一般引用类型的可变性(传值的时候把地址传过去,相当于把仓库的要是交给方法,方法拿到钥匙去移动仓库里的东西)
*/
Person p = new Person("xy");
public String changePerson(Person p)
{
p.setName("xyy");
return p.toString();
}
public static void main(String[] args)
{
print(testString1()); // true
print(testString2()); // true
print(testString3()); // fasle
print(testString4()); // false
print(testString5()); // false
print(testString6()); // true
print(testString7()); // false
print(testString8()); // false
StringTest t = new StringTest();
print(t.str); // xy
print(t.chage(t.str)); // xxy
print(t.str); // xy
print(t.p.toString()); //xy
print(t.changePerson(t.p)); //xyy
print(t.p.toString()); //xyy
}
public static void print(Object o)
{
System.out.println(o);
}
}
Java中String类型的不可变性和驻留池的更多相关文章
- Java中String对象的不可变性
首先看一个程序 package reverse; public class Reverse { public static void main(String[] args) { String c1=n ...
- Java中String类型细节
Java中String类型细节 一 . String两种初始化方式 1 . String str1= “abc”;//String类特有的创建字符对象的方式,更高效 在字符串缓冲区中检测”abc”是否 ...
- Java中String类型详解
这篇博客是我一直想总结的,这两天一直比较忙,先上传下照片吧,过后有时间再弄成正常的. 本文主要是对Java中String类型的总结,包括其在JVM中是怎么存储的...
- 【转载】 Java中String类型的两种创建方式
本文转载自 https://www.cnblogs.com/fguozhu/articles/2661055.html Java中String是一个特殊的包装类数据有两种创建形式: String s ...
- 关于JAVA中String类型的最大长度
前些天看到一道面试题,题目很容易理解:String的长度限制是多少? 针对这个题目,浏览了网友的回答,大概得到了3个层次的答案. 最浅的层次: 近似计算机内存大小的长度.这是作为一个程序员最浅显的回答 ...
- Java中String类型的数据比较
在Java中如果想比较两个字符串是否相等,可以使用string1==string2 或string1.equal(string2)来比较. 但是,第一种方法过于局限.例如, String string ...
- java中String类型的相关知识
String类方法整理说明: ·Length()用来求字符串的长度,返回值为字符串的长度: ·charAt()取该字符串某个位置的字符,从0开始,为char类型: ·getChars()将这个字符串中 ...
- java中String类型
string对象常用方法 string对象比较方法: string类获取包含子串的方法: 字符串和数字的转换: String类 String对象是不可改变的,字符串一旦创建,内容不能再改变. 构造字符 ...
- Java中String类型的部分用法
1.如何将字符串转换为整型数值? int i = Integer.parseInt("20"); 2.如何用“==”还是equals比较两个字符串? “==”是用来比较俩引用是不是 ...
随机推荐
- Oracle—用户管理的完全恢复(四)
在用户管理的备份(三)中,最后打开数据库时,用了alter database open resetlogs;的命令,这里为什么用resetlogs命令? 一.resetlogs的作用 1.将当前的日志 ...
- vim的列编辑操作
转载:http://www.cnblogs.com/xiaowant/articles/1992923.html 删除列 1.光标定位到要操作的地方. 2.CTRL+v 进入“可视 块”模式,选取这一 ...
- 使用Thread类可以创建和控制线程
1.创建线程 static void Main(string[] args) { /* Thread类 * 创建控制线程 * 其构造函数接受ThreadStart和ParameterizedThrea ...
- Linux下redis安装与使用
redis官网地址:http://www.redis.io/ 最新版本:2.8.3 在Linux下安装Redis非常简单,具体步骤如下(官网有说明): 1.下载 ...
- (六)u-boot2013.01.01 for TQ210:《精简u-boot文件目录,定制自己的目标板》
1. 删改U-boot代码结构 把不用到的和与我们s5pv210移植无关的硬件平台代码统统删除,眼不见为净.这样代码看起来就干净利落多了. 1.1.进入arch目录,删掉除arm以外的目录 处理前: ...
- 在CentOS下安装配置MySQL
经常需要在linux环境下部署项目或安装Mysql数据库,由于记性不好,每次都是求助度娘,每次搜到的步骤都不一样,所以每次都在尝试.冒险:于是乎,把安装的重要步骤贴出,供自己以后参照. 1.首先要看看 ...
- 接入淘宝API(PHP版本)
本文链接! http://www.cnblogs.com/MicroHao/p/4030117.html 遇到的问题一: $req = new WaimaiOrderIndexGetRequest; ...
- ReactNative学习-ListView
ListView相对于View的优点就在于可以不用一下子就把数据加载完,而是滑动着加载着数据,可以缓解数据加载,避免软件卡死. 官方文档:https://facebook.github.io/reac ...
- highchairts柱状图显示数值并且带单位
$(target).highcharts({ chart: { type: 'bar' }, colors: [ "#1ab394" ], title: { text: title ...
- [转载]JSON序列化与反序列化
转载:http://www.cnblogs.com/ejiyuan/archive/2010/04/09/1708084.html 方法一:引入System.Web.Script.Serializat ...