• Post Reply Bookmark Topic Watch Topic
  • New Topic

String is premitive datatype or reference datatype  RSS feed

 
abhinas raj
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need a good explanation of this question. String is primitive datatype or reference datatype.
1) perhaps it is reference datatype. [but i need reason]
2) some where i seen that, Strings behave like a primitive because they are immutable [i could not understand this] please help me guys
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are objects, and String is not a primitive type.

Class String is indeed immutable, which means that once you've created a String object, you cannot change its contents. But that does not have anything to do with it being a primitive type or reference type. Primitive types are not immutable, the sentence "Strings behave like a primitive because they are immutable" does not make sense.

Java has a fixed set of primitive types: byte, short, int, long, char, boolean, float, double

All other types are reference types.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote: . . . Primitive types are not immutable, . . . .
I would prefer to say that trying to impute mutability or immutability to primitives doesn't make sense either. Can't remember what the JLS says, however.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!