String

在 Java 中,String 是一种特殊的类,用于表示和操作字符串。String 对象在内存中的存储方式有其独特之处。

String 是不可变的对象,一旦创建,字符串内容不能被改变。它主要用于表示不可变的字符序列。

创建String对象的两种方法
public class HelloWorld {
    public static void main(String[] args) {

       String s = "abc";//直接赋值
       
       //通过构造器
        String s1 = new String();

       String s2 = new String("abc");
       
       char[] ch={'a','b','c'};
       String s3 = new String(ch);
        System.out.println(s3);//abc

        byte[] byt={97, 98, 99};
        String s4 = new String(byt);
        System.out.println(s4);//abc

    }
}

String 的常用方法

public class HelloWorld {
    public static void main(String[] args) {

       String s = "found";
       System.out.println(s.length());//5,字符串的长度

        for (int i = 0; i < s.length(); i++) {
            System.out.print(s.charAt(i));
        }
        System.out.println();//found

        char[] ch = s.toCharArray();//将字符串转为字符数组
        for (int i = 0; i < ch.length; i++) {
            System.out.println(ch[i]);
        }//f o u n d

        String s1 = new String("found");
        String s2 = new String("found");
        System.out.println(s1 == s2);//false
        //直接用==比较,比较的是s1,s2的地址
        System.out.println(s1.equals(s2));//true

        String s3 = "FOUNd";
        System.out.println(s1.equalsIgnoreCase(s3));//true
        //忽略大小写进行比较

        System.out.println(s1.substring(2,3));//u
        //输出下标从2,到3,但不包含3的子串,不得超过字符串长度
        System.out.println(s1.substring(2));//und
        //输出下标从2开始的字串

        System.out.println(s1.replace("ou", "**"));//f**nd
        //用字符串”**“,取代"ou"

        System.out.println(s.contains("oun"));//true
        //查询字符串中是否含有子串"oun"

        System.out.println(s.startsWith("fou"));//true
        //判断字符串是否以字符串"fou"开头

        String s4 = "fo,un,d";
        String[] s5 = s4.split(",");
        for (int i = 0; i < s5.length; i++) {
            System.out.print(s5[i]);
        }//found
        //s4.split("s") 以字符串s对s4进行分割,返回字符串数组
    }
}
注意事项

1.以"…"形式创建的String对象,会存储到字符串常量池(在堆内),且相同内容的字符串只存储一份。

2.通过new方式创建的字符串对象,每new一次都会产生一个新的对象放在堆内存中。

在 Java 中,直接获取对象的内存地址是不被支持的,因为 Java 的内存管理和垃圾收集机制屏蔽了这些底层细节。

System.identityHashCode(Object x) 方法返回对象的哈希码,这个哈希码是基于对象的内存地址计算的(在对象的生命周期内是唯一的)。虽然这不是内存地址,但它可以作为对象唯一性的标识。

public class HelloWorld {
    public static void main(String[] args) {

        String s1 = "abc";
        String s2 = "abc";
        System.out.println(s1 == s2);//true
        System.out.println(System.identityHashCode(s1));//1324119927
        System.out.println(System.identityHashCode(s2));//1324119927
        //s1==s2比较的是两对象的地址是否相同

        String s3 = new String("abc");
        String s4 = new String("abc");
        System.out.println(s3 == s4);//false
        System.out.println(System.identityHashCode(s3));//990368553
        System.out.println(System.identityHashCode(s4));//1096979270

    }
}
+=操作
public class HelloWorld {
    public static void main(String[] args) {

        String s1 = "abc";//
        System.out.println(System.identityHashCode(s1));//1324119927
        s1 += 's';
        System.out.println(System.identityHashCode(s1));//1747585824
        s1 += "1223";
        System.out.println(System.identityHashCode(s1));//102389292
        System.out.println(s1);//abcs1223
      
    }
}

每次执行+=操作都会创建一个新对象,并不是在原有对象的基础上进行修改,即不可变的字符串类型

思考: 由于不可修改性使得 String s1 = new String("found");的意义并不大,不如直接 String s = "found";将字符串放在字符串池(String Pool)中,可以节省内存。很明显,对字符串修改操作比较多的代码不建议使用String,每次修改都需要创建一个新对象,操作较慢。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值