67,547
社区成员




Object obj = NULL; // Not Ok
Object obj1 = null //Ok
private static Object myObj;
public static void main(String args[]){
System.out.println("What is value of myObjc : " + myObj);
}
1
What is value of myObjc : null
String str = null; // null can be assigned to String
Integer itr = null; // you can assign null to Integer also
Double dbl = null; // null can also be assigned to Double
String myStr = (String) null; // null can be type cast to String
Integer myItr = (Integer) null; // it can also be type casted to Integer
Double myDbl = (Double) null; // yes it's possible, no error
int i = null; // type mismatch : cannot convert from null to int
short s = null; // type mismatch : cannot convert from null to short
byte b = null: // type mismatch : cannot convert from null to byte
double d = null; //type mismatch : cannot convert from null to double
Integer itr = null; // this is ok
int j = itr; // 这样转没问题,但是在运行时,会有空指针异常
Integer iAmNull = null;
if(iAmNull instanceof Integer){
System.out.println("iAmNull is instance of Integer");
}else{
System.out.println("iAmNull is NOT an instance of Integer");
}
iAmNull is NOT an instance of Integer
public class Test {
public static void main(String args[]) throws InterruptedException {
String abc = null;
if(abc == null){
System.out.println("null == null is true in Java");
}
if(abc != null){
System.out.println("null != null is false in Java");
}
}
}
public class Test1 {
public static void main(String[] args) throws ParseException {
String s = null; //初始化字符串为null
System.out.println(s.equals("null")); //这里调用为null的字符串的equals方法,会报NullPointException
String str = new String();
System.out.println(str.equals("null")); //这个调用就没问题,因为new 了一个对象,str不是null,只是对象里面是空壳而已
}
}
public static void main(String[] args) {
new Ss().fun(null);
}
public void fun(Object o){
System.out.println("Object");
}
public void fun(String o){
System.out.println("String");
}
输出应该是什么?
2.map的key和value可以同时为null吗?