== 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;
Joined: Jun 25, 2008
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 ]
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
Joined: Jun 25, 2008
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.
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.