tags:

views:

531

answers:

11

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.

+22  A: 

== 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.

FreeMemory
The C++ compilers warn about this case. So if you're watching the warning errors, not only the compiler errors (recommended policy :) ), you'll notice it.
Cătălin Pitiș
It's an equals expression (which may be an expression statement). The value of the expression, is the value of the right hand subexpression. if (x = y) is perfectly legal if x and y are of the boolean or Boolean.
Tom Hawtin - tackline
C and C++ allow it (with warnings) because the test is whether the value is 0 or not (roughly). There is boolean type.
Tom Hawtin - tackline
Dear Tom,I can not get what u mean with if(x=y)!!!!
Johanna
@Neda, since x and y are both booleans, if(x=y) equates to "Store the value of Y in X, and if the new value of X is 'true', do something.
TheMissingLINQ
+4  A: 

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

Darron
+4  A: 
== is an equality check. if (x == 0) // if x equals 0
= is an assignment. x = 0; // the value of x is now 0
Jason Miesionczek
+1  A: 
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.

Bill the Lizard
+12  A: 

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.

Ambuoroko
+1  A: 

Interpret the error to mean

"The expression

x=0

cannot be converted to Boolean."

Thomas L Holaday
A: 

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

in your program change

if(x=0)

to

if(x==0)

leadingzero
+1  A: 

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

Svetlozar Angelov
A: 
"==" 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.

Mahesh
+2  A: 

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.

Schamp
+1 Exactly what I was going to recommend!
n8wrl
+1  A: 

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 '=').

Greg Adamski