37

Why doesn't Java have a primitive type for String when most of the other data types do?

6
  • 5
    Ugh. I wish there were no primitives at all to be honest with you. Jan 20, 2010 at 5:46
  • Basically the same question you asked here: stackoverflow.com/questions/2099171/integer-as-primitve-type
    – David
    Jan 20, 2010 at 5: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?) Jul 29, 2013 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 9, 2013 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 9, 2013 at 22:41

3 Answers 3

62

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.

2
  • 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, 2010 at 6:03
  • 1
    A 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 9, 2014 at 5:54
19

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.

2
  • 1
    Well, 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
  • 1
    The 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, 2015 at 14:44
6

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.

2
  • Does any language consider it to be a primitive? Would a fixed length in memory cause it to be one?
    – Jeff
    May 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.