This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!