Why doesn't Java have a primitive type for String when most of the other data types do?
-
5Ugh. I wish there were no primitives at all to be honest with you. – CookieOfFortune Jan 20 '10 at 05:46
-
Basically the same question you asked here: http://stackoverflow.com/questions/2099171/integer-as-primitve-type – David Jan 20 '10 at 05:50
-
Did you really mean, why is there no "value-type" object for string instead of primitive type? (i.e. how do you put a string on the stack versus the heap?) – BrainSlugs83 Jul 29 '13 at 22:25
-
@BrainSlugs83: By having it hold information sufficient to identify a sequence of characters which may be stored elsewhere [possibly, but not necessarily, on the same heap as other objects]. As one simple variation, have it hold an `Object` which will contain a reference a `char[]`, `byte[]`, or `Integer` [depending upon its length and whether it contains any non-ASCII characters]. Storing a `string` to an `Object` would convert it to a `String`--a class containing a single `final` field of type `string`. – supercat Dec 09 '13 at 22:37
-
@BrainSlugs83: Having `string` as a primitive could have reduced the number of objects that need to be manipulated when working with strings and allowed a GC to be implemented in substring-aware fashion (if string contents were stored in a special "strings only" heap which the GC managed along with the "ordinary" one). It would also have allowed `==` to operate on string *contents* the way `+` does. – supercat Dec 09 '13 at 22:41
-
But you're not treating it any different than an object then (i.e. pointer goes on the stack, object in the heap)... -- Aren't all of the other primitives in Java value types? Perhaps, my question is silly, if all of the other primitives in Java are value types, then the two would be synonymous? -- the accepted answer even uses "primitive" to specifically mean "non-object"... -- also, if it was a value-type, wouldn't == work anyway? -- I suppose you could make this work manually -- just store all your strings in a big array, and pass around integers... then == will work (for exact matches). – BrainSlugs83 Dec 12 '13 at 20:35
3 Answers
String is an object, it isn't a primitive type at all, just an array of chars. The reason why primitive types exist in Java at all is an interesting one, excerpt from a James Gosling interview:
Bill Venners: Why are there primitive types in Java? Why wasn't everything just an object?
James Gosling: Totally an efficiency thing. There are all kinds of people who have built systems where ints and that are all objects. There are a variety of ways to do that, and all of them have some pretty serious problems. Some of them are just slow, because they allocate memory for everything. Some of them try to do objects where sometimes they are objects, sometimes they are not (which is what the standard LISP system did), and then things get really weird. It kind of works, but it's strange.
Just making it such that there are primitive and objects, and they're just different. You solve a whole lot of problems.
So in short the primitive types exist for efficiency reasons.

- 302,674
- 57
- 556
- 614

- 62,090
- 32
- 125
- 150
-
30+1 - And the corollary is that String is not primitive because it making it a primitive would not make it any more efficient. – Stephen C Jan 20 '10 at 06:03
-
1A very good reason to have primitive types indeed! It sure saves time to just make an int than go waste precious seconds(approximately 10) making an instance `Integer foo = new Integer(aValueFoo);`. t'would be messy to see `Integer foo Integer foo Integer foo Integer foo...` lining your code in my opinion. Integer has its uses when it comes to bigger things to do with integers... primitives are a blessing! – AMDG Mar 09 '14 at 05:54
int, char, float, double, etc. all have a fixed length in memory. e.g. a int have 4 bytes, thus 32bits.
but a string can have different length, it is actually an array of char.

- 16,980
- 13
- 75
- 117
-
1Well, that's more of a reason for saying why it should be an object-type (i.e. a class) and not a value-type (i.e. a struct) -- the question is, why is it not a primitive? -- In Java are all of the primitives value types? -- Are there no structs in Java? – BrainSlugs83 Dec 12 '13 at 20:37
-
1The ONLY logical answer found to a straight question: Because of memory allocation issues. an int or a float type requires a specific memory size, but a string (sorry, String) is of a variable length made up of a undetermined (at declaration time) length of memory. Period. Thank you so much! – alejandrob Jul 23 '15 at 14:44
Most programming languages don't consider a string primitive because it's actually an array of characters. Primitive types almost always have a fixed size.
I should say though that some people might consider String to be "primitive" because it is built-in. But it's not primitive in the sense of being a basic type as opposed to a composite type. Because a string is an array of characters, it is a composite type.

- 10,309
- 2
- 30
- 41
-
Does any language consider it to be a primitive? Would a fixed length in memory cause it to be one? – Jeff May 11 '16 at 15:02
-
Strangely enough, in Javascript string is primitive. It can have fixed memory length as it is immutable. – Vojtech Ruzicka Jan 22 '18 at 14:09