10

This question came to my mind because I have read somewhere that Java is not a pure Object oriented language since it is using primitives (which are not objects). I can agree with that. Now my problem is why we are using primitives/wrappers while we already have Object in same type?

As an example if we consider Integer, It has same value limit as int other than object behavior. why still Java use primitives under these condition?

As my opinion, if Java only use Object type Autoboxing and Unboxing no need. Also there is no primitive for String by the way.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • 3
    related: http://stackoverflow.com/questions/3257967/primitives-types-in-java?rq=1 –  Aug 20 '13 at 11:21
  • 1
    Please see: [Why do people still use primitive types in Java?](http://stackoverflow.com/questions/5199359/why-do-people-still-use-primitive-types-in-java) – Jost Aug 20 '13 at 11:22
  • 1
    This is a discussion question, which makes it off-topic for StackOverflow. – Ernest Friedman-Hill Aug 20 '13 at 11:22
  • ohh please I am really want to know why? please leave a comment when you downvoting. Point me out what is your point – Ruchira Gayan Ranaweera Aug 20 '13 at 11:23
  • The wrappers were in the language all along, that's not the point. The point is efficiency. – Marko Topolnik Aug 20 '13 at 11:28
  • That was come from languages like C/C++. The primary case was to make programs compatible with those languages. – Roman C Aug 20 '13 at 11:29
  • Java is not compatible with either C or C++. Not even remotely. – Marko Topolnik Aug 20 '13 at 11:31
  • @MarkoTopolnik Java is just a language, or platform whatever you think, but it will always lack in performance gained by native languages. – Roman C Aug 20 '13 at 11:33
  • @RomanC I disagree. Yes, C can by definition always outperform Java (since Java is implemented in C), but the dominant factor to performance is programmer capabilities, and most don't qualify as "brilliant". If you take the user into account, Java can be just as fast as C. – Has QUIT--Anony-Mousse Aug 20 '13 at 11:36
  • IMHo, the dominant factor to program nowadays is performance, but the price is so high it could leverage other factors including *parogrammer capabilities* which never been important so far as it's always available. – Roman C Aug 20 '13 at 11:41
  • 1
    @Anony-Mousse There is a stronger point in favor of Java: C is compiled into static machine code, whereas Java is profiled and optimized at runtime. Many JIT optimizations are impossible to do ahead of time. So there are use cases where the Java model can actually beat C. – Marko Topolnik Aug 20 '13 at 11:41
  • 1
    @Ruchira Today it mostly comes down to heritage. An `Integer` could be internally optimized into a primitive `int`, given a sufficiently smart runtime. But, the Java designers didn't dare to count on the existence of such sufficiently smart runtimes, and probably rightly so. – Marko Topolnik Aug 20 '13 at 11:49
  • @MarkoTopolnik this is not correct. An `Integer` is only optimized into an `int` if the object creation can be *inlined*. A `HashMap` comes at a *massive* memory cost compared to e.g. Troves `TIntIntMap`. Since `Integer` can be `null`, this cannot be simply fixed unless you use 33 bit integers... – Has QUIT--Anony-Mousse Aug 20 '13 at 12:16
  • When Java is slower than C, it is usually because the programmer doesn't understand memory management in Java. He then would also not understand memory management in C either, though... – Has QUIT--Anony-Mousse Aug 20 '13 at 12:17
  • @Anony-Mousse You missed my point. I said it *could* be optimized, if the Java designers took that choice. I was not talking about Java as it is, but as it might have been. Compare with LISPs, which routinely have this kind of optimization built-in. Given your mention of 33 bits, you are probably already aware of what I had in mind. – Marko Topolnik Aug 20 '13 at 12:19

1 Answers1

10

One reason is due to memory usage. Primitives, such as int, float etc. require less memory allocations (I think 4 bytes) in comparison to Objects which are at the very least 8 bytes. Please see the following reference:

In addition, a lot of arithmetic (numeric) is completed with the use of primitives rather than their Object equivalents and this is another reason why they are quite critical in the Java language.

blackpanther
  • 10,998
  • 11
  • 48
  • 78
  • 1
    Why then String not has a primitive ? – Ruchira Gayan Ranaweera Aug 20 '13 at 11:26
  • 3
    Because strings are not primitive types of the CPU. Characters are, to some extend. The overhead of a `String` object compared to a `char[]` is neglibile. – Has QUIT--Anony-Mousse Aug 20 '13 at 11:27
  • 2
    In Hotspot, an `Integer` needs 16 bytes. An `int` is 4 bytes. They also do *not* have the same semantics, because an `Integer` can be `null`, an `int` cannot. – Has QUIT--Anony-Mousse Aug 20 '13 at 11:29
  • @Anony-Mousse `char[]` is no less an object than a `String`. Java doesn't have array-valued variables. – Marko Topolnik Aug 20 '13 at 11:29
  • @MikkelLøkke char isn't a primitive type for String – Ruchira Gayan Ranaweera Aug 20 '13 at 11:30
  • @MarkoTopolnik I didn't claim otherwise. Since it doesn't save you anything to make it otherwise, arrays were also made `Object`s. They aren't primitive after all. But e.g. you cannot subclass it IIRC. – Has QUIT--Anony-Mousse Aug 20 '13 at 11:33
  • @Anony-Mousse But arrays in general *can* be considered primitive at the hardware level. The main difference here is that in Java, you can`t avoid the object overhead when dealing with arrays. In C, you can have an array-valued variable, or a pointer to a dynamically allocated array. In Java you don't have that choice. – Marko Topolnik Aug 20 '13 at 11:39
  • @Ruchira I didn't say it was. I said char[] was. If you look at the source for java.lang.String, you'll find that it's backed by a char[]. In exactly the same way that a java.lang.Integer is backed by an int. – Mikkel Løkke Aug 20 '13 at 11:41
  • @MikkelLøkke It is not exactly the same because the `Integer` instance contains the `int` *value*, whereas the `String` instance contains just a *pointer* to another object, which is the `char[]`. In fact, a better analogy is between `char[]` and `char`. – Marko Topolnik Aug 20 '13 at 11:45
  • @MarkoTopolnik It's also not the same because of string interning (aka. the Java String Pool), Strings being immutable, etc. But for the purpose of this (non technical) discussion, it is accurate. I suppose a technical reason for there not being a primitive type for String, is because a String in Java, is by definition not a primitive type. It's the same reason why there isn't a primitive type for Maps, Lists, Tuples or even Arrays. – Mikkel Løkke Aug 20 '13 at 11:52
  • @MikkelLøkke Interning has little to do with this story, it's just caching. The main point about primitive vs. object is dynamic vs. static allocation of memory. Put it another way, a primitive is an integral part of its containing data structure, whereas an object never is---it is a separately allocated structure which must be pointed to. The ramifications of that are far-reaching (memory fragmentation, memory overhead, dereference overhead, CPU cache issues, etc.). – Marko Topolnik Aug 20 '13 at 11:57
  • Java deals with Objects. If you want to add primitives to Collections then you cann't , you need to write a wrapper class for that. Readymade Classes are made for that – Prabakaran Sep 08 '15 at 07:12