• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

John Meyer's SCJP 5 mock exam - casting

 
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
If you're gonna buy things, buy this thing and I get a fat kickback:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic