Java中null==1,为什么会报错?
PHP中文网
PHP中文网 2017-04-18 10:09:58
0
4
851

if(a=1){
}
当a=null的时候,会报错,为什么呀?
是不是每一个判断都要先判断a是否等于null?

    String a=null;
    if (a.equals("")) {
        System.out.println(1);
    }
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(4)
黄舟

First of all, you wrote the if conditional statement if(a=1){} wrong. It should be written as if(a==1){}
Secondly, if the reference type a of String is empty, if the method of a is called Will throw the null pointer
 

String a=null;
if (a.equals("")) {
    System.out.println(1);
}

If you write like this, you must determine that a is not null. If you don’t want to write the operation of determining that a is not null, you can write it as

String a=null;
if("".equals(a)){
     System.out.println(1);
}
Peter_Zhu

What is the syntax of

a=1
If a is String type;
a= "1";
If a is int type;
a = 1;
If a is int type, int type does not have null
null can only Judge String

大家讲道理

What is written in the brackets of the if statement is a judgment expression, the result is true or false, and the a=1 you wrote is an assignment expression

左手右手慢动作

The type in the brackets is bool, yours is an assignment

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template