Java常用类-String类
2021-04-17 13:27
                         标签:同步   结果   mamicode   接口   enc   序列化   可变   strong   不包含    字符串相关的类:String String类:代表字符串,Java 程序中的所有字符串字面值(如 "abc" )都作 为此类的实例实现。 注:两个对象里面还是字面量定义的方式赋值 面试题 两个,一个是堆空间的new的结构,一个是char[]对应的常量池中的数据 "abc" 重点: 特别注意:当 final String s1 = “java”,这个也就相当于一个常量了 good and best 这里可以参考之前的Java的值传递 若在scr前面加一个this,情况会是怎样? 注意String的不可变性,原字符不变 String ---> 基本数据类型、包装类 基本数据类型、包装类 --- >字符串 String ---> char[] : 调用String的toCharArray() char[] ---> String: 调用String的构造器 编码:String ---> byte[] :调用String的getBytes() 面试题:对比String、StringBuffer、StringBuilder ? 注意:作为参数传递的话,方法内部String不会改变其值,StringBuffer和StringBuilder 会改变其值 问题1: 注意这个是里面 里面数据的长度, 问题2: 扩容问题,如果要添加的数据放不下了,那就需要扩容数组,默认情况下,扩容为原来的2倍+2,将原有数组中的数据复制到新的数组中去 String -----> StringBuffer、StringBuilder : 调用StringBuffer、StringBuilder构造器就可以了 StringBuffer、StringBuilder -----> : 调用String的构造器 StringBuffer类的常用方法和StringBuilder类似 总结: 面试题: Java常用类-String类 标签:同步   结果   mamicode   接口   enc   序列化   可变   strong   不包含    原文地址:https://www.cnblogs.com/mengd/p/13301810.html
public void test1(){
        String s1 = "abc";//字面量的方式
        String s2 = "abc";
        System.out.println(s1 == s2);// ture , 比较的是s1和s2的地址值
        s1 = "hello";
        System.out.println(s1); // hello
        System.out.println(s2); // abc
        String s3 = "abc";
        s3 += "def";
        System.out.println(s3); // abcdef
        System.out.println(s2 == s3); // false , 此时的s2和s3的地址值已经不同
        String s4 = "abc";
        String s5 = s4.replace(‘a‘ , ‘d‘);
        System.out.println(s4); // abc
        System.out.println(s5); // dbc
    }
1. String的实例化方式
  public void test2(){
        String s1 = "Java";
        String s2 = "Java";
        String s3 = new String("Java");
        String s4 = new String("Java");
        System.out.println(s1 == s2); // true
        System.out.println(s1 == s3); // false
        System.out.println(s1 == s4); // false
        System.out.println(s3 == s4); // false
        Person p1 = new Person("Tom",18);
        Person p2 = new Person("Tom",18);
        System.out.println(p1.name.equals(p2.name)); // true , String重写了equals方法,此时比较的就是内容了
        System.out.println(p1.name == p2.name); // true , 两个对象里面还是字面量定义的方式赋值
    }


String s = new String("abc")方式创建对象,在内存中创建了几个对象?2. 字符串的特性

public void test3(){
        String s1 = "Java";
        String s2 = "Python";
        String s3 = "JavaPython";
        String s4 = "Java" + "Python";
        String s5 = s1 + "Python";
        String s6 = s1 + s2;
        System.out.println(s3 == s4); // true
        System.out.println(s3 == s5); // false
        System.out.println(s4 == s5); // false
        System.out.println(s3 == s6); // false
        String s7 = s5.intern();
        System.out.println(s7 == s3); //true
    
    }
3. 面试题:

package com.atguigui.exer;
/**
 * @author MD
 * @create 2020-07-12 9:41
 */
public class StringTest {
    String str = new String("good");
    char[] ch = {‘t‘,‘e‘,‘s‘,‘t‘};
    public void change(String str, char ch[]){
        this.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);
        // test ok
        // best
        
    }
}
4. String常用的方法
5. String与基本数据类型转换
public void test2(){
        String str = "123";
        
        int num = Integer.parseInt(str);
        
        String str1 = String.valueOf(num);
    }
public void test3(){
        String str = "asdf123";
        char[] charArray = str.toCharArray();
        for (int i = 0; i 
解码:byte[] ---> String : 调用String的构造器/**
     * 编码:字符串 -->字节 (看得懂的---->看不懂的二进制数据)
     * 解码:字节 ---> 字符串(看不懂的二进制数据---->看得懂的)
     * @throws UnsupportedEncodingException
     */
    @Test
    public void test4() throws UnsupportedEncodingException {
        String str = "abc123你好";
        byte[] bytes = str.getBytes(); // 使用默认的字符集进行转换 UTF-8 编码
        System.out.println(Arrays.toString(bytes));
        String str1 = "abc123你好";
        byte[] bytes1 = str1.getBytes("gbk"); // 指定编码格式
        System.out.println(Arrays.toString(bytes1));
        System.out.println("---------------------");
        String str2 = new String(bytes); // 解码,使用的默认的格式
        System.out.println(str2);
        String str3 = new String(bytes1 , "gbk");// 解码,使用的指定的格式
        System.out.println(str3);
    }
6. StringBuffer、StringBuilder ?
源码分析:
String str = new String(); // char[] value = new char[0];
String str1 = new String("abc"); // char[] value = new char[]{‘a‘,‘b‘,‘c‘};
StringBuffer sb1 = new StringBuffer(); // char[] value = new char[16];底层创建一个长度16的char型数组
StringBuffer sb1 = new StringBuffer("abc"); // char[] value = new char["abc".length()+16];
public void test5(){
        StringBuffer sb1 = new StringBuffer();
        System.out.println(sb1.length()); // 0
        StringBuilder sb2 = new StringBuilder("abc");
        System.out.println(sb2.length()); // 3
    }
 public void testStringBuffer(){
        String str = null;
        StringBuffer sb = new StringBuffer();
        sb.append(str);
        System.out.println(sb.length()); // 4
        System.out.println(sb); //  "null" 这个是字符串null
        StringBuffer sb1 = new StringBuffer(str); // 抛异常 NullPointerException
        System.out.println(sb1);
    }