• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

John Meyer's SCJP 5 mock exam - casting  RSS feed

 
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
When we try to assign the higher value literals to the lower value literals at that time we need to do the explicitly cast to avoid the compiler problem.



But above problems correct answer is stating that "Compiler error:loss of precesion".
I know this is the stupid question but I want to clear the doubt for this.
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rizwan I didn't get what you are trying to say. The error is correct.. It will say possible loss of precision. What are you expecting...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will difinitely give you an compile error since your assigning a value which in int type to a variable which has byte type.. But of course, you can explicitly cast it (e.g byte b = (byte)i; ) but the precise value will be lost.. The value will be squeeze to byte and disregard the rest which will not be fitted to the size of byte(8 bits)

I hope I answered your question correctly.
 
Rizwan Patel
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When run this program on my machine i get the below comppiler problem

Type mismatch: cannot convert from int to byte

I read about the loss of precision while down-casting (only reference in literals).

Whether the above compiler error message are equal.
 
Rizwan Patel
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When run this program on my machine i get the below comppiler problem

Type mismatch: cannot convert from int to byte

I read about the loss of precision while down-casting (only reference in literals).

Whether the above compiler error message are equal.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because here i is not compile time constant.If you replace the line final int i;
i = 127; with int i=127 then it will be fine.
 
Rizwan Patel
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When run this program on my machine i get the below comppiler problem

Type mismatch: cannot convert from int to byte

I read about the loss of precision while down-casting (only reference in literals).

Whether the above compiler error message are equal.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rizwan,
This sure will give you possible loss of precision error.But if you initialize final int i where you declared it, will compile fine.Because the compiler sees that 127 will surely fit in a byte and its value is not going to change since it is final.
But, if you just say int i=127 or anything <127 ,the compiler cannot be sure if that value is going to be same.So it flags an error eventhough the value fits comfortably in a byte.
Another thing,this holds true only if the value is <=127.If the value is above the range of byte,even if it is a comile time constant ,compiler flags an error.Hope this clears!
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!