• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why Does Java Support both Wrapper Classes & Primitive types?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Peter,

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


Thank you

Waez Ali.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic