• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int or Integer

 
Saurabh S Jain
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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?
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why on earth did you use Floats in the first place?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic