• 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

questions regarding auto box

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I happened to test the following code,

pulic class Test {
public static void main(String[] args) {

Byte bb = 12; // here
//byte bb =12;
bb+=2; // compile error
System.out.println(bb);
}

}


the above code got compile error, however, if change the bb from type "Byte" to "byte",it compiles ok, anyone can help to clarify why "unboxing" not working here? thanks.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Byte bb = 12; // here
//byte bb =12;
bb+=2; // compile error

for byte bb=12;
bb+=2; is converted to bb=(byte)(bb+2); by the compiler, as bb+2 return int, so implicitly type casting is done by compiler.

for Byte bb=12;
bb+=12; I do not know but, I think compiler must be doing
1) unboxing means bb=bb.byteValue()+12;
2) and bb.byteValue()+12; will return int, so bb=intValue; that is compile time error.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the original post and the reply to the post got a doubt to me that is
Byte bb= 12;
In the above statement is literal 12 considered as int literal or byte literal
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my knowledge

Byte b=12;

b+=2;------------line 2

here in line2 it is unboxing the byte value but after adding value 2 finally it gives int value which is assigned to byte variable thats why it is giving compilation error...
 
Sheriff
Posts: 9707
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
I think the statement is not working as the compiler is doing this



Byte bb = 12;
bb+=2; // compile error

will become

Byte bb = Byte.valueOf((byte)12);
bb = Byte.valueOf( bb.byteValue() + 2 );

now Byte.valueOf takes a byte parameter but bb.byteValue() + 2 will result in an int. So the call would look as Byte.valueOf(intVal); This is why it is giving a compilation error...
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Tharakan:
the original post and the reply to the post got a doubt to me that is
Byte bb= 12;
In the above statement is literal 12 considered as int literal or byte literal


All integral literals are of type int, unless you put an l or L at the end, and then the type is long. It just happens that in this case there is an implicit conversion going on. Punit talked about this in a recent post.



To make things more confusing:
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic