• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all!
i see
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic