File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper classes

 
Adi Sharma
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean Double d should be declared static

Am sorry
Aditya Sharma
 
Henry Wong
author
Marshal
Pie
Posts: 20833
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Adi Sharma
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20833
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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:

 
Adi Sharma
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

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

Aditya Sharma
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic