about reverse method.

why String didn't have reverse method? but StringBuffer have...
[71 byte] By [ramesh.shrirama] at [2007-11-26 17:44:27]
# 1

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.

ProZa at 2007-7-9 0:12:38 > top of Java-index,Java Essentials,New To Java...
# 2

> 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.

ProZa at 2007-7-9 0:12:38 > top of Java-index,Java Essentials,New To Java...
# 3

> 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.

ProZa at 2007-7-9 0:12:38 > top of Java-index,Java Essentials,New To Java...
# 4
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
ProZa at 2007-7-9 0:12:38 > top of Java-index,Java Essentials,New To Java...
# 5

> 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,

ProZa at 2007-7-9 0:12:38 > top of Java-index,Java Essentials,New To Java...
# 6
A new one is created too with the StringBuffer
ProZa at 2007-7-9 0:12:38 > top of Java-index,Java Essentials,New To Java...
# 7
> A new one is created too with the StringBufferA new what? StringBuffer reverse does not return a new StringBuffer(?)Kaj
kajbja at 2007-7-9 0:12:38 > top of Java-index,Java Essentials,New To Java...
# 8
Yes you're right. It returns only a reference to the same StringBuffer
ProZa at 2007-7-9 0:12:38 > top of Java-index,Java Essentials,New To Java...
# 9

> 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.

ProZa at 2007-7-9 0:12:38 > top of Java-index,Java Essentials,New To Java...
# 10
Yes i thought that and i was wrong that is why my previous reply #8 has sense ...
ProZa at 2007-7-9 0:12:38 > top of Java-index,Java Essentials,New To Java...