• 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

int or Integer

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is best to use
wheather int or Integer, float or Float, Should we define Wapper class type variable?

I used Float in pojo, etc and find its better to use wapper class type variable in place of native data type
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prefer primitive types: int, float etc. instead of wrapper types: Integer, Float etc., because they are much more efficient. Instances of wrapper classes are ofcourse objects, which take up more memory than primitives.

Ofcourse sometimes you have to use wrapper types. For example, the Java collection classes can only store objects, not primitives - an ArrayList<int> is impossible, you'll have to use ArrayList<Integer>.

Saurabh S Jain wrote:I used Float in pojo, etc and find its better to use wapper class type variable in place of native data type


What are your reasons to think that using wrapper classes is better than using primitive types?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saurabh S Jain wrote:Which is best to use
wheather int or Integer, float or Float, Should we define Wapper class type variable?

I used Float in pojo, etc and find its better to use wapper class type variable in place of native data type


It really depends on what you're doing. In some cases you have no choice: For example, you cannot set up a List<int>; it has to be List<Integer> (or, possibly even better, List<AtomicInteger>).

If you're simply using the class as a fixed value then it really doesn't make much difference. On the other hand, if you need to modify it or use it for arithmetic then, as Jesper said, a primitive is much better.

Winston
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why on earth did you use Floats in the first place?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic