Reputation: 364
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?
Upvotes: 3
Views: 1072
Reputation: 43728
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.
Upvotes: 4
Reputation: 12438
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:
null
value Collection
Object
along with other ObjectsString
to manipulate Strings objectsLinks:
Upvotes: 4