• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

premitive types & Wrapper clases

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the difference between
int i=2;
and
Integer i=new Integer(2);
In the first statement, i is an object of Integer class or not ?
In the second statement, we are explicitly creating an object of Integer class , What is the use of second statement ? Which of them is more advantageous and why ?
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello sam,
-1) in first line 'int i =2;' i is not an integer object. it is a simple int type variable as we have in c, basic or others.
we can't call any methods on it coz it's not an object.
-2) in second case Integer i = new Integer(2); i is an object of integer type. although it stores the same int value as its primitive and nothing else. but diff. lies here.
u can call methods on it. like i.intValue() & many more are there.
Now When shall we use primitive & when Wrapper class.
so
-) use primitive type whenever u don't need to call any methods & for simple needs & when u want to pass them to
a function & don't want to get it changed during function call.

-) u may need to use wrapper class when u want to alter the variable from many places. since a reference of wrapper will
be available. so u can pass it to a function & the function will alter its value using reference.
in another case u may want to use few integers in a vector but vector can store only objects & no primitive type
so here u will need wrapper class.
hope it clears things.
regards
deekasha


 
Sam
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-) use primitive type whenever u don't need to call any methods & for simple needs & when u want to pass them to
a function
& don't want to get it changed during function call.
-) u may need to use
wrapper class when u want to alter the variable from many places. since a reference of wrapper will
be available. so u can pass it to a function & the function will alter its value using reference.
Hi Deekasha,
Yeah !! most of the things get cleared.
But You mentioned that we should use premitives when we don't want its value to get changed during the program execution. But, I think the other way; - we should use the primitives when we want its values to get changed during program execution.
You also mentioned we should use wrapper classes when we want to alter the values of the object of wrapper class. Deekasha, its not possible !! Bcoz , all wrapper classes are final and the values of their objects cant be changed at a later stage in the program.
Correct me if I am wrong.
Regards,
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam, you are correct - all the wrapper classes are immutable, like Strings, and so if you want to be able to chage a value, the wrapper class won't let you. Of course you can create a new wrapper object and reassign the reference...
The main use for the wrapper classes I think is so that you can store primitives in Vectors, Lists, Maps and the like. These are all set up to store Objects, not primitives - if you want to store primitives in them, you have to wrap the primitives in wrappers. E.g.
<code><pre> Vector v = new Vector();
v.add(new Integer(1));
v.add(new Integer(2));
v.add(new Integer(3));
v.add(new Integer(4));

...

for (int i = 0; i < v.size(); i++) {
System.out.println(((Integer) v.elementAt(i)).intValue());
}
</pre></code>
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic