File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes question on String method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 18116
    
  39

//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: 18116
    
  39

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://aspose.com/file-tools
 
subject: question on String method
 
Similar Threads
Generic question
Generics Doubt
Question on using return value and not.
StringBuffer / StringBuilder methods
String Question - Im in Doubt