Java - 创建字符串的方式

  1. 提取字符数组array中的一部分创建一个字符串对象。参数offst表示开始截取字符的位置,length表示截取字符的长度
    String a = new String(array,int offset,int length)
  2. 用一个字符数组创建一个字符串对象
  3. 引用字符串常量赋值给一个字符串变量
public class TextAboutBook {
    public static void main(String[] args) {
        char[] array = {'t','h','f'};
        String a = new String(array,0,3);//方式1
        String b = new String(array);//方式2
        String c = "thf";//方式3
        String d = "thf";
        System.out.println(a);//thf
        System.out.println(b);//thf
        System.out.println(c);//thf
        System.out.println(d);//thf
        System.out.println(c == d);//true c和d的内存地址都指向了 'thf'
        System.out.println(a == b);//false a和b开辟了不同的空间,内存地址不同
        System.out.println(a.equals(c));//true a和c的内容相同
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值