• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on String method

 
Naveen Megharaj
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21005
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
 
Naveen Megharaj
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21005
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are right........sorry for the mistake.......i had done a small mistake while writing the code....now i got it.....thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic