String

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

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

创建String对象的两种方法
java 复制代码
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 的常用方法
java 复制代码
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) 方法返回对象的哈希码,这个哈希码是基于对象的内存地址计算的(在对象的生命周期内是唯一的)。虽然这不是内存地址,但它可以作为对象唯一性的标识。

java 复制代码
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

    }
}
+=操作
java 复制代码
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,每次修改都需要创建一个新对象,操作较慢。

相关推荐
头发还在的女程序员2 小时前
Java充电桩平台源码解析:枪口状态、上报时间与页面刷新怎样协同
java·开发语言·充电桩系统·充电桩管理·充电系统·充电小程序
Cosolar5 小时前
从 Java 21 升到 25、Spring Boot 3 升 4:一次可复现的迁移踩坑实录
java·后端·面试
IT空门:门主7 小时前
LangChain4j 1.24.0 深度解读:新特性、版本对比与学习路线
java·学习
用户3721574261358 小时前
Java 批量操作 Excel 超链接:添加、读取、修改和删除
java
用户3721574261359 小时前
Java 将 Excel 图表导出为图片:指定图表、批量导出与图表工作表
java
这个昵称不容易10 小时前
交流论坛网站设计与实现:SpringBoot 论坛 130 个接口,附论文与运行截图
java·springboot·毕设
秋天的怀念_David11 小时前
算法一千零一夜 · 第三十七夜 · 类型的过路费
java·程序人生·算法
apcipot_rain11 小时前
C++转java日记(13)
java·开发语言
CoderYanger13 小时前
Java EE 进阶:12.Spring AOP(第二弹)
java·前端·数据库·spring boot·spring·java-ee·mybatis