Reputation: 2072
Currently I'm using:
int a=10;
if(a=20)
printf("TRUE");
else
printf("false");
Which prints, in C, the value TRUE
.
But in case of java:
int a=10;
if(a=20)
System.out.println("TRUE");
else
System.out.println("FALSE");
I'll get a compile time error about an incompatible type.
Upvotes: 1
Views: 22020
Reputation: 986
for comparison you should use ==
if(a==20)
instead of a=20
, which is assignment
Upvotes: 0
Reputation: 491
You need to replace =
with ==
.
=
is an assignment operator
int a=10;
if(a==20){
System.out.println("TRUE");}
else{
System.out.println("FALSE");}
This should work fine.
Edit:
In C, the returned integer value of the assignment (in this case 20) is a positive int, which evaluates to true. In java, a boolean is expected.
(See the more thorough answer provided by Aleks G)
Upvotes: 6
Reputation: 882716
A single =
is an assignment. If you want to test for equality, you use the double ==
in both C and Java.
It should be evident that you're doing the wrong thing, even in the C version, since you set a
to 10 and then, on "comparing" it to 20, you get a true value. I'm not aware of any mathematical systems where 10 and 20 are considered equal :-)
What happens in your C case is that the assignment actually "returns" a value as well.
The statement:
if (a = 20) ...
is equivalent to:
a = 20;
if (a) ...
so it first sets a
to 20, then uses that as the if
condition. Since 0 is false and anything else is true, the body of the if
is executed.
A good compiler (like gcc
) will actually warn you of what you're doing so that you don't get caught out by these little things. Some people also use the trick of putting constants first:
if (20 == a) ...
so that, if you mistakenly use assignment, it's a syntax error. But I find that sort of code ugly, especially since I mostly use those good compilers mentioned above :-)
The reason you get an error in Java is because it's much more strict on what you can do with assignments. By that, I mean there is no automatic conversion from int
to boolean
as with C. So this code refuses to compile, producing an error:
class Test {
static public void main(String[] args) {
int a = 10;
if (a = 20)
System.out.println ("true");
}
}
But you can still get burnt with something like this:
class Test {
static public void main(String[] args) {
boolean a = false;
if (a = true)
System.out.println ("true");
}
}
Because that assignment is of a boolean value inside the if
, it doesn't cause a compilation error due to the wrong type. However, since it prints true
, it's still not what you wanted.
Bottom line, go back an re-read that first paragraph. =
is assignment, ==
is equality checking.
Upvotes: 2
Reputation: 57346
The reason for that is that in C there is no specific type boolean
- instead any non-0 integer evaluates to a boolean "true". Thus in your C code:
if(a=20)
a
is assigned the value 20, which is non-0 - and the condition is evaluated as true
In java, there's a fundamental type boolean
and the value of the conditional inside if
must be of this type.
a=20
in Java assigns 20 to a
and returns the final result of evaluation as integer value 20
, however type boolean
is expected - hence you're getting a compile-time error about the incompatible types.
If you want to do a comparison of a
with 20, however, you need to use ==
operator both in C and Java:
if(a == 20)
This will compile in both C and Java and print FALSE in both languages.
Upvotes: 14
Reputation: 41025
Use the ==
to test the condition instead of =
. That way, you are assigning 20
to
a and not testing for a condition.
Upvotes: 0