Why doesn't Java have a primitive type for String when most of the other data types do?
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.
-
30+1 - And the corollary is that String is not primitive because it making it a primitive would not make it any more efficient. Jan 20, 2010 at 6: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 seeInteger 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!– AMDGMar 9, 2014 at 5: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.
-
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? Dec 12, 2013 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! Jul 23, 2015 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.
-
Does any language consider it to be a primitive? Would a fixed length in memory cause it to be one?– JeffMay 11, 2016 at 15:02
-
Strangely enough, in Javascript string is primitive. It can have fixed memory length as it is immutable. Jan 22, 2018 at 14:09
Object
which will contain a reference achar[]
,byte[]
, orInteger
[depending upon its length and whether it contains any non-ASCII characters]. Storing astring
to anObject
would convert it to aString
--a class containing a singlefinal
field of typestring
.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.