File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need Help with this code 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need Help with this code" Watch "Need Help with this code" New topic
Author

Need Help with this code

Rahul Siddharth
Ranch Hand

Joined: Feb 17, 2007
Posts: 33
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rahul Siddharth
Ranch Hand

Joined: Feb 17, 2007
Posts: 33
Thanks Buddy.Looks like the question was improperly typed on the simulator.Thanks anyway.Appreciate your help.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Apr 20, 2002
Posts: 510
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help with this code
 
Similar Threads
String and StringBuffer
help with test ?...strings
String Changed
Replace a char in a String
String and StringBuffer