views:

231

answers:

4

I was trying to write a simple method:

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

And got a strange error on the == null part:

Syntax error on token ==. Invalid assignment operator.

Maybe my Java is rusty after a 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 this be?

I'm using jdk160_05.

To clarify: I'm not trying to assign anything, just do an && operation between two boolean values. I don't want to do this:

if (o.propertyA() == null && o.propertyB() == null) { return true; }
else { return false; }
+9  A: 

== is not an assignment operator, it's a boolean equality operator, see:

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

If you want to set i 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 is null then you need to use the == operator

if (i == null)
Jon
I dont want to assign anything. See my updated post please.
Tom
Can you please explain what you are trying to do then... you're question is a little confusing...
Jon
So I cant do something like `return i==null` ? Hard to believe.
Tom
Yes, you can write "return i==null;"But that is not what you submitted us for our consideration.
leonbloy
`return i==null` will work. Your code above is only `i==null`.`i==null` by itself doesn't really mean anything.
ryanprayogo
@ryanprayogo. Still, compiler error? Should just compile into a no-op or something.
Tom
+1  A: 

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

In Java, assigning a value to a variable is done with the = operator. Comparing two values is done with ==, or a .equals() method in some cases.

You can do things like this:

x = i==null;

This will test if i is null and if so, the value true will be assigned to x (assuming that x is a boolean).

FrustratedWithFormsDesigner
+4  A: 

I don't think you are confusing assignment and equality comparison. I think your compiler is just giving you a confusing error message. This program:

Integer i = 4;
i ==null; 

should give an error something like this:

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

Your first program should compile correctly. Perhaps there is some invisible unicode character in the text that is confusing the compiler. Try deleting the entire function and typing it in again.

Mark Byers
Thanks for putting some thought into it and not giving the `you cant assign` crappy answer.
Tom
? so did you fix your problem or not?
Pyrolistical
@Pyrolistical. It helped more than all the other answers.
Tom
I think Tom's question is more completely answered by my answer, but yours at least also points in the right direction. The issue is that `i==null` isn't a statement, but some java compilers give crappy diagnostics for the "not a statement" case.
Daniel Martin
@Tom So if it helped, what was the problem?
starblue
+4  A: 

I think I see your problem. I'm sorry the other answers don't address it.

So, Java has this idea that is shared by some other languages that just because something is a valid expression doesn't mean that that thing, by itself, is a valid statement.

For example, this code will complain similarly:

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

And yet obviously I can use i+3 (go unboxing!) elsewhere to mean "7":

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

It gets a bit confusing because unlike some languages that have this expression/statement distinction, java allows you to use any method call - whether it returns a value or not - as a statement. However, most java operators do not - by themselves - form a valid statement.

Likewise, this fails to compile:

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

For the full gory details, see http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#32588

Daniel Martin
Thanks for giving it some thought.
Tom