aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String and StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String and StringBuffer" Watch "String and StringBuffer" New topic
Author

String and StringBuffer

xie li
Ranch Hand

Joined: Nov 30, 2005
Posts: 54
1 Public class test (
2 Public static void stringReplace (String text) (
3 Text = text.replace ('j' , 'i');
4 )
5
6 public static void bufferReplace (StringBuffer text) (
7 text = text.append ("C")
8 )
9
10 public static void main (String args[]} (
11 String textString = new String ("java");
12 StringBuffer text BufferString = new StringBuffer ("java");
13
14 stringReplace (textString);
15 bufferReplace (textBuffer);
16
17 System.out.printLn (textString + textBuffer);
18 }
19 )
What is the output?
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163

public class Test
{
public static void stringReplace (String text)
{
text = text.replace ('j' , 'i');
}

public static void bufferReplace (StringBuffer text)
{
text = text.append ("C") ;
}

public static void main (String args[])
{
String textString = new String ("java");
StringBuffer textBuffer = new StringBuffer("java");
stringReplace(textString);
bufferReplace(textBuffer);
System.out.println (textString + textBuffer);
} //main

}//Test


Output: javajavaC

for understanding try this code

class K
{
public static void main(String args[])
{
String s="java";
s.replace('j','p');
System.out.println(s); //Strings are imutable

StringBuffer sb=new StringBuffer("s");
sb.append("C");
System.out.println(sb);
}//main
}//class


Output: java
sC

Regards
Naresh
Cm Ananth
Ranch Hand

Joined: Oct 20, 2005
Posts: 44
The answer will be
----------
javajavaC
----------


By Ananth, Bangalore
xie li
Ranch Hand

Joined: Nov 30, 2005
Posts: 54
thank u
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String and StringBuffer