1.String不是8种基本类型中的一种,String是Object,其默认值是null
2.String不可变,是final的,当对 String的对象进行拼接等操作的时候都是生成的新的String对象
3.new String( )或者new String(”")是创建了一个空字符串,其值并不是null
4.常量池中的字符串常量解析期间确定,且只有一份拷贝;new String( )出来的字符串不是常量,不放在常量池中,有自己的地址空间
5.String的intern( )方法扩充常量池:当一个String实例调用intern方法时,如果常量池中有相同unicode的字符串常量,则返回其引用;如果没有,增加一个unicode为该实例的String常量, 并返回其引用
6.一个实例:
String str0 = “flynewton”;
String str1 = “flynewton”;
String str2 = “fly” + “newton”;
String str3 = new String(”flynewton”);
System.out.println(str0 == str1); //true
System.out.println(str0 == str2); //true
System.out.println(str0 == str3); //false
str3.intern();
System.out.println(str0 == str3); //false
System.out.println(str0 == str3.intern()); //true
Java String特性详解
本文深入探讨了Java中String类型的特性,包括String不是基本类型而是Object,默认值为null;String对象的不可变性及其带来的内存使用特点;通过new关键字创建String实例与常量池中的字符串处理方式的区别;以及intern()方法如何帮助优化内存使用。

被折叠的 条评论
为什么被折叠?



