3

I understood that Strings in java are not normal variables like int or float, it is object but my question is why was it necessary to do this? why string could not be normal variable like sequence of characters? what was that thing that made java designers to make string as object?

thisisbhavin
  • 344
  • 3
  • 15
  • 2
    It's not necessary, just useful, `String` holds information about its state and nicely groups methods methods together such as `String.length`, `String.contains`, `String.hashCode` etc... It might also be worth noting that you can just use a char array but you'll lose a lot of the utility of `String`. – George Jan 21 '18 at 06:55
  • You can find good discussion about String https://stackoverflow.com/questions/2009228/strings-are-objects-in-java-so-why-dont-we-use-new-to-create-them – Ravi Jan 21 '18 at 07:00
  • Possible duplicate of [Why is String immutable in Java?](https://stackoverflow.com/questions/22397861/why-is-string-immutable-in-java) – roottraveller Jan 21 '18 at 07:04
  • @George just what I wanted to know. we can create strings the traditional way using char array but then we wont get to use predefined methods that calculate length,gives characters at some location and so on right? – thisisbhavin Jan 21 '18 at 07:05
  • @bhavindhedhi From a practical stand point, yes. There are other minor reasons too, `String` s are optimised by using a string intern pool which leads to memory optimisation, as well as performance optimisations by way of the `String` objects being immutable/unchanging. – George Jan 21 '18 at 07:18
  • Have we persuaded/convinced you of the importance of String being an object? – Allan Jan 21 '18 at 11:34
  • 1
    @Allan its the comment that helped me! – thisisbhavin Feb 28 '18 at 07:57

2 Answers2

4

I would say that there are several reasons that are close to the ones for which wrappers does exist for int/byte/double/... in Java:

  • To allow null value
  • To include it in Collection
  • To have access to the power of generic and polymorphism as an Object along with other Objects
  • To have all the wonderful methods of the class String to manipulate Strings objects
  • To have the String pool working and saving memory (would be difficult if Strings where on the stack and not in the heap)
  • etc.

Links:

Allan
  • 12,117
  • 3
  • 27
  • 51
4

The main difference between String and other primitive types (like int) is that its values need variable amount of memory depending on the string length. This makes it difficult to keep them on the stack.

On the other hand we have the string concatenation operator s1 + s2 and string literals "abc", which makes it different from any other object.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • yeah but actually since Strings are immutable they are fixed size upon creation, therefore they could also be saved on the stack as it is possible for a char array – Allan Jan 21 '18 at 07:13
  • 1
    @Allan String variables are not immutable, e.g. `s="a"; s="abc"` how much memory would you allocate for `s`? And a char array is not on the stack either. – Henry Jan 21 '18 at 07:15
  • String variables **are** immutable: `s` is not an object but a reference to an object! therefore you can change it and make it point to a new string object `abc` however I agree with you for the memory allocation, therefore what is done in c++ when you work with char arrays is to have a MAX fixed size and use `\0` to find the real size of your char array if you want to use them on the stack of you want to have a dynamic allocation then you need to go for the heap obviously – Allan Jan 21 '18 at 07:29
  • 1
    @Allan It is the string **object** that is immutable, a variable of type `String` can be changed (independent if it contains a reference to a String object or hypothetically a primitive string value). But I think we mean the same. – Henry Jan 21 '18 at 07:45
  • yeah not string variable but string object! :-) Anyway, have a nice Sunday :-) – Allan Jan 21 '18 at 07:48