File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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

nan s
Greenhorn

Joined: Dec 28, 2000
Posts: 16
Hi!
I try to compile the following code, and get a compile error
said:
Test.java:4: 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
Greenhorn

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.
((i&j)==4)//right
Ricke Hor
Greenhorn

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
Greenhorn

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 http://www.javaranch.com/naming.jsp ... 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: http://aspose.com/file-tools
 
subject: if ( i&j == 4 ): what is wrong?