企业真题

以下两种方式创建的 String 对象有什么不同?

来源:?团

String str1 = new String("test");  

String str2 = "test";

String 的内存结构

String s = new String("xyz"); 创建了几个 String Object?

来源:新?陆

两个,一个在堆上、一个在字符串常量区

String a="abc";String b="a"+"bc"a==b?

来源:网?邮箱

是!

String 中 “+” 怎样实现?

来源:阿??巴

常量 + 常量:编译时优化成字面量

变量 + 常量 、变量+变量:创建一个 StringBuilder 的实例,通过 append() 添加字符串,最后调用 toString() 返回一个字符串。(toString() 内部 new 一个 String 的实例)

String 是否可以被继承?

来源:凡?科技、网?邮箱、湖南?利软件、阿??巴

String 类是 final 的,不能被继承

String 为啥不可变,在内存中的具体形态?

来源:阿??巴

规定不可变。提供字符串常量池。

String 可以在 switch 中使用吗?

来源:上海?睿

可以。从 JDK7 开始可以使用

String 中有哪些方法?列举几个

来源:闪?购

String 的常用 API

subString() 到底做了什么?

来源:银?数据

String str = "hello";
 
String subStr = str.subString(1, 3);
// 底层是 new 的方式返回一个 subStr,实体内容是"el"

Java 中操作字符串有哪些类?他们之间有什么区别。

来源:南?电网、亿?国际、天?隆、?团、平?金服、?为、润?软件

  • String:不可变的字符序列;底层使用 char[](JDK8 及之前),底层使用 byte[](JDK9 及之后)
  • StringBuffer:可变的字符序列;JDK1.0 声明,线程安全的,效率低;底层同 String
  • StringBuilder:可变的字符序列;JDK5.0 声明,线程不安全的,效率高;底层同 String

String 的线程安全问题

来源:闪?购

线程不安全的

简单说说 ComparableComparator 的区别和场景?

来源:软??力

比较器

Comparable 接口和 Comparator 接口实现比较

来源:阿??巴

比较器

拓展练习

字符串的 length 和数组的 length 有什么不同?

字符串是 length() 方法,数组是 length 属性

值传递

public class Exercise8 {
	public static void main(String[] args) {
		StringBuffer a = new StringBuffer("A");
		StringBuffer b = new StringBuffer("B");
		operate(a, b);
		System.out.println(a + "," + b);
	}
 
	public static void operate(StringBuffer x, StringBuffer y) {
		x.append(y);
		y = x;
        y.append('x');
	}
}

值传递、不可变对象

class TEXT {
	public int num;
	public String str;
	
	public TEXT(int num, String str) {
		this.num = num;
		this.str = str;
	}
}
 
public class Exercise2 {
	// 都是值传递,区别在于这个值代表的是数据还是指针
	public static void f1(TEXT tIn, int intIn, Integer integerIn, String strIn){
		tIn.num =200;
		tIn.str = "bcd";
		// 形参和实参指向的是同一个 TEXT 的对象,修改了属性,就相当于修改实参对象的属性
 
		intIn = 200;
		// 数据
 
		integerIn = 200;
		strIn = "bcd";
		// 包装类和 String,都是不可变对象,一旦修改都是新对象,不影响原对象
	}
 
	public static void main(String[] args) {
		TEXT tIn = new TEXT(100, "abc");
		int intIn = 100;
		Integer integerIn = 100;
		String strIn = "abc";
		
		f1(tIn, intIn, integerIn, strIn);
		
		System.out.println(tIn.num + tIn.str + intIn + integerIn + strIn);
	}
}

值传递、“遮蔽”

public class Exercise5 {
	int a;
	int b;
    String str;
 
	public void f() {
		a = 0;
		b = 0;
        str = "hello";
		int[] c = {0};
		g(b, c, str);
		System.out.println(a + " " + b + " " + c[0] + "," + str);
	}
 
	public void g(int b, int[] c, String s) {
		a = 1;
		b = 1;
		c[0] = 1;
        s = "world";
	}
 
	public static void main(String[] args) {
		Exercise5 t = new Exercise5();
		t.f();
	}
}

StringStringBuffer 1

public class Exercise3 {
    public static void stringReplace(String text) {
        text = text.replace('j', 'i');
    }
 
    public static void bufferReplace(StringBuffer text) {
        text.append("C");
        text = new StringBuffer("Hello");
        text.append("World!");
    }
 
    public static void main(String[] args) {
        String textString = new String("java");
        StringBuffer textBuffer = new StringBuffer("java");
 
        stringReplace(textString);
        bufferReplace(textBuffer);
 
        System.out.println(textString);
		System.out.println(textBuffer);
    }
}

StringStringBuffer 2

public class Exercise6 {	
	private static void change(String s, StringBuffer sb) {
		s = "aaaa"; // 字符串对象是不可变,一旦修改,就是新对象
		sb.setLength(0); // 先把 sb 里面的内容给清空了
		sb.append("aaaa"); // 再拼接 aaaa
	}
	
	public static void main(String[] args) {
		String s = "bbbb";
		StringBuffer sb = new StringBuffer("bbbb");
		change(s, sb);
		System.out.println(s + sb);
	}
}

Stringchar[] 1

public class Exercise4 {
	public static void main(String[] args) {
		String str = new String("world");
		char[] ch = new char[]{'h', 'e', 'l', 'l', 'o'};
		change(str, ch);
		System.out.println(str);
		System.out.println(String.valueOf(ch));
	}
 
	public static void change(String str, char[] arr) {
		str = "change";
		arr[0] = 'a';
		arr[1] = 'b';
		arr[2] = 'c';
		arr[3] = 'd';
		arr[4] = 'e';
	}
}

Stringchar[] 2

public class Exercise7 {
	String str = new String("good");
	char[] ch = {'a', 'b', 'c'};
	
	public static void main(String[] args) {
		Exercise7 ex = new Exercise7();
 
		ex.change(ex.str,  ex.ch);
 
		System.out.print(ex.str + " and ");//good + and + gbc
		System.out.print(ex.ch);
	}
	
	public void change(String str, char[] ch) {
		str = "test ok";
		ch[0] = 'g';
	}
}

字符集、编解码

public class Exercise9 {
	/*
	 * GBK、UTF-8、ISO8859-1 所有的字符编码都向下兼容 ASCII 码
	 */
	public static void main(String[] args) throws Exception {
		String str = "中国";
		System.out.println(str.getBytes("ISO8859-1").length); // 2
		// ISO8859-1 把所有的字符都当做一个 byte 处理,处理不了多个字节
		System.out.println(str.getBytes("GBK").length); // 4 每一个中文都是对应 2 个字节
		System.out.println(str.getBytes("UTF-8").length); // 6 常规的中文都是 3 个字节
 
		/*
		 * 不乱码:(1)保证编码与解码的字符集名称一样(2)不缺字节
		 */
		System.out.println(new String(str.getBytes("ISO8859-1"), "ISO8859-1")); // 乱码
		System.out.println(new String(str.getBytes("GBK"), "GBK")); // 中国
		System.out.println(new String(str.getBytes("UTF-8"), "UTF-8")); // 中国
	}
}

String 相关