This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes int or Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "int or Integer" Watch "int or Integer" New topic
Author

int or Integer

Saurabh S Jain
Greenhorn

Joined: Sep 15, 2012
Posts: 2
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

Joined: Aug 16, 2005
Posts: 14279
    
  21

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8060
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Why on earth did you use Floats in the first place?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int or Integer