Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

boxing and widening doubt cant understand the logic for this program

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class BoxAndWiden
{
static void go(Object o)
{
Byte b2=(Byte)o;
System.out.println("b2");
}
public static void main(String args[])
{
byte b=5;
go(5);
}

}

reference kathy sieera book.Its first line said
that

byte b was boxed to Byte.....

now where is this boxing taking place?
 
Sandesh Hiremath
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are referring to Head First then you must have come across the sentence that JVM does automatic boxing on your behalf. If you are not referring that book then by now, its known to you. Yes, Java does automatic boxing (Wrap a varible inside a matching object). In this ex, when a byte value, a simple varible (5), is passed as a argument, it goes n sits in the Object type. This is the the place 'boxing' is done automatically. Then this Object is casted to Byte type object. Hope this helps.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic