• 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

Casting  RSS feed

 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This question is from tipsmart.com's quiz.(25)

Q25.What will be the outcome of executing following code.
class MyClass
{
public static void main(String []args)
{
final int i = 100;
byte b = i;
System.out.println(b);
}
}
Will give compilation error
Will compile and print 100
Will throw an exception
SKIP THE QUESTION
Answer is compile and print 100.
My question is when we assign an integer to byte don't we get an error?
Vanitha.
[This message has been edited by Vanitha Sugumaran (edited May 30, 2001).]
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the conversion here is okay and no data is lost because 100 is small enough to be a byte (it's between -128 and +127)
Try it with a bigger number and the binary code will be truncated
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i think the reason is that when u specify the final modifier the compiler knows that the value of this variable will not change during the lifetime of the program execution and since the value u asssigned does fit in the range of a byte .... try assigning a value greater than 127 and u'll get a compile time error
hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
Samith is right. But IMO, heres a further clarification, if you remove the final keyword from the variable then the code will not
compile, as in that case the compiler knows for sure that the value of literal value of variable "i" can be more that the range of byte.
for example,

Hope this clears the issue.
Ravindra Mohan.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to you all. Now I understand the reason.
Vanitha.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
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!