aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if comparison problem" Watch "if comparison problem" New topic
Author

if comparison problem

S. Shree
Greenhorn

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: 685
== 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
Greenhorn

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
Bartender

Joined: Oct 02, 2003
Posts: 10915
    
  12

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?


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

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
Greenhorn

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

Thanks in Advance
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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: http://aspose.com/file-tools
 
subject: if comparison problem