Invalid assignment error from statement ==

I tried to write a simple method:

boolean validate(MyObject o) { // propertyA && propertyB are not primitive types. return o.getPropertyA() == null && o.getPropertyB() == null; } 

And we got a strange error == null :

Syntax error in token ==. Disabled assignment operator.

Maybe my Java is rusting after the season in PLSQL. So I tried a simpler example:

 Integer i = 4; i == null; // compile error: Syntax error on token ==. Invalid assignment operator. Integer i2 = 4; if (i == null); //No problem 

How can it be?

I am using jdk160_05.

To clarify: I'm not trying to assign anything, just do the && operation between two booleans. I do not want to do this:

 if (o.propertyA() == null && o.propertyB() == null) { return true; } else { return false; } 
+4
source share
4 answers

I do not think that you confuse the comparison of purpose and equality. I think your compiler just gives you a confusing error message. This program:

 Integer i = 4; i ==null; 

should give an error like this:

 Program.java:8: not a statement i ==null; 

Your first program should compile correctly. Perhaps there is an invisible unicode character in the text, which the compiler confuses. Try deleting the entire function and re-entering it.

+4
source

== is not an assignment operator, it is a logical equality operator, see:

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.21.2

If you want to set me to null, use the simple assignment operator =:

 i = null; 

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26.1

If you want to check that I am null, you need to use the == operator

 if (i == null) 
+9
source

I think I see your problem. I am sorry that the other answers do not relate to this.

So, Java has this idea, which is shared by some other languages, because something is a valid expression, does not mean that this thing in itself is a valid statement.

For example, this code will complain in a similar way:

 Integer i = 4; i+3; // this line gives a compilation error 

And yet I can use i+3 (go unboxing!) Elsewhere to denote " 7 ":

 System.out.println(i+3); // this is fine 

It is a bit confusing, because unlike some languages ​​that have this difference between an expression / expression, java allows you to use any method call - whether it returns a value or not - as an operator. However, most java statements themselves do not form a valid statement.

Similarly, this does not compile:

 Integer i = 4; i; // this line gives a compilation error 

See details at http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#32588

+4
source

In PL / SQL, assigning a value to a variable is done using the := operator. Comparison of two values ​​is done with = .

In Java, assigning a value to a variable is done using the = operator. Comparison of two values ​​is performed using the == or .equals() method in some cases.

You can do things like this:

 x = i==null; 

This will check if i is null , and if so, then true will be assigned x (assuming x is Boolean).

+1
source

Source: https://habr.com/ru/post/1308392/


All Articles