Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Why need Integer class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why need Integer class ?" Watch "Why need Integer class ?" New topic
Author

Why need Integer class ?

Tom Salo
Greenhorn

Joined: Oct 28, 2002
Posts: 7
(int) VarName // Standard Type Casting
(Integer) VarName // Is it still casting ?
Why do we need (Integer) class ?
When do we apply with it ?
Example please.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Tom
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!
Tom, a more appropriate question is why are there primitives if Java is supposed to be an object oriented language. The primitives are there to speed things up and allow the compiler to make optimizations. In a pure object oriented language there qwould be no primitives and you would only have the objects (Integer, Double, Boolean, etc. ).
A big use is in collections that can only hold objects you can not put an int there but an Integer works just fine.
hope that helps


Dave
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
All of the primitive types have their own 'wrapper' class so that whenever you want to treat any of your primitives like an object you can 'wrap' them in an object.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
You have to use instead of primitive type (int,
float, double,...) wrapper classes if you use
it with Vector, Hashtable, ArrayList, Map, ... etc. Because in standard Java no exist Vector of
int, only Vector of Object.
(Of course exists a lot of non-standard implementation of int vector, because it's
important sometime to optimize code; wrapping isn't time and memory effective).
----------------------------------------------
As I understand will be in java 1.5 template
construction like Vector<...> is only syntax sugar. (I read somewhere that it will be the same
container library: Vector, Hashtable... for template and non-template syntax usage)
And doesn't solve the problem.
Am I right ?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why need Integer class ?