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,每次修改都需要创建一个新对象,操作较慢。

相关推荐
Jelena157795857921 天前
电商运营分析数据比价接口实战:多平台价格监控与智能决策系统
java·大数据·数据库
神明不懂浪漫1 天前
【第五章】Java中的继承与多态
java·开发语言
AI多Agent协作实战派1 天前
AI多Agent协作系统实战(十七):凌晨4点,我的AI系统在“假装工作“——3个bug同时爆炸的5小时
java·前端·bug
gaolei_eit1 天前
Java+Ai+vue
java·spring·maven
qq_2518364571 天前
基于java Web 动漫视频网站毕业论文
java·开发语言·前端
LayZhangStrive1 天前
JUC相关的函数、注解、变量杂记
java·面试·多线程·juc
未秃头的程序猿1 天前
给公司做了个AI客服Agent,用的Spring AI 1.0,3天上线领导拍板了
java·后端·ai编程
曹牧1 天前
Eclipse 批量文本替换
java·ide·eclipse
程序员清风1 天前
OpenAI官方发布最新提示词技巧!
java·后端·面试
一只枫林1 天前
MySQL内、外连接知识点汇总
java·前端·数据库