| Author |
question on String method
|
Naveen Megharaj
Ranch Hand
Joined: Feb 13, 2009
Posts: 39
|
|
public class Zobb {
public static void main(String [] args) {
StringBuilder sb = new StringBuilder("Foo");
sb.append("bar").delete(2, 4).reverse().insert(2, "tt");
String s = sb.toString().substring(1, 5);
s.toUpperCase();
System.out.println(s);
}
}
//my question is even though "toUpperCase()" method is being called on String object "s", the output being printed is "atto". why is it like that....?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
//my question is even though "toUpperCase()" method is being called on String object "s", the output being printed is "atto". why is it like that....?
Strings are immutable. The toUpperCase() method returns a new string, which in your example code, you didn't assign to anything.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Naveen Megharaj
Ranch Hand
Joined: Feb 13, 2009
Posts: 39
|
|
|
what you are saying is correct....... but i already checked it out by assigning to a new String variable, buy even then its showing the same output........"atto"
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Naveen Megharaj wrote:what you are saying is correct....... but i already checked it out by assigning to a new String variable, buy even then its showing the same output........"atto"
Highly unlikely... Show us the code.
Henry
|
 |
Naveen Megharaj
Ranch Hand
Joined: Feb 13, 2009
Posts: 39
|
|
|
yes you are right........sorry for the mistake.......i had done a small mistake while writing the code....now i got it.....thank you.
|
 |
 |
|
|
subject: question on String method
|
|
|