2、String类——构造方法(掌握)
1、字符串概念
由多个字符组成的一串数据。
2、构造方法
创建字符串对象的几种方式
String s = new String();
通过字节数组创建字符串对象
String s = new String(byte[] bys);
通过字节数组创建字符串对象常用
String s = new String(byte[] bys,int index, int length)
通过字节数组一部分创建字符串对象
String s = new String(char[] chs)
通过字符数组创建字符串对象常用
String s = new String(char[] chs, int index, int length);
通过字符数组一部分创建字符串对象
String s = new String(String);
通过给构造方法传入字符串创建字符字符串对象
String s = “helloworld”
直接给字符串对象赋值常用
2、面试题
a:String s = new String("hello")和String s = "hello"的区别。
答:new String(“hello”)在内存中创建了1个或两个对象,为什么..
“hello”在内存中创建了0个或一个对象,为什么…
b:请写出结果:
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));