java 19:不可变字符串和限定字符串(Immutable String and Interned String)

1 String 的构造

Astringis a sequence of characters. In many languages, strings are treated as an array of characters, but in Java a string is an object. The Stringclass has 11 constructors and more than
40 methods for manipulating strings. Not only is it very useful in programming, but also it is
a good example for learning classes and objects.

You can create a string object from a string literal or from an array of characters. To create a
string from a string literal, use a syntax like this one:
String newString = newString(stringLiteral);

You can also create a string from an array of characters. For example, the following statements create the string “Good Day”:
char[] charArray = {'G','o','o','d',' ','D','a','y'};
String message = newString(charArray)

一个String就是字符系列,在其他语言中,String是以字符数组形式存在, 但java中存在String类,他有11种不同的构造方式,同时有40多种方法,是java中非常重要的一个类。

我们可以通过 String newString = newString(stringLiteral);方式构造例如 String hello=new String("welcome to java");并且java中提供了简单的记法,它等同于 String hello="welcome to java"; 另外,我们也可以将一个字符数组构造为一个String类。例如

char []  charArray={'g','o','o','d',' ','d','a','y'};

String  goodDay=new String(charArray);

其他的构造方法可以参考API文档。

2 不可变字符串

AStringobject is immutable; its contents cannot be changed.Does the following code
change the contents of the string?
String s = "Java";
s = "HTML";
The answer is no. The first statement creates a Stringobject with the content “Java” and
assigns its reference to s. The second statement creates a new Stringobject with the content “HTML” and assigns its reference to s. The first Stringobject still exists after the
assignment, but it can no longer be accessed, because variable snow points to the new
object

一个String 对象的内容是不可变,当你对String 进行修改, 其实他是创建了新的字符串对象,然后将它的引用赋值给了原来的引用变量,这样原来的对象就再也不能访问了,因为他之前的引用已经弃他而去指向了新的字符串对象。他只能等着JVM的自动回收。

例如 String s="java";

        s="hello"; 

这样之后,执行第二句语句时候,并不是去修改了"java"成了“hello”,而是新创建了一个"hello", 也就是在内存中的另一个位置分配了空间,然后s原本是指向了“java”的内存空间,现在他指向了新欢 "hello",而"java"因为已经没有引用指向他所以访问不到,等着被JVM回收。

3 限定字符串

因为String的不可变性,而他又是如此频繁的出现在我们的程序中,素以JVM通融了一下,相同字符串内容的字符串直接量(字面量 literal)使用同一个实例。这就是叫限定字符串。注意这里说的字面量literal(定义:  a literal is a contant value that appears directly in the program),也就是直接在程序中出现的常量。

Since strings are immutable and are ubiquitous in programming, the JVM uses a unique
instance for string literals with the same character sequence in order to improve efficiency and
save memory. Such an instance is called interned.

例如:

public class ImString
{
	public static void main(String [] args)
	{
		String  s1="welcome to java";
		String s2=new String("welcome to java");
		String s3="welcome to java";
		System.out.println(s1==s2);
		System.out.println(s1==s3);
		System.out.println(s2==s3);
		System.out.println(s1.equals(s2));
		System.out.println(s1.equals(s3));
		System.out.println(s2.equals(s3));
		
	}
}

因为这里 s1 与s3都是用了同一个字面量常量创建的,在创建s3时候会搜到该字面量已经创建过对象,所以会让s3也指向s1指向的对象。但是s2创建的方式跟s1都不一样,他们指向不同的对象,尽管他们的content相同。

这里顺便说下 == 与equals的区别,很多人都不知道怎么分辨他们,但按照我的理解比较好分辨。你就想想我们在其他语言等等中都是用 = =来比较两个量是否相等,但几乎都是用在两个int型,bool型等这样的基本数据类型,他们比较的就是变量,所以当到了引用类型时候,= =依旧是用来比较这两个变量,但这种情况下他是引用变量,所以比较的是两个引用变量,也就是比较两个变量是否指向同一个东西(对象),而在之前语言我们比较两个字符串是否相同我们需要一个循环,所以以前不会有equal函数,所以在java中出现的当然是比较我们之前不能做到的比较,也就是比较content了。所以,很好记,就是还是记住像其他语言一样, == 用来比较两个变量,只是对于引用变量类型,变量是引用,那就是比较两个变量是否为同一个引用,也就是是否指向同一个对象。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值