aspose file tools
The moose likes Beginning Java and the fly likes question on String method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "question on String method" Watch "question on String method" New topic
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
    
  19

//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
    
  19

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: question on String method
 
Similar Threads
Generic question
Generics Doubt
String Question - Im in Doubt
Question on using return value and not.
StringBuffer / StringBuilder methods