Why there is primitive type for integer(int) even though we have an object for integer as Integer? But the same is not with String type. There is no such primitive type for String. Always String deals with object reference?
4 Answers
Speed. It's much faster for machine code to add two int's using native CPU instructions, rather than having to take two Integer objects, extract the int values from them, then add those, creating a new result Integer object to contain the result. (how JNI maps primitives)
Strings are complex, have many methods, and as such have no machine code counterpoint. They are promoted to a true Object. Also, a String shares state with other Strings created with the same value. No primitive value shares state with other primitive values like this. (immutable can be shared | primitive no sharing)

- 2,167
- 1
- 18
- 24
-
2I would also add space. Less space to store an integer to memory, than a whole object with all its operations. – Alex Ntousias Jan 20 '10 at 05:11
This is because processors (CPUs) have direct support for integer types but not for strings. And for performace reasons Java supports some native types that are likely to be supported by the processors the JVM might run on.

- 13,877
- 6
- 48
- 58
A string datatype is considerably more complex than an int - there's a variable amount of memory to hold a string for one thing.

- 333,147
- 50
- 533
- 760
Ultimately the calculations being done at machine level language. I am wondering what makes the difference between primitives and objects in terms of performance. Elaborated answer will be appreciated.
-
this should be a comment - not an 'answer' please compete the SO tour here: http://stackoverflow.com/tour – David May 11 '15 at 05:15