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

casting doubt

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all!
byte b=16;
//ok as the source value is well with in the range of byte
now consider the case
Byte byteobj=new Byte((byte)16);
why do we need to go for type casting in the above line???
Please explain
Regards
Lusha
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
16,by default is int
and int is not an argument of Byte constructors (it takes byte and String) so,you need to cast 16 to byte before
create new instance of Byte
 
lusha tak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nut why don't we require a cast in th eline one.
i.e.
byte b=16//works ok even without cast
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works finr if u send variable b to the constructor.
i.e. Byte obj = new Byte (b);
But when u write 16, it's a integer literal by default.So the cast is required as mentioned in the second post
 
lusha tak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all!
Lusha
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte b=16; works because even though 16 is an int, the compiler knows that 16 can fit in a byte. This doesn't work:
int a=16;
byte b=a;
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then look at the code:
int i= -20;
final int j=20;
final int k=i;
byte b1=j; //works
byte b2=(byte)i; //cast required
byte b3=(byte)k; //cast needed
Why is cast not required when source is final int? i. e. can you explain about b1 and b3?
Thanks.
[This message has been edited by jayu jadhav (edited July 18, 2001).]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jayu,
I don't think that your statement is correct.
Consider the following code:

the b variable will need to be cast explicitly into byte, otherwise it'll give a compiler error.
After running javap -c on the class, here's what I got:
0 bipush 41
2 istore_1
3 sipush 129
6 istore_2
7 bipush 11
9 istore_3
10 bipush -127
12 istore 4
14 iload_3
15 i2b
16 istore 5
18 return
please note the difference in line 0 and line 3, where:
final int a=41
will be stored using bipush (assumably byte-push), while:
final int b=129
will be stored using sipush (assumably short-push)
another interesting thing to note is when you explicitly cast a final variable then it'll do the push command, but if you explicitly cast a non-final variable then it'll try to load & convert it..
it's much like assembler where you have macro and compiler simply just replace all instance of "final variable" with the value assigned.
correct me if i'm wrong.
- eric
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the final variable is within a range that does not cause loss of precision then the compiler will allow it. The reason is that the compiler can treat the final variable as a constant since it knows that it will never be changed.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
reply
    Bookmark Topic Watch Topic
  • New Topic