user2892857
user2892857

Reputation: 435

What does this Java statement mean?

boolean rhs;  
rhs = value == null;

Specifically, the part I don't understand is the = operator followed by value followed by ==. What does that mean?

Upvotes: 0

Views: 121

Answers (4)

Reimeus
Reimeus

Reputation: 159874

value == null is a boolean expression which evaluates to true if value == null, otherwise it is false. The value of this expression is assigned to rhs

The 2 statements are equivalent to

boolean rhs;
if (value == null)
   rhs = true;
else
   rhs false;

Upvotes: 3

gjman2
gjman2

Reputation: 920

This is the simple way to check whether the the value is null or not. If null then is will assign true to rhs, else false. You can try it by your self using following code:

    String value = null;
    String value2 = "Testing";
    boolean rhs;
    System.out.println(rhs=value == null); //print true
    System.out.println(rhs);

    System.out.println(rhs=value2 == null);//print false
    System.out.println(rhs);

Upvotes: 0

OscarRyz
OscarRyz

Reputation: 199333

It is assigning to the boolean variable rhs the result of evaluating: value == null

Upvotes: 1

Pshemo
Pshemo

Reputation: 124275

Since comparing == has higher priority than = assigning, code

rhs = value == null;

is the same as

rhs = (value == null);

So it will check if value is null and store result of that test in rhs.

Upvotes: 10

Related Questions