Why There is support for Primitives data types when everything else in Java is Object . Although Java does provide Wrapper classes of all the primitives data types
-
1In the absence of an accepted definition of '100% object-oriented', this is not a real question and 'primarily opinion-based'. – user207421 May 06 '14 at 10:25
3 Answers
we use primitives because they are fast.. here is answer from Why do people still use primitive types in Java? post for quick reference
In Joshua Bloch's Effective Java, Item 5: "Avoid creating unnecessary objects", he posts the following code example:
public static void main(String[] args) {
Long sum = 0L; // uses Long, not long
for(long i = 0; i <= Integer.MAX_VALUE; i++) {
sum += i;
}
System.out.println(sum);
}
and it takes 43 seconds to run. Taking the Long into the primitive brings it down to 6.8 seconds... if that's any indication why we use primitives.
The lack of native value equality is also a concern (.equals()
is fairly verbose compared to ==
)
for biziclop:
class biziclop {
public static void main(String[] args) {
System.out.println(new Integer(5) == new Integer(5));
System.out.println(new Integer(500) == new Integer(500));
System.out.println(Integer.valueOf(5) == Integer.valueOf(5));
System.out.println(Integer.valueOf(500) == Integer.valueOf(500));
}
}
Results in:
C:\Documents and Settings\glow\My Documents>java biziclop
false
false
true
false

- 1
- 1

- 2,180
- 1
- 12
- 19
-
I would question if object `==` is missing. It just means something different to `equals()`; absolutely and faithfully the same object. I don't use it often but I wouldn't want it removed from the language – Richard Tingle May 06 '14 at 10:22
-
How is `Integer.valueOf(5) == Integer.valueOf(5)` true and `Integer.valueOf(500) == Integer.valueOf(500)` false? – Moak May 06 '14 at 10:30
-
2@Moak `Integer.valueOf()` provides an `Integer` of that value, not an `int'. However it caches small values so it truely is the same `Integer` – Richard Tingle May 06 '14 at 10:38
-
+1 @Richard, let me more clear about the range. Integer class caches values in the range -128 to 127. – niiraj874u May 06 '14 at 10:48
Because of autoboxing.... http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html. You will find your answer there.

- 31,590
- 6
- 57
- 99
Another explanation, taken from here:
Not everything in Java is an object. There is a special group of data types (also known as primitive types) that will be used quite often in your programming. For performance reasons, the designers of the Java language decided to include these primitive types.
Creating an object using new isn't very efficient because new will place objects on the heap. This approach would be very costly for small and simple variables. Instead of create variables using new, Java can use primitive types to create automatic variables that are not references. The variables hold the value, and it's place on the stack so its much more efficient

- 1,429
- 1
- 19
- 34