public class Typetest
{
public static void main(String[] args)
{
String a=new String();
StringBuffer c=new StringBuffer();
int[] e=new int[3];
if (a==null)
System.out.println(a+"abc");
if (c!=null)
System.out.println(c+"hello");
}
}
{
public static void main(String[] args)
{
String a=new String();
StringBuffer c=new StringBuffer();
int[] e=new int[3];
if (a==null)
System.out.println(a+"abc");
if (c!=null)
System.out.println(c+"hello");
}
}
这样才是null值
String a = new String();
这样是初始化一个新创建的 String 对象,它表示一个空字符序列。
你可以查查API文档
这样才是null值
String a = new String(); 这个是新建了一个对象,是空字符队列~
另外,经测试String a = new String()不是空字符啊("")。
String str;
当这个str作为类的属性时,默认值是空的null
如果作为局部变量用时,你要给个初值,new String()相等于给它一个初值了.
如果
class test{
static String a;
public static void main(String[] args){
System.out.println(a);
}
}
这个时候才有初始化值,并且是null
所谓的出值是说一个对象的参数在没有赋给任何值的情况下的值。不是调用默认方法的情况下的值,并且方法里面的局部变量是必须赋值才能使用,是不存在默认值的
static String a;
public static void main(String[] args){
System.out.println(a==null); //返回是true
}
}
证据2:
class test{
static String a;
public static void main(String[] args){
System.out.println(a.toString()); //报告空指针异常,证明a是null,没有指向任何对象
}
}
public String()初始化一个新创建的 String 对象,使其表示一个空字符序列。注意,由于 String 是不可变的,所以无需使用此构造方法。
if("".equals(a))
System.out.println("a.equals(\"\")");
System.out.println(str == null); // 输出trueString str = new String();
System.out.println("".equals(str)); // 输出true
-------------------------
String属性默认初始值是null,也只有属性才有默认初始值( ̄(工) ̄)
要是string 变量的话,没有附植,则为空字符串