File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

copyValueOf vs ValueOf

 
cyril vidal
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marlene for your very clear answer.
We are very lucky to learn from you...
Cyril.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Marlene.
You explained this question so clearly.Except that, you taught me one method of learning
 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marlene I mean I will also read the source codes if I meet with the problems like that.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alton
In the folder where you have installed java. Look for a file src.zip.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic