Johanna
Johanna

Reputation: 27660

What is the difference between '==' and '='?

I know that one of them is bitwise and the other is logical but I can not figure this out:

Scanner sc = new Scanner(System.in);
System.out.println("Enter ur integer");
int x=sc.nextInt();
if(x=0)//Error...it can not be converted from int to boolean
System.out.println("...");

The error means that x cannot be converted to boolean or the result of x=0 can not be converted to boolean.

Upvotes: 0

Views: 4177

Answers (12)

ameed
ameed

Reputation: 1170

As others stated, = assigns while == compares.

However, these statements have their own values as well.

The = operator returns the value of its right-hand operand. This is how statements like a = b = c = 5 work: they are parsed as a = (b = (c = 5)), which evaluates to a = (b = 5) and then a = 5.

The == operator returns a boolean that is true if its operands are equal. The if statement runs its body if its argument is true. Thus, if headers like if (5 == 5) translate to if (true). This is why sometimes you see infinite while loops with header while (true); the while loop runs "while" toe argument is true.

If you had a boolean in your if statement, it would give no error and run the code if the value being assigned (or "compared to") was true. This is why it is so important to never mix up the = and == operators, especially when working with booleans.

Hope this helped!!

Upvotes: 0

Greg Adamski
Greg Adamski

Reputation: 1099

One interesting note: Since assignment operator evaluates to the right operand, the following is valid in Java(albeit not pretty):

if (( x = blah ) > 0) ...

Parenthesis are needed because of operator precedence ( '>' binds stronger than '=').

Upvotes: 1

Schamp
Schamp

Reputation: 302

I know the question has been answered, but this still comes up from time to time not as a programmer error but as a typographical error (i.e., the programmer knew what he meant, but failed). It can be hard to see, since the two look so similar.

I've found that a way to help avoid doing this is to put the constant expression on the left-hand-side, like so:

if (0 == x) 
   ...

That way, if I accidentally use only one "=" sign, the compiler will fail with an error about assigning to a constant expression, whether or not the assignment operator is left-associative and whether the if conditional expects a strongly-typed Boolean.

Upvotes: 2

Mahesh
Mahesh

Reputation: 487

"==" checks for equality



"=" Is used for assignment. 

It is giving you error cause you're assigning value to x in if(), where you're supposed to check for the equality. Try changing it to equality instead of assignment operator.

Upvotes: 0

Svetlozar Angelov
Svetlozar Angelov

Reputation: 21680

Just to clarify about C/C++ - assignment is evaluated to the right operand

if(a = n)

is evaluated to n, so (n = 1) is true (n = 0) is false

Upvotes: 1

leadingzero
leadingzero

Reputation: 181

As others have already said, '=' is assignment; '==' is compare.

in your program change

if(x=0)

to

if(x==0)

Upvotes: 0

Thomas L Holaday
Thomas L Holaday

Reputation: 13862

Interpret the error to mean

"The expression

x=0

cannot be converted to Boolean."

Upvotes: 1

Ambuoroko
Ambuoroko

Reputation: 342

When you write 'x = 0' you are saying "Store 0 in the variable x". The return value on the whole expression is '0' (it's like this so you can say silly things like x = y = 0).

When you write 'x == 0' it says "Does x equal 0?". The return value on this expression is going to be either 'true' or 'false'.

In Java, you can't just say if(0) because if expects a true/false answer. So putting if(x = 0) is not correct, but if(x == 0) is fine.

Upvotes: 13

Bill the Lizard
Bill the Lizard

Reputation: 406105

if(x=0)

Here you're assigning the value of 0 to the variable x. The if statement in Java can't evaluate an integer argument as it can in many other languages. In Java, if requires a boolean. Try

if(x == 0)

to do a comparison.

Upvotes: 1

Jason Miesionczek
Jason Miesionczek

Reputation: 14448

== is an equality check. if (x == 0) // if x equals 0
= is an assignment. x = 0; // the value of x is now 0

Upvotes: 4

Darron
Darron

Reputation: 21620

== is a comparison operator, and = is assignment.

Upvotes: 5

FreeMemory
FreeMemory

Reputation: 8634

== checks for equality. = is assignment.

What you're doing is: if( x = Blah ) - in Java this statement is illegal as you can not test the state of an assignment statement. Specifically, Java does not treat assignment as a boolean operation, which is required in an if statement. This is in contrast with C/C++, which DOES allow you to treat assignment as a boolean operation, and can be the result of many hair-pulling bugs.

Upvotes: 23

Related Questions