一 基本概念

可变类和不可变类(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类型的不可变性和驻留池的更多相关文章

  1. Java中String对象的不可变性

    首先看一个程序 package reverse; public class Reverse { public static void main(String[] args) { String c1=n ...

  2. Java中String类型细节

    Java中String类型细节 一 . String两种初始化方式 1 . String str1= “abc”;//String类特有的创建字符对象的方式,更高效 在字符串缓冲区中检测”abc”是否 ...

  3. Java中String类型详解

    这篇博客是我一直想总结的,这两天一直比较忙,先上传下照片吧,过后有时间再弄成正常的. 本文主要是对Java中String类型的总结,包括其在JVM中是怎么存储的...

  4. 【转载】 Java中String类型的两种创建方式

    本文转载自 https://www.cnblogs.com/fguozhu/articles/2661055.html Java中String是一个特殊的包装类数据有两种创建形式: String s ...

  5. 关于JAVA中String类型的最大长度

    前些天看到一道面试题,题目很容易理解:String的长度限制是多少? 针对这个题目,浏览了网友的回答,大概得到了3个层次的答案. 最浅的层次: 近似计算机内存大小的长度.这是作为一个程序员最浅显的回答 ...

  6. Java中String类型的数据比较

    在Java中如果想比较两个字符串是否相等,可以使用string1==string2 或string1.equal(string2)来比较. 但是,第一种方法过于局限.例如, String string ...

  7. java中String类型的相关知识

    String类方法整理说明: ·Length()用来求字符串的长度,返回值为字符串的长度: ·charAt()取该字符串某个位置的字符,从0开始,为char类型: ·getChars()将这个字符串中 ...

  8. java中String类型

    string对象常用方法 string对象比较方法: string类获取包含子串的方法: 字符串和数字的转换: String类 String对象是不可改变的,字符串一旦创建,内容不能再改变. 构造字符 ...

  9. Java中String类型的部分用法

    1.如何将字符串转换为整型数值? int i = Integer.parseInt("20"); 2.如何用“==”还是equals比较两个字符串? “==”是用来比较俩引用是不是 ...

随机推荐

  1. WIN7 下 Qt Creator 安装 QWT

    WIN7 下 Qt Creator 安装 QWT 环境:WIN7 +QT Creator2.6.2 1.下载QWT源代码 qwt-6.1-rc3.zip 2 编译QWT  open projects- ...

  2. Java再学习——sleep(), wait(), notify(), notifyAll()

    首先一点就是Thread.sleep(long millis)方法是Thread类的静态方法,其他三个wait(), notify()和notifyAll()都是Object类的方法. sleep(l ...

  3. Javascript教程:AngularJS的五个超酷特性

    AngularJS是一个超棒的javascript框架,不单单对于开发人员来说非常有吸引力,对于UI设计师来说也同样出色.在这篇教程中,我们将简单的介绍AngularJS几个重量级必备特性,并且介绍它 ...

  4. eclipse[downloads]

    下载J2EE:http://www.eclipse.org/downloads/ 下载WPT插件:http://download.eclipse.org/webtools/updates 下载TOMC ...

  5. cocos2dx3.0-tinyxml在Android环境下解析xml失败的问题

    本文由@呆代待殆原创,转载请注明出处. 正常情况下,我们在用tinyxml读取xml文件的的时候,会像下面这样写. std::string filePath = FileUtils::getInsta ...

  6. recent.css常用的页面初始化样式

    <style> @charset "utf-8"; body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form ...

  7. Nodejs新建博客练习(一)安装express并新建项目

    安装express npm install -g express-generator 新建工程 express blog //新建项目 cd blog && npm install / ...

  8. Redis缓存异常的容错实现方法( .net)

    using DotNet.Log; /// <summary> /// Redis缓存辅助类 /// /// 修改纪录 /// /// 2015-10-26 版本:1.0 SongBiao ...

  9. uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列

    题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...

  10. eclipse启动问题

    今天在公司正上班,突然跳出来一个windows update补丁更新,然后就确认呗,结果更新完成之后, eclipse打不开了,启动报错: could not find the main class, ...