
//String类的构建
String str1 = "goodboy";//直接赋值
String str2 = new String("goodboy!");//使用new String();
char[] ch = {'g','o','o','d','b','o','y'};//利用字符数组来帮助构建
String str3 = new String(ch);
//下图是不同创建方式在内存上存储的区别

String str1 = "hahahaha";
String str2 = "HAHAHAHA";
System.out.println(str1.compareTo(str2));//32
//忽略大小写比较大小
System.out.println(str1.compareToIgnoreCase(str2));//0
System.out.println("=====================");
System.out.println(str1.equals(str2));//false
//忽略大小写比较是否相同
System.out.println(str1.equalsIgnoreCase(str2));//true String str1 = "holabce"; char ret = str1.charAt(3); System.out.println(ret);//输出结果为:a
class student{
public int age;
public student(int age) {
this.age = age;
}
@Override
public String toString() {
return "student{" +
"age=" + age +
'}';
}
}
//方法1,
String s1 = String.valueOf(123);
String s2 = String.valueOf(0.666);
方法2,
String s3 = String.valueOf(new student(16));
System.out.println(s3); int a = Integer.valueOf("66");
int b = Integer.parseInt("99"); String s1 = "goodboy"; //不会改变字符串s1 String ret = s1.toUpperCase(); System.out.println(s1);//goodboy System.out.println(ret);//GOODBOY String s2 = "GOODBOY"; String ret2 = s2.toLowerCase(); //不会改变字符串s2 System.out.println(s2);//GOODBOY System.out.println(ret2);//goodboy
String s1 = "goodboy";
char[] chars = s1.toCharArray();
for (char x: chars) {
System.out.println(x);
}
System.out.println(s1);//goodboy 

只要是new一个对象,这些对象都是唯一的,他们的地址都不一样。
解释一下String类三种对象实例化的区别。
1.String str1 = "abcde"; 只会开辟一块堆内存空间并存放在常量池中 2.String str2 = new String("abcde"); 会开辟俩块堆内存空间,将字符串abcde到常量池中,还有一块存放:new一个String对象 3.char[] ch = {'a','b','c','d','e'}; String str3 = new String(ch); 会开辟三块堆内存空间,一块空间存放数组,另外一块存放拷贝后的数组,还有一块存放:new一个String对象
1.
public final array[] = {1,2,3,4,5,6};
array[0] = 66;//不会报错
2.
array = new int[]{9,10,11,12};//array的引用都变了,报错 1.String的内容不可以修改,StringBuilder和StringBuffer的内容可以修改 2.StringBuilder和StringBuffer的功能类似 3.StringBuffer属于线程安全操作
判断创建了多少个String对象
//hello String
String str1 = new String("hello");共创建了俩个对象
//a String b String StringBuffer toString
String str2 = new String("a")+new String("b");//共创建了6个对象 如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹