Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String and StringBuffer

 
xie li
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer will be
----------
javajavaC
----------
 
xie li
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic