Java的String对象

String类型:

String类型并不是基本类型,但是它常被误以为是一种基本类型。
String类型是Immutable的,一旦创建就不能够被改变。

不可改变的具体含义是指:
不能增加长度
不能减少长度
不能插入字符
不能删除字符
不能修改字符
一旦创建好这个字符串,里面的内容 永远 不能改变
String字符串的拼接:(+/+=)
字符串拼接实质是创建了新的字符串对象 ,原字符串对象没有发生改变
String字符串的替换:(replaceAll 替换所有 ,replaceFirst 只替换第一个)
这里的替换并非修改了原字符串,而是生成了新的字符串。

我的理解是,对String字符串对象的所有操作都不是更改对象本身,而是返回了新的字符串;就像replaceAll之前和之后不是一个对象了!
跑了网上网友给出的一个例子,运行之后秒懂:

String a = "ABC";
String anotherA = a.replaceAll("A", "B");
System.out.println(a);
System.out.println(anotherA);
System.out.println(a==anotherA);
StringBuffer b = new StringBuffer("ABC");
StringBuffer anotherB = b.replace(0, 1, "B");
System.out.println(b);
System.out.println(anotherB);
System.out.println(b==anotherB);

执行结果:

I/System.out: ABC
I/System.out: BBC
I/System.out: false
I/System.out: BBC
I/System.out: BBC
I/System.out: true

参考:
https://zhidao.baidu.com/question/272799186.html
http://how2j.cn/k/number-string/number-string-manipulate/325.html#step718

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值