File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Wrapper classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrapper classes" Watch "Wrapper classes" New topic
Author

Wrapper classes

Adi Sharma
Ranch Hand

Joined: May 18, 2009
Posts: 33
I was reading in the SCJP book

All of the numeric wrapper classes provide an overloaded, static toString()
method that takes a primitive numeric of the appropriate type (Double.
toString() takes a double, Long.toString() takes a long, and so on) and, of
course, returns a String:

Double d= new Double(1.2);
String s= Double.toString(d);

My question is that if this overloaded method is static, shouldn't it accept a static variable only as the arguments. I mean that the Double variable d should be declared double.

Thank You
Aditya Sharma
Adi Sharma
Ranch Hand

Joined: May 18, 2009
Posts: 33
I mean Double d should be declared static

Am sorry
Aditya Sharma
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

My question is that if this overloaded method is static, shouldn't it accept a static variable only as the arguments.


I am not sure what you are asking. Are you saying that parameters of static methods should be static variables and not considered as local variables? Or are you saying that only values held in static variables should be passed to a static method? In either case, why?

Henry


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

Joined: May 18, 2009
Posts: 33
I got it. I am wrong. I did not think that through. I was thinking that a static variable should be passed only static variables as arguments.
But I am wrong. Thanks A lot.

You know I am a rookie at java and am just getting started with it. Please bear with my stupid questions


Thanks a zillion
Aditya Sharma
Adi Sharma
Ranch Hand

Joined: May 18, 2009
Posts: 33
Hi,

In the book it says:

In order to save memory, two instances of the
following wrapper objects (created through boxing), will always be == when their
primitive values are the same:
- Boolean
- Byte
- Character from \u0000 to \u007f (7f is 127 in decimal)
- Short and Integer from -128 to 127

I did not got the meaning of the phrase "created through boxing" in the second line.

Thanks
Aditya Sharma
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

I did not got the meaning of the phrase "created through boxing" in the second line.


In the future, please start a new topic for a new question.

Autoboxing is a mechanism where compiler will automatically generate wrapper objects for primatives, when objects are needed. And where the compiler will automatically extract a primative value from a wrapper object, when primatives are needed.

So, if you do something like this....



Technically, this is not legal, as you are trying to assign a primative int value to an Integer object. However, with autoboxing, the compiler will change the code to this...



Which basically, convert or "box" the primative 5, in a wrapper Integer, for you.

Henry
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

"Boxing" is the term for converting the primitive types to their Wrapper types (consider the primitive value being 'wrapped in a box' of the Wrapper type). For example, if I wanted an 33 to become an Integer, I would do this:

But actually, I wouldn't. Java supports "Autoboxing" which means that the compiler will take care of the 'boxing' operation for us a lot of the time. I can write:

And the compiler converts that to:


Steve
Adi Sharma
Ranch Hand

Joined: May 18, 2009
Posts: 33
Hi all,

Am sorry for mixing two topics of in one. Thanks for all the answers.

Aditya Sharma
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper classes