File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes if comparison problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if comparison problem" Watch "if comparison problem" New topic

if comparison problem

S. Shree

Joined: Jun 25, 2008
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 (&&).
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 692
== is a binary operator, taking two operands and returning true or false.
Those are the rules.

How would the compiler parse your code? By adding parans?
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

Joined: Jun 25, 2008
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

Joined: Oct 02, 2003
Posts: 11955

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


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?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
S. Shree

Joined: Jun 25, 2008
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

Joined: Jan 17, 2006
Posts: 1296
You could always write something generic like this:

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

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ambi Avatar

Joined: Jun 27, 2008
Posts: 2
Hi Guys, I tried the below progam, and it is working fine for me.
Please correct me if I made any mistakes

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");
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

Thanks in Advance
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: if comparison problem
It's not a secret anymore!