• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why need Integer class ?

 
Tom Salo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic