Java API Doc :
String :Strings are constant; their values cannot be changed after they are created.
Stringbuffer:A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
> Java API Doc :
> String :Strings are constant; their values
> cannot be changed after they are created.
> Stringbuffer:A string buffer is like a String,
> but can be modified. At any point in time it contains
> some particular sequence of characters, but the
> length and content of the sequence can be changed
> through certain method calls.
This is true but it doesn't explain why String doesn't have a reverse method. String could have a method which would return a new String object which would be the reverse of another String.
> why String didn't have reverse method? but
> StringBuffer have...
Just because both classes deal with strings doesn't mean they automatically must have the same functionality. It doesn't follow.
They could have though. For example StringBuffer and StringBuilder do. Theye're "plug-in" compatible.
So there is no reason why. It's a whim of fortune. You can,
- file a request for such a method,
- find one on the internet or in a book,
- write one yourself,
- hire a consultant to write if for you,
- use StringBuffer/StringBuilder.
Make your pick and get used to this situation. You cannot expect to find everything you need to be handed to you without any effort of your own. Now let this be the last time you ask why it isn't so.
> The reverse method transform the orginal StringBuffer
> in another one (check API for exact explanation) that
> is why it's not available in String class as for a
> String object you can only read in it.
> Well that's what i guess with the documentation
Nothing prevents String from having a reverse method. It's just that it couldn't work exactly as reverse of StringBuffer does. In the String case a new object would have to be created,
> A new one is created too with the StringBuffer
Well if you thought that then your previous replies make no sense at all.
If the StringBuffer reverse would create a new object then it would behave exactly like how a String reverse would have to behave, and what you state in say #4 is total gibberish.