Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# if comparison problem

S. Shree
Greenhorn
Posts: 16
why doesn't if (p==q==r==s) work? Is there any other way to check whether more than two variables are equal? I prefer not to your AND (&&).

Ranch Hand
Posts: 882
6
== is a binary operator, taking two operands and returning true or false.
Those are the rules.

For example (a==b) == (c==d) would return true if a=b=4; and c=d=5;

Chained assignment statements work because they return the value assigned
so a=b=5; is the same as a = (b=5); the paraned expression returning 5;

S. Shree
Greenhorn
Posts: 16
Hi Norm, thanks for your reply. I understand somethings you told but still can't get the picture clear. I would be glad if you can suggest me a way with example on how I can actually compare a,b,c,d to be equal in a if condition
[ June 27, 2008: Message edited by: S. Shree ]

fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
a==b==c==d doesn't work because that's how the language is defined.

a==b returns one of two values - true or false. that's how the creators defined it to work. if you tried to chain them all together, the compiler would have to decide what to do first. let's assume it starts at the right side and goes backwards.

first, we'd evaluate c==d. we can, for the sake of discussion, assume it returns 'true'. now the thing is, with most languages, to evaluate a long string like this, you evaluate the first part, then substitute the result in for what you just figured out. so, we'd stick 'true' in where 'c==d' was. that would then give us

a==b==true

now we evaluate b==true - and this would most likely not make sense, since b is probably not a boolean - i.e. it's a number like 7.

it just doesn't make sense. Therefore, the compiler does not allow it.

you're pretty much stuck with either a bunch of && somewhere in your code.

is there some REASON why you don't want to use them?

S. Shree
Greenhorn
Posts: 16
Thanks Fred for your reply. I would stick to && just thought was there any other way or java api that would have let me handled that. Thanks though, really appreciate your time.

Garrett Rowe
Ranch Hand
Posts: 1296
You could always write something generic like this:

[ June 28, 2008: Message edited by: Garrett Rowe ]

Ambi Avatar
Greenhorn
Posts: 2
Hi Guys, I tried the below progam, and it is working fine for me.

pblic class ComparsionExample
{
public static void main(String[] args)
{
boolean i=true,j=true,k=true,l=true;
if(i==j==k==l) {
System.out.println("It is equal i" + i);
System.out.println("It is equal j" + j);
System.out.println("It is equal k" + k);
System.out.println("It is equal l" + l);

System.out.println("It is equal");
}
else
System.out.println("It is not equal");
System.out.println("Hello World!");
}
}

If all declarations are true
Output --> true

If any of the declaration is false.
Output --> false

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24211
35
This is parsed as

if (((i == j) == k) ==l)

So you see why it only works for booleans; you must be able to compare the result of a comparison to another variable. It's not telling you that all the variables are equal. To see that I'm telling the truth, go ahead and try this: it will print "true" for

i: true
j: false
k: false
l: true

because i == j is false, which is == to k, since k is false; since false == false, this is equal to l, which is true.