wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes getting confused Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting confused" Watch "getting confused" New topic
Author

getting confused

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

class Xxx
{
static void go(Object o)
{
Byte b2=(Byte)o;
}
public static void main(String args[])
{
byte b=5;
go(b); // how is boxing done here.I have'nt even created a Byte wrapper class???
}
}


http://www.lifesbizzare.blogspot.com || OCJP:81%
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

// how is boxing done here.I have'nt even created a Byte wrapper class???


That's what autoboxing means... the compiler will generate code that will wrap and unwrap primitives to their counterpart class automatically. You don't have to write the code that create the Byte wrapper class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
here is how it works.


You know byte can be boxed to Byte which extends Object.

hence byte can be passed to Object through Byte


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

ok Mr. Henry Wong

does that mean when i declare a varible int will it create its wrapper class for no reason???


class xxx
{
public static void main(String args[])
{
Int i=10 // is it autoboxing in Integer wrapper class
}
}


PS: how to upload our pics in our account of javaranch
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Vishal Hegde wrote:ok Mr. Henry Wong

does that mean when i declare a varible int will it create its wrapper class for no reason???


class xxx
{
public static void main(String args[])
{
Int i=10 // is it autoboxing in Integer wrapper class
}
}


No it won't this case but if you declare it as Integer i=10 then autoboxing comes into play

Autoboxing comes into play whenever you want to represent primitive as object .e.g. collections needs object not primitive so if you add any of the primitive into collection then it will be automatically converted into its own Wrapper type.

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

ok thanks Mr siva for your help can you please help me to sort out the difference between

Integer i=10;

and

Integer i=new Integer(10);
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
You know all the wrapper classes are immutable.That is once its value is assigned you can not change it .

e.g.

Integer i=10;
i++;

It seems to be contradict to what i say inthe first line.

here is how it works.

i++ is nothing but i=i+1;

So when the above step is executed i value is changed to 11 and since Integer is immutable it can not change the value of the object that holds 10 so instead it creates new object with value 11 and make the i reference to points to this object and hence the object with value 10 is now eligible for garbage collection.

This is done in order to improve memory efficiency.

So if you create Integer i=10;
Integer j=10;

then it won't create two object with value 10 instead it creates only one object with value 10 which has two reference i and j.

But if you use new Integer.No matter what JVMhas to create new Object even if they have same value which is generally not advisable.

Also there are some restrictions on this too.

The above conditions works well for these scenarios.(Integer i=10)

Byte,Boolean --- If they have same value then no new object will be created.

Character---if the value is 0000 to 007f then no new object will be created but if it is greater than 007f then,even though they have same value new objects will be created.

Short,Integer---if value is between -128 to 127 then no new object else create new objects for each reference.

hope it clears your doubt now.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
You should by now know that a thread title like "getting confused" simply gets everybody confused.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

sorry Mr Campbell Ritchie damn soory will see that i write the title thread in a proper manner and thnks Mr Siva i got it atlast
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Don;y say Mr Siva......toooo professional.....

I like those who call me by just name.....Siva.......see how beautiful is that
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Ok siva thanks man for that stuff you wrote reaaly cleared my doubt
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getting confused