Win a copy of Pipeline as Code this week in the Cloud/Virtualization 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

how is this possible??--Help Wanted

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i ran the following program, I got the output as 100.My question is: how did the int value of "i" get typecasted to byte??
class Nq
{
public static void main(String args[])
{
final int i=100;
byte b=i;
System.out.println(b);
}
}
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting though, but java Bible says that final variable may be assigned to a wider variable. If you delete the final and compile, you will get an error.
Albert

Originally posted by prash fresh:
when i ran the following program, I got the output as 100.My question is: how did the int value of "i" get typecasted to byte??
class Nq
{
public static void main(String args[])
{
final int i=100;
byte b=i;
System.out.println(b);
}
}


 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
There is a simple explanation to it. That 100 can easily fir inro byte's 7 bits. so it soes not give any error even i u r not type casting the value to byte. where as if u put the value of ini variable as 256 or something more. The program will not compile and will give u an error
possible loss of precision
found : int
required: byte
byte b=i;
^
1 error
I hope i made my point clear.
------------------
Cheers
Tamanna :-)
 
prash fresh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Albert,
Your reply seems ok but when we replace int declaration of "i" to long, the program does not work even though it is still final. Why is it so?
-Prash
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
See JLS §5.2 Assignment Conversion. It explains what your seeing.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic