Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes copyValueOf vs ValueOf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "copyValueOf vs ValueOf" Watch "copyValueOf vs ValueOf" New topic
Author

copyValueOf vs ValueOf

cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Dear all,
Can someone explain me the main difference between String.copyValueOf et String.valueOf?
I've tried simply this:

and output is :
abcd
abcd
in both case...
Thanks in advance,
Cyril.


SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
(1) Here is the SDK 1.4.1 source code. As you can see, there is no difference.

(2)
There are 2 overloaded forms of String.copyValueOf.
public static String copyValueOf(char data[])
public static String copyValueOf(char data[], int offset, int count)
There are 9 overloaded forms of String.valueOf.
public static String valueOf(char data[])
public static String valueOf(char data[], int offset, int count)
public static String valueOf(boolean b)
public static String valueOf(char c)
public static String valueOf(int i)
public static String valueOf(long l)
public static String valueOf(float f)
public static String valueOf(double d)
public static String valueOf(Object obj)
(3)
Notice that there are three ways to create a String from an array of char: the String contructor, valueOf and copyValueOf
public String(char value[])
public String(char value[], int offset, int count)
(4)
And finally, don�t confuse String.valueOf with the primitive type wrapper valueOf methods.
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Thanks Marlene for your very clear answer.
We are very lucky to learn from you...
Cyril.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I use to wonder the same thing myself. So finally I decided I had better dig into the source code, so that I would stop wasting time wondering.
Calvin Yan
Greenhorn

Joined: Jun 25, 2003
Posts: 19
Thank you Marlene.
You explained this question so clearly.Except that, you taught me one method of learning


I will do the best with what God gave me!
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Cyril and Calvin.
Calvin, I am not sure I understand what you mean : Except that, you taught me one method of learning. I am curious to know.
Calvin Yan
Greenhorn

Joined: Jun 25, 2003
Posts: 19
Marlene I mean I will also read the source codes if I meet with the problems like that.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Oh, now I understand. Thank you.
Yes, reading the source code does help sometimes. It can be very helpful when people are arguing about something, but just guessing. Then you look at the source code and settle the question once and for all.
[ July 19, 2003: Message edited by: Marlene Miller ]
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Does anybody know if the source codes are publish somewhere in the Sun's web site? Downloading the SDK is too big for my machine.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Alton
In the folder where you have installed java. Look for a file src.zip.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: copyValueOf vs ValueOf
 
Similar Threads
A problem using charAt..........
Difference between String.copyValueOf(char[]) and String.valueOf(char[])
Can this be a case of deadlock ?And how to determine it output ?
Strings
== with Strings is giving a different result