简体   繁体   English

作业分配Java

[英]Assignment Operation Java

class string1 {
    static public void main(String[] ar) {
        String s1 = "test";
        String s2 = new String();
        if(s2=null) { // line 0
            System.out.println(s1);
            System.out.println(s2.length()); //Line 1
        }
    }
}

What i expected to happen was 我期望发生的是

a) s2 will be set to null because its an assignment operation as I am not using == a)s2将被设置为null,因为它是我不使用==时的赋值操作

b) And I will get a NPE at Line 1 during Runtime b)我将在运行时在1号线获得NPE

and instead I got the following output as shown below. 相反,我得到以下输出,如下所示。

Output is 输出是

if(s2=null)
     ^
  required: boolean
  found:    String
1 error

Can someone explain why I am getting compile error ? 有人可以解释为什么我遇到编译错误吗?

Java will not implicitly cast to a boolean like some languages will. Java不会像某些语言那样将其隐式转换为布尔值。 You must put an expression that evaluates to a boolean inside of conditional: 您必须在条件内部放入一个表达式,该表达式的值为布尔值:

if ((s2 = null) != null) { }

This condition of course makes no sense, but it's the equivalent of what you're attempting to do. 当然,这种情况没有任何意义,但这等同于您要尝试做的事情。 It's worth noting by the way that assignments should rarely be done in a condition. 值得注意的是,很少应在某种情况下完成分配。

You should do like this: 您应该这样做:

if( (s2 = null) != null)
{
  // do stuff
}

The reason you are getting this error because you have to explicitly put the code for boolean evaluation in conditions as Java doesn't implicitly cast it boolean . 之所以会出现此错误,是因为您必须explicitly将用于boolean evaluation的代码置于条件下,因为Java doesn't implicitly cast it boolean

An if-then statement is executed by first evaluating the Expression. 通过首先评估表达式来执行if-then语句。

From, JLS - Chapter14 : Blocks and Statements 来自JLS,第14章:块和语句

14.9.1. 14.9.1。 The if-then Statement if-then语句

An if-then statement is executed by first evaluating the Expression. 通过首先评估表达式来执行if-then语句。 If the result is of type Boolean, it is subject to unboxing conversion (§5.1.8). 如果结果为布尔类型,则将进行装箱转换(第5.1.8节)。

If evaluation of the Expression or the subsequent unboxing conversion (if any) completes abruptly for some reason, the if-then statement completes abruptly for the same reason. 如果出于某种原因对表达式的评估或后续的取消装箱转换(如果有的话)突然完成,则if-then语句由于相同的原因而突然完成。

Otherwise, execution continues by making a choice based on the resulting value: 否则,将根据结果值进行选择以继续执行:

If the value is true, then the contained Statement is executed; 如果该值为true,则执行所包含的Statement;否则,为false。 the if-then statement completes normally if and only if execution of the Statement completes normally. 如果且仅当该语句的执行正常完成时,if-then语句才能正常完成。

If the value is false, no further action is taken and the if-then statement completes normally. 如果该值为false,则不采取进一步的操作,并且if-then语句正常完成。

You should note the fact that in java there are only two boolean values : true and false unlike some other languages which use 0 as false and anything else as true . 您应该注意到一个事实,在Java中只有两个布尔值: truefalse与其他一些将0用作false并将其他任何值用作true的语言不同。

Can someone explain why I am getting compile error ? 有人可以解释为什么我遇到编译错误吗?

In an if statement the condition can either be true or false which are returned by comparison operations and by the boolean variables. 在if语句中,条件可以是true或false,由比较操作和布尔变量返回。

In, 在,

if(s2=null)

the s2=null doesn't evaluate to any boolean value ie, true or false therefore the compiler error : s2=null不会计算任何布尔值,即true或false,因此是编译器错误:

required: boolean
  found:    String

The if condition evaluates only boolean conditions. if条件仅计算布尔条件。

And the assignment operator does not give a boolean result here. 并且赋值运算符在此处不给出布尔结果。

That is the reason for getting exception. 这就是得到例外的原因。

If you use if ((s2 = null) != null) instead of if(s2=null) it will work. 如果使用if ((s2 = null) != null)而不是if(s2=null) ,它将起作用。

Besides what everyone else said, s2 will never be null - you just assigned a new String to it! 除了其他人说的,s2 永远不会为空-您刚刚为其分配了一个新的String! Are you sure you're not conflating string length/existence tests from other languages? 您确定不混淆其他语言的字符串长度/存在性测试吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM