• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Need Help with this code

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldnt figure out the output.Could someone please explain the code?

code:
-------------------------------------------------------------------------
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 text BufferString = new StringBuffer(�java�);
stringReplace (textString);
BufferReplace (textBuffer);

System.out.printLn (textString + textBuffer);
}
}

Answer: JAVAJAVA
-----------------------------------------------------------------------

Thanks in Advance.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried compiling this? I see at least 10 typos. Remember, Java is case sensitive, and parentheses and braces are quite different. The output is actually javajavaC (and that 'C' was probably intended to be lowercase).

The idea illustrated here is that Strings are immutable. When you call myString.replace('a', 'b'), the original String is not changed in any way. Instead, a new String is created with the replaced chars.

In contrast, a StringBuffer is mutable. When you call myStringBuffer.append("Str"), the original StringBuffer's contents is appended with the argument String.

Therefore, the output will be the original String, unchanged by the call to replace, concatenated with a String representation of the StringBuffer, which has been changed to include the appended "c".

So the result is "java" and "javac." (Get it? )
[ July 28, 2007: Message edited by: marc weber ]
 
Rahul Siddharth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Buddy.Looks like the question was improperly typed on the simulator.Thanks anyway.Appreciate your help.
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the exam they will give you some complicated code with a method that takes in a String and changes it locally in the method. The method returns void. It's a trick. If you pass into a method any immutable object (String, Integer, etc) you will not be able to change the underlying object and the original reference variable will be pointing to the original (and unchanged) object. In the case of a String any attempt to change it will result in a new String object being created.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Ku:
... It's a trick...


It's testing knowledge of String methods and local variable scope. I'm not sure that's a "trick." It's just something you need to know.
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The trick is not the concept. The trick is that if you do not recognize early if not immediately that the String cannot be changed then you waste valuable time trying to figure out the "changes" to the String
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic