# Java常用类之String **Repository Path**: fpfgitmy_admin/java-used-string-class ## Basic Information - **Project Name**: Java常用类之String - **Description**: 常用类String的简介 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-04-28 - **Last Updated**: 2021-04-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #### String 1. String:字符串,使用一对""引起来表示 2. String声明为final的,不可被继承 3. String实现了Serializable接口:表示字符串是支持序列化的 4. 实现了Comparable接口:表示String可以比较大小 5. String内部定义了final char[] value用于存储字符串数据 6. String:代表不可变的字符序列,简称:不可变性 1. 体现:当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的内存区域 2. 当对向有关的字符串进行连接操作时,也需要重新指定内存区域赋值 3. 当调用String的replace()方式修改字符或字符串时也需要重新指定内存区域赋值 7. 通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中。 8. 字符串常量池中是不会存储相同内容的字符串的,不同的值有不同的地址 ##### 不可变的字符序列代码演示 + 常量池中的字符是不可变的,每次都是创建新的字符 ``` package com.felixfei.study.common; /** * @describle String类的使用 *
*/ public class StringTest { public static void main(String[] args) { String s1 = "abc"; // 字面量的定义方式 String s2 = "abc"; System.out.println(s1 == s2); // 比较两个字符串的地址(方法区中含有字符串常量池(常量池中不会存储两个相同的字符串)) System.out.println(s1.equals(s2)); // 比较两个字符串的值 System.out.println("************"); String s3 = "abc"; s3 += "def"; System.out.println(s3); System.out.println(s2); System.out.println("************"); String s4 = "abc"; String s5 = s4.replace("a", "m"); System.out.println(s4); System.out.println(s5); } } ``` ##### String实例化的方式-1 ``` package com.felixfei.study.common; /** * @describle String类的使用 * String实例化的方式 * 方式一:通过字面量的方式 * 方式二:通过new+构造器的方式 */ public class StringTest { public static void main(String[] args) { //此时s1的数据声明在字符串的量池中 String s1 = "abc"; String s2 = "abc"; //通过 new +构造器的方式:此时s2的保存的地址值,是数据在堆空间中 String s3 = new String("abc"); String s4 = new String("abc"); System.out.println(s1 == s2); //true System.out.println(s1 == s3); //false System.out.println(s1 == s4); //false System.out.println(s3 == s4); //false } } ``` ##### String实例化方式-2 + 结论 1. 常量与倡廉的拼接结果在常量池。且常量池中不会存在相同内容的常量。 2. 只要其中有一个是变量,结果就在堆中。 3. 如果拼接的结果调用intern()方法,返回值就会被放到常量池中去。 ``` package com.felixfei.study.common; /** * @describle String类的使用 * String实例化的方式 * 方式一:通过字面量的方式 * 方式二:通过new+构造器的方式 * 面试题:String s = new String("abc");方式创建对象,在内存中创建了几个对象? * 两个:一个是对空间中new结构,另一个是char[]对应的常量池中的数据"abc" */ public class StringTest { public static void main(String[] args) { String s1 = "abc"; String s2 = "def"; String s3 = "abcdef"; String s4 = "abc" + "def"; String s5 = s1 + "def"; String s6 = "abc" + s2; String s7 = s1 + s2; System.out.println(s3 == s4); // true 比较地址,字面量定义:保存在常量池 System.out.println(s3 == s5); // false 比较地址,通过new对象定义,开辟了堆空间,值存放在对空间 System.out.println(s3 == s6); // false 比较地址,通过new对象定义,开辟了堆空间 System.out.println(s3 == s7); // false 比较地址,通过new对象定义,开辟了堆空间 System.out.println(s5 == s6); // false 比较地址,通过new对象定义,开辟了堆空间 System.out.println(s5 == s7); // false 比较地址,通过new对象定义,开辟了堆空间 String s8 = s5.intern(); // 返回值是常量池中的数据 System.out.println(s3 == s8); // true } } ``` ##### String的一道面试题-1 ``` package com.felixfei.study.common; /** * @describle String类的使用 * String的一道面试题 */ public class StringTest { String str = new String("good"); char[] ch = { 't', 'e', 's', 't' }; // String的不可变性,String传地址 public void change(String str, char[] ch) { str = "test ok"; ch[0] = 'b'; } public static void main(String[] args) { StringTest ex = new StringTest(); ex.change(ex.str, ex.ch); System.out.println(ex.str); System.out.println(ex.ch); } } ``` ##### String的面试题-2 ``` package com.felixfei.study.common; /** * @describle String类的使用 * String的一道面试题 */ public class StringTest { public static void main(String[] args) { String s1 = "abc"; String s2 = "def"; String s3 = "abcdef"; String s4 = s1 + "def"; // 对象和常量拼接,结果在堆中 System.out.println(s3 == s4); // false final String s5 = "def"; // 原因,被final修饰的为常量,常量相拼接,结果在常量池中 System.out.println(s3 == s5); // true } } ```