15

Possible Duplicate:
When we have wrappers classes, why primitives are supported?

If there are Wrapper classes which make Java pure object-oriented language, then why are there Primitive datatypes which can be used in Java???

Community
  • 1
  • 1
user2003432
  • 181
  • 1
  • 4
  • check here http://stackoverflow.com/questions/6151497/why-java-is-not-pure-object-oriented-language – PermGenError Jan 23 '13 at 10:46
  • @GanGnaMStYleOverFlowErroR: It does not explain *why* there are primitive types. – Felix Kling Jan 23 '13 at 10:47
  • 2
    There are [rumours that primitives will disappear in Java 10](http://java.dzone.com/articles/oracle-discusses-features-java). – assylias Jan 23 '13 at 10:50
  • Actually, I heard there is some debate going on about eliminating primitive data types starting with Java 10. – Radu Murzea Jan 23 '13 at 11:16
  • Here you can read more about primitive type. http://stackoverflow.com/questions/12670817/java-is-not-purely-object-oriented-what-does-this-mean-w-r-t-primitve-types http://stackoverflow.com/questions/12836522/java-is-pure-object-oriented-or-not – SANN3 Jan 23 '13 at 10:46
  • The link doesn't give me answer for why are there Primitive datatypes exist if we already have wrapper classes in Java?? – user2003432 Jan 23 '13 at 10:48
  • @user2003432 Check this link http://stackoverflow.com/questions/12670817/java-is-not-purely-object-oriented-what-does-this-mean-w-r-t-primitve-types – SANN3 Jan 23 '13 at 10:51
  • @GanGnaMStYleOverFlowErroR No, wrapper classes such as `Integer` etc. have existed since Java 1.0; they were not new in Java 1.4. – Jesper Jan 23 '13 at 10:55
  • these links only give "WHAT ARE PRIMITIVE DATATYPES" and i want to know "WHY PRIMITIVE DATATPES" – user2003432 Jan 23 '13 at 10:56
  • @Jesper oops, seems like i was confusing b/w autoboxing(which was introduced in 1.5) with wrapper class's , thanks though :) – PermGenError Jan 23 '13 at 10:59

3 Answers3

34

For efficiency. Variables of primitive types contain the value directly; variables of non-primitive types are references, referring to an object stored somewhere else in memory.

Each time you need to use the value of a wrapper type, the JVM needs to lookup the object in memory to get at the value. This isn't needed for primitive types, because the variable contains the value itself, instead of a reference to an object that contains the value.

However, that doesn't explain why primitive types need to be explicitly visible in the Java programming language. The designers of the Java language and the JVM could have chosen to hide primitive types from the language itself, so that you could treat everything as an object; the compiler could then translate it under the covers to more efficient primitive types.

Some newer programming languages that run on the JVM (Groovy, Scala and others) let you do exactly that: in the language itself everything looks like an object, which you can for example call methods on, but below the covers the compiler translates them to primitives.

I guess that in the time the Java language was developed (in the first half of the 1990's) people didn't think of that, and now it's too late for a radical change in the language to allow this.

Jesper
  • 202,709
  • 46
  • 318
  • 350
7

The main reason primitive data type are there because, creating object, allocating heap is too costly and there is a performance penalty for it. As you may know primitive data types like int, float etc are most used, so making them as Objects would have been huge performance hit. So Java designers thought it would be better to make it as non-objects. And yes, wrappers exists in-case you are ready to compromise bit on performance but you require more facility of OOP. So in that case you can use wrappers. Hope this information helps you.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
  • *creating object, allocating heap is too costly and there is a performance penalty for it* => do you have a reference? There is a penalty but it is not that high. – assylias Jan 23 '13 at 10:55
  • For example, [Goetz states](http://www.ibm.com/developerworks/java/library/j-jtp09275/index.html) *The common code path for new Object() in HotSpot 1.4.2 and later is approximately 10 machine instructions*. – assylias Jan 23 '13 at 10:58
  • _The primitive types represent single values—not complex objects. Although Java is otherwise completely object-oriented, the primitive types are not. They are analogous to the simple types found in most other non–object-oriented languages. The reason for this is efficiency. Making the primitive types into objects would have degraded performance too much_ from Java the complete reference. – Pradeep Simha Jan 23 '13 at 10:59
  • @assylias, yes that penalty maybe less, but on context of creating more number of int, float wrappers, you can observe that penalty – Pradeep Simha Jan 23 '13 at 11:03
2

For performance reasons. In some other languages like SmallTalk types like int or char are also objects, and the methods can be invoked on them. This is more theoretically correct but current implementations run slower. Primitive types are compromise between purity and performance.

Where more necessary (null value possible or to use with collection framework), Java provides wrapper classes like java.lang.Integer and similar.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93