String str = null和String str =“ null”有什么区别?

Nee*_*raj -2 java string null

我有一个带有两个字符串变量的类,即:

String str = null;
String str1="null";
Run Code Online (Sandbox Code Playgroud)

一个是“字符串值null”,另一种是将null对象分配给字符串变量。那么,这两个任务有什么区别?如何检查一个任务是否与另一个任务不同?

Kha*_*hah 7

String str = null; 表示str是String的引用,它指向null。

并且String str1="null";表示str1对象,该对象指向“ null”的字符串对象。

首先检查object是否为null?然后使用equals方法进行比较。喜欢

if(str==null && str1==null){
   //Both are null and equal
}   

if(str != null && str.equals(str1)){
  //return true;
}
else
{
   //return false;
}  
Run Code Online (Sandbox Code Playgroud)