• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

byte to int ?

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys and girls,
Public class AnotherDirtyOne
{
private final int i =10;
private byte k = i;
}
this compiles fine.
how come i doesn't need (byte) casting when assigned to k ?
can someone shed some light on this please?
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know the specefics of this answer, but my guess is that because you made i final, the compiler determines the value before the program is run and assings it then so it really no longer views it as an int. Because of this, you don't need to explicitly cast the value.
This is just an assumption, so if anyone can give a better and more scientific answer that would be great.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question. Hope there is an explanation.
 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Bill is right just because:
public class B
{
private final int i = 127;
private byte k = i;
}
compiles fine...BUT:
public class B
{
private final int i = 128;
private byte k = i;
}
does not...it's interesting I didn't know this happened. Does anybody know if this is legit based on the JLS? I always thought that you would have to do a cast if you were doing a narrowing conversion.
Matt
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i think its because, the assigned value was in range of -128 and 127. that is the reason y it was possible to assign to a byte. once the range was crossed, it gives a compiler error, stating there will be loss of information.
hope i am right,
bhuvana
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bhuvana is right.
When you assign an int to a byte, as long as the int is within the range acceptable for a byte (which is -128 to 127), no explicit cast is required. If it is beyond this range, an explicit cast is required.
HTH
 
sean cee
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sean
When you convert an int into a byte type narrowing conversion takes place without any loss of data. Further I somewhere read that conversion between primitive data types happens by implecit type casting i.e. the compiler does the casting for you.
 
Corbin Dallas
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The replys are good, but i don't feel it's answered yet.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When you do a "narrowing conversion" of a "non-final" primitive data type, then, the compiler gives an error!
For eg.,
short to byte
int to byte
long to byte
int to short
long to short
all give compiler errors
But then,
final int to byte
final int to short
work fine.
But,
final short to byte doesn't work!(tho' the non-final form of this works!!)
Does this work in 1.3 version? Or is it something to do specifically with final short?
Thanks,
Aparna
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variable assigments...
Byte to Short is fine as this is a widening conversion.
Short to Byte is not ok. Remember, all narrowing conversions between char, short and byte require a cast.
Only integer literals or integer final variables can implicitly and narrowly convert to byte, short or char if in range of the destination type.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer is simple. When the compiler can be 100% sure of the number you put in a byte, all assignments can be done compile time and casting will be done automatically.
"byte b=10" will compile because the compiler implicitly casts between the int 10 and the byte because it knows that no loss can occur. Since final variables are like conatants, the compiler knows that whenever a final int has gotten a value, it can be treated as a literal from then on. When the int is not final this should now work because the compiler doesn't feel confident the value will still be 10 by the time it gets to the byte assignment.
You should try to put the final int statement AFTER the byte assignment. This should work as well!
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic