• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String is premitive datatype or reference datatype

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic