Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
nan s
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the priority of == is higher than &, therefor it become
(i&(j==4)), (int)&(boolean) result in compile error
 
nan s
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all!
i see
 
Angela Poynton
Ranch Hand
Posts: 3143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic