This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes if ( i&j == 4  ): what is wrong? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "if ( i&j == 4  ): what is wrong?" Watch "if ( i&j == 4  ): what is wrong?" New topic

if ( i&j == 4 ): what is wrong?

nan s

Joined: Dec 28, 2000
Posts: 16
I try to compile the following code, and get a compile error
said: operator & cannot be applied to int,boolean
if ( i&j == 4 )
1 error

public class Test {
public static void main(String[] args){
int i = 4, j = 7;
if ( i&j == 4 )
System.out.println(" equal ");
else System.out.println(" not equal");
what is the compiler talking about?
alpa urja

Joined: Dec 27, 2000
Posts: 17
here compiler give first priority to == operator so
first check7==4 which will give false so int&boolean
& is not operate on boolean type so it will give an error.
Ricke Hor

Joined: Jan 05, 2001
Posts: 2
Since the priority of == is higher than &, therefor it become
(i&(j==4)), (int)&(boolean) result in compile error
nan s

Joined: Dec 28, 2000
Posts: 16
Thanks all!
i see
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
nan s,
your name does not comply with the Javaranch naming standards outlined here ... could you please re-register with a valid name.
Thank you

Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
I agree. Here's the link:
subject: if ( i&j == 4 ): what is wrong?
It's not a secret anymore!