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 String.valueOf() 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.valueOf()" Watch "String.valueOf()" New topic
Author

String.valueOf()

Binu K Idicula
Ranch Hand

Joined: Jul 11, 2002
Posts: 99
Hi,
I ve a one doubt regarding this method
public static void main(String args[]){
System.out.println(String.valueOf(1234) ==String.valueOf(1234));
System.out.println(String.valueOf("1234")==String.valueOf("1234"));
}
will print
false
true.
It seems when it is primitive value , a new string is created and if its a string(actually Object) , its toString method is invoked which returns the reference to actual string . Is this the reason why it prints the above ?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Yes it is


SCJP2. Please Indent your code using UBB Code
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This example helps convince one not to use "==" to
compare Strings. If the equals() method was used we would get two times true.

-Barry
[ August 09, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
However, it is important to know for the exam:
String s1 = "hello";
String s2 = "hello";
System.out.println (s1==s2);
// prints true; all identical string literals are ==
String s3 = s2.toString();
System.out.println (s2==s3);
//prints true; String.toString() returns the string itself
On the other hand
String s4 = "hello";
String s5 = new String("hello");
System.out.println (s4==s5);
// prints false; s2 is not a literal


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
John Sun
Greenhorn

Joined: Aug 09, 2002
Posts: 1
Originally posted by Binu K Idicula:
Hi,
I ve a one doubt regarding this method
public static void main(String args[]){
System.out.println(String.valueOf(1234) ==String.valueOf(1234));
System.out.println(String.valueOf("1234")==String.valueOf("1234"));
}
will print
false
true.
It seems when it is primitive value , a new string is created and if its a string(actually Object) , its toString method is invoked which returns the reference to actual string . Is this the reason why it prints the above ?

Also it maybe helpful to recognize that the calls made using the Object-parameter version passes in references to the same String object.


cheers,<br />John
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String.valueOf()
 
Similar Threads
valueOf() method
String doubt
Changing the variable in method scope inside the method ?
Question about string immutable
who can tell what is the method parseInt( )?