广州大石附近做网站的公司,电商平台推广员是做什么的,哈尔滨 建网站,二级域名免费发放个人简介 #x1f440;个人主页#xff1a; 前端杂货铺 ⚡开源项目#xff1a; rich-vue3 #xff08;基于 Vue3 TS Pinia Element Plus Spring全家桶 MySQL#xff09; #x1f64b;♂️学习方向#xff1a; 主攻前端方向#xff0c;正逐渐往全干发展 #x1…个人简介 个人主页 前端杂货铺 ⚡开源项目 rich-vue3 基于 Vue3 TS Pinia Element Plus Spring全家桶 MySQL ♂️学习方向 主攻前端方向正逐渐往全干发展 个人状态 研发工程师现效力于中国工业软件事业 人生格言 积跬步至千里积小流成江海 推荐学习开源 rich-vue3 前端面试宝典 Vue2 Vue3 Vue2/3项目实战 Node.js实战 Three.js 个人推广每篇文章最下方都有加入方式旨在交流学习资源分享快加入进来吧 内容参考链接Java基础一Hello World8种数据类型键盘录入Java基础二数组方法方法重载Java基础三类和对象、构造方法 文章目录 前言String 字符串StringBufferStringBuilderStringJoiner总结 前言
大家好这里是前端杂货铺。
本篇文章我们认识字符串。 String 字符串
通过 new 创建的都是存储在堆内存中所以 s1 和 s2 的地址并不相同故不相等。
通过 equals 方法我们可以只比较值而不比较地址通过 equalsIgnoreCase 方法我们可以忽略字符串大小写比较值。
public static void main(String[] args) {String s1 new String(Abc);String s2 Abc;// 基本数据类型比较数据值// 引用数据类型比较地址值System.out.println(s1 s2); // false// 比较对象的内容是否相等System.out.println(s1.equals(s2)); // false// 比较对象的内容是否相等忽略大小写System.out.println(s1.equalsIgnoreCase(s2)); // true
}StringBuffer
在使用 StringBuffer 类时每次都会对 StringBuffer 对象本身进行操作而不是生成新的对象所以如果需要对字符串进行修改推荐使用 StringBuffer。
StringBuilder 类在 Java 5 中被提出它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的不能同步访问。
由于 StringBuilder 相较于 StringBuffer 有速度优势所以多数情况下建议使用 StringBuilder 类。
但是在应用程序要求线程安全的情况下则必须使用 StringBuffer 类。
方法描述append(String s)将指定的字符串追加到此字符序列insert(int offset, String str)将字符串插入此序列中delete(int start, int end)移除此序列的子字符串中的字符replace(int start, int end, String str)使用给定 String 中的字符替换此序列的子字符串中的字符reverse()将此字符序列用其反转形式取代
StringBuffer sBuffer new StringBuffer(hello);
sBuffer.append( );
sBuffer.append(world);
sBuffer.append(!);System.out.println(sBuffer);sBuffer.insert(5, Java);
System.out.println(sBuffer); // helloJava world!sBuffer.delete(7, 9);
System.out.println(sBuffer); // helloJa world!sBuffer.replace(5, 7, ab);
System.out.println(sBuffer); // helloab world!sBuffer.reverse();
System.out.println(sBuffer); // !dlrow baollehSystem.out.println(sBuffer.length()); // 14StringBuilder
StringBuilder 可以看成是一个容器创建之后里面的内容是可变的。StringBuilder 类的对象能够被多次的修改并且不产生新的未使用对象。
方法描述append(String s)将指定的字符串追加到此字符序列insert(int offset, String str)将字符串插入此序列中delete(int start, int end)移除此序列的子字符串中的字符replace(int start, int end, String str)使用给定 String 中的字符替换此序列的子字符串中的字符reverse()将此字符序列用其反转形式取代
StringBuilder sb new StringBuilder(hello);sb.append( ).append(world).append(!);
System.out.println(sb); // hello world!sb.insert(5, Java);
System.out.println(sb); // helloJava world!sb.delete(7, 9);
System.out.println(sb); // helloJa world!sb.replace(5, 7, ab);
System.out.println(sb); // helloab world!sb.reverse();
System.out.println(sb); // !dlrow baollehSystem.out.println(sb.length()); // 14StringJoiner
JDK8 出现的一个可变的操作字符串的容器可以高效方便的拼接字符串。
StringJoiner sj new StringJoiner(---);
sj.add(aaa).add(bbb).add(ccc);
System.out.println(sj);StringJoiner sj2 new StringJoiner(, , [, ]);
sj2.add(aaa).add(bbb).add(ccc);
int len sj2.length();
System.out.println(len);
System.out.println(sj2);// 转为字符串
String str sj2.toString();
System.out.println(str);总结
本篇文章我们学习了字符串地址和值的比较、StringBuffer、StringBuilder、StringJoiner认识了其各自的作用及StringBuffer 与 StringBuilder 的区别等…
好啦本篇文章到这里就要和大家说再见啦祝你这篇文章阅读愉快你下篇文章的阅读愉快留着我下篇文章再祝 参考资料 Java 基础bilibili-黑马程序员菜鸟教程–Java 文章转载自: http://www.morning.mbmtz.cn.gov.cn.mbmtz.cn http://www.morning.mfzyn.cn.gov.cn.mfzyn.cn http://www.morning.plpqf.cn.gov.cn.plpqf.cn http://www.morning.hmtft.cn.gov.cn.hmtft.cn http://www.morning.wkcl.cn.gov.cn.wkcl.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.ltpmy.cn.gov.cn.ltpmy.cn http://www.morning.rnrwq.cn.gov.cn.rnrwq.cn http://www.morning.zdmrf.cn.gov.cn.zdmrf.cn http://www.morning.mnjwj.cn.gov.cn.mnjwj.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.rkwlg.cn.gov.cn.rkwlg.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.pxmyw.cn.gov.cn.pxmyw.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.pbwcq.cn.gov.cn.pbwcq.cn http://www.morning.dnmgr.cn.gov.cn.dnmgr.cn http://www.morning.pqnkg.cn.gov.cn.pqnkg.cn http://www.morning.nuobeiergw.cn.gov.cn.nuobeiergw.cn http://www.morning.lstmg.cn.gov.cn.lstmg.cn http://www.morning.qbzdj.cn.gov.cn.qbzdj.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.rgxll.cn.gov.cn.rgxll.cn http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.gfprf.cn.gov.cn.gfprf.cn http://www.morning.lynb.cn.gov.cn.lynb.cn http://www.morning.trfrl.cn.gov.cn.trfrl.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.cbnlg.cn.gov.cn.cbnlg.cn http://www.morning.rgmls.cn.gov.cn.rgmls.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn http://www.morning.bmtkp.cn.gov.cn.bmtkp.cn http://www.morning.gllgf.cn.gov.cn.gllgf.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.syqtt.cn.gov.cn.syqtt.cn http://www.morning.psxxp.cn.gov.cn.psxxp.cn http://www.morning.mzqhb.cn.gov.cn.mzqhb.cn http://www.morning.qnywy.cn.gov.cn.qnywy.cn http://www.morning.gsjw.cn.gov.cn.gsjw.cn http://www.morning.mdpcz.cn.gov.cn.mdpcz.cn http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.qwmsq.cn.gov.cn.qwmsq.cn http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.nzkc.cn.gov.cn.nzkc.cn http://www.morning.dfkby.cn.gov.cn.dfkby.cn http://www.morning.rytps.cn.gov.cn.rytps.cn http://www.morning.hxlpm.cn.gov.cn.hxlpm.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.cfnht.cn.gov.cn.cfnht.cn http://www.morning.bzjpn.cn.gov.cn.bzjpn.cn http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.ctlbf.cn.gov.cn.ctlbf.cn http://www.morning.yrddl.cn.gov.cn.yrddl.cn http://www.morning.fkmqg.cn.gov.cn.fkmqg.cn http://www.morning.dmfdl.cn.gov.cn.dmfdl.cn http://www.morning.bnmrp.cn.gov.cn.bnmrp.cn http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn http://www.morning.fnnkl.cn.gov.cn.fnnkl.cn http://www.morning.fksxs.cn.gov.cn.fksxs.cn http://www.morning.nzms.cn.gov.cn.nzms.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.zfzgp.cn.gov.cn.zfzgp.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.kggxj.cn.gov.cn.kggxj.cn http://www.morning.rlbg.cn.gov.cn.rlbg.cn http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.bylzr.cn.gov.cn.bylzr.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.yqtry.cn.gov.cn.yqtry.cn