• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Incomparable types  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Q016 {

public String get ( int i ) {
return null ;
}

public static void main ( String ka [ ] ) {
byte b = 127 ; // ---> line 1
byte d = b >> 3 ; // ---> line 2
b >>= 3 ; // ---> line 3
System.out.println ( "b == d" + b == d) ; // ---> line 4
}

};


the Program when compile gives 2 error
Error1: possible loss of precision
byte d = b >> 3 ; // ---> line 2
^
Error2: incomparable types: java.lang.String and byte
System . out . println ( "b == d" + b == d) ; // ---> line 4

For error1 : Each timeassigning result of shift operators to byte do we always need a cast( shift opearotr always produce an int result..is it??)

and why b and d are incomparable..both are byte..then why it is giving error?
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swati,

I dont think we have shift operators for the 1.5 exam.


Uttara Rishi.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System . out . println ( "b == d" + b == d) ; // ---> line 4

I think this statement is evaluated from left to right. So the "b==d" + b part is evaluated first, resulting in a String. Then Java tries to compare the newly created String to the byte variable d, which results in a compilation error.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!