Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, should Integer object be preferred over int primitive (same for other numeric types)?

Ok, so I understand that Integer is simply a wrapper class. however my concern is that avoiding to use a "wrapper", there might be a micro-optimization in execution time when using primitive ints variables.

My question is regarding, is really Integer object the one we should prefer to use, specially in programs which are required to have great performance(with great I mean, heavy duty, O(N^n) algorithms, the ones that take days).

Also, same case for double vs Double, float vs Float , etc.

like image 415
Jorch914 Avatar asked May 11 '15 18:05

Jorch914


People also ask

Is it better to use Integer or int in Java?

In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.

Is it better to use Integer or int?

int provides less flexibility as compare to Integer as it only allows binary value of an integer in it. Integer on other hand is more flexible in storing and manipulating an int data. Since Wrapper classes inherit Object class, they can be used in collections with Object reference or generics.

Why do we use a number class object over primitive data?

There are three reasons that you might use a Number object rather than a primitive: As an argument of a method that expects an object (often used when manipulating collections of numbers). To use constants defined by the class, such as MIN_VALUE and MAX_VALUE , that provide the upper and lower bounds of the data type.

Is Integer a primitive data type in Java?

Primitive Data Types. The eight primitives defined in Java are int, byte, short, long, float, double, boolean and char. These aren't considered objects and represent raw values.


2 Answers

You should prefer using the primitives whenever you can. Otherwise they wouldn't exist. The developers of Java even made extra effort in developing (for Java 8) Streams that support primitive types (IntStream, LongStream, DoubleStream), so you won't have to pay the performance penalty of multiple boxings and unboxings that you pay when using Streams of reference types for wrapper classes.

The wrappers are only for cases in which you have no choice (for example, you can't put primitive types directly in a Collection).

like image 181
Eran Avatar answered Sep 20 '22 06:09

Eran


A wrapper class instance takes more memory (wrapped value + reference), and generates some method calls where the primitive types only perform basic operations. However, some mechanisms tend to reduce this overhead (for example, Integer instances between -128 and 127 are kept in a pool if not declared using new). The difference is probably slight, but where you can use primitives, do it simply by principle : don't use classes that provide more features that you need.

like image 42
Dici Avatar answered Sep 20 '22 06:09

Dici