Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Does Java Support both Wrapper Classes & Primitive types?

 
Waez Ali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just want to know,

1) Why does Java support both int & Integer,
char & Charecter,
and so on........?

2) When should we use these classes?

Please let me know this concept.

Thank You
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ali,

'int' and 'char' are basic types whereas 'Integer' and 'Character' are classes.

You may find a detailed explanation here

Best regards,
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops ;-)

It seems that the link I have just provided forget to mention a noticeable point : basic types can't be used in association with Java Collections (ArrayList, Vector ...) whereas wrapper classes can. For example, you may declare a HashMap of 'Integer' but not a HashMmap of 'int' (as the Java collections work only on objects)

Best regards,
 
Waez Ali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Peter,

I was so confused but I got it now
You are just great.


Thank you

Waez Ali.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Primitive values should typically be preferred when doing extensive calculations. As mentioned, wrapper classes are helpful when you need to store primitives in a Collection. Since you cannot do this directly, the wrapper classes are necessary.

Layne
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cant use primitive types in Collections which form a major part of the Java programming language for managing the data structures.
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cant use primitive types in Collections which form a major part of the Java programming language for managing the data structures.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic