Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java (string==null)和(string.length()==0)之间的差异?_Java_String - Fatal编程技术网

Java (string==null)和(string.length()==0)之间的差异?

Java (string==null)和(string.length()==0)之间的差异?,java,string,Java,String,当我用这两种方式编程时,得到了不同的模拟结果: if (S == null) { return new LinkedList<>(); } if(S==null){ 返回新的LinkedList(); } 及 int len=S.length(); if(len==0)返回新的LinkedList(); 第一个代码给了我[],它通过了测试。第二个给了我[],但我失败了。 我还注意到还有另一种方式:S.isEmpty() 有人能解释一下吗?非常感谢 如果要

当我用这两种方式编程时,得到了不同的模拟结果:

if (S == null) {
        return new LinkedList<>();
    }
if(S==null){
返回新的LinkedList();
}

int len=S.length();
if(len==0)返回新的LinkedList();
第一个代码给了我[],它通过了测试。第二个给了我[],但我失败了。 我还注意到还有另一种方式:S.isEmpty()


有人能解释一下吗?非常感谢

如果要传递到第二个字符串的字符串为null,则应发生异常,因为调用null字符串时,
.length()
将引发异常。

如果
字符串
实例为
null
myInstance.length()=0
将引发
NullPointerException
,因为调用未实例化实例的实例成员会使应用程序崩溃


因此,如果您不确定字符串实例是否实例化,请始终执行
null
-检查,或者更好地使用Java 8或更高版本,使用
Optional
避免
null
String==null
检查对象是否为
null
(无,甚至不是空字符串)和
String#length()==0
(实际上,应该使用
String#isEmpty()
)检查String对象是否有0个字符。此外,如果对象为
null
,则无法访问任何方法,它将抛出
NullPointerException
(简称NPE)

(string==null)和(string.length()==0)之间的差异

非常不同

当您选中
(string==null)
时,它会检查字符串引用是否指向任何现有对象。如果它没有引用任何对象,它将返回
true

string.length()==0
只需检查现有字符串对象的内容并查看其长度是否为0。如果调用
.length()
时当前变量中不存在对象,则会得到一个
NullPointerException

s是一个引用变量(应以小写形式编写)

        //case1        
        String s;
            if(s==null)System.out.println("I am null");
            System.out.println(s.length());
     //error: variable s might not have been initialized
    //case2
    String s=null;
            if(s==null)System.out.println("I am null");
            System.out.println(s.length()); 
    /* output 
    I am null
    error: Null pointer exception
    */
    //case 3
    String s=new String();
            if(s==null)System.out.println("I am null");
            System.out.println("length is "+s.length());
    /* output 
    length is 0 
    */
    String s="";
            if(s==null)System.out.println("I am null");
            System.out.println("length is "+s.length());
    /* output 
    length is 0 
   */
S(或者更确切地说S)引用提供方法length()的对象

如果s确实是对对象的引用,则只能访问s引用的对象。如果s为null(s==null),则s不引用对象,因此无法调用方法length()。如果尝试,将得到NullPointerException

当s引用对象时,可以调用该对象上的length方法。在本例中,它是一个string对象。string对象可能不包含任何字符(空字符串或“”)

  • String s;//只是一个引用,初始值为null
  • s=”“;//s现在引用一个空字符串,并且不再为null
  • newstring(“”;//使用空字符串创建新对象
在Java中,您从来没有真正使用过对象。您只使用对对象的引用,尽管在大多数情况下,它看起来像是直接使用对象


请记住,引用变量和对象实际上是不同的对象。

S==null
意味着,例如,如果您尝试打印某些内容,将不会发生任何事情(或者可能是
nullpointerception
),因为null意味着此变量中没有任何内容

String.lenght==0
表示您的字符串等于“”

例如:

String S1 = '';
String S2 = null;
try{
  System.out.println(S1.length() == 0) {
  System.out.println('S1 is not null');
}catch(nullPointerExeption e){
  System.out.println('S1 is null');
}
try{
  System.out.println(S2.length())//it will throw you a java.nullpointerexcption
  System.out.println('S2 is not null');
}catch(nullPointerExeption e){
  System.out.println('S2 is null');
}
系统将写入

0
S1 is not null

S2 is null

空对象没有方法一个是空字符串,另一个不指向任何内容(因此
null
),您自己的想法是什么?最后,.isEmpty()将只执行.length()命令==0。没有太大区别。嗯……不。在您的示例中,s不是空的,s是实例化的。字符串s;这里,s是空的。另外:=和==之间有很大区别,您应该仔细阅读。@MiaT我知道您接受了这个答案,希望您意识到这是错误的?
0
S1 is not null

S2 is null