Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Converting to String

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Converting to String

    String class provides us with the method String.valueOf(some stuff) that basically converts "some stuff" into a string..
    But we can also concatenate "some stuff" with an empty String ("").
    Both these methods result in a String representation of an object(or other data type) so my question is what is the difference between the two, advantages of one over the other, disadvantages of one over the other and which method do you guys usually use when forced to convert something to a String and why??


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Converting to String

    Indirectly, both of these methods are calling the objects' toString() method.
    I normally just call this method to get the object to a string, or alternatively you can directly "add" your object to a string, automatically calling the toString() method and concatenating the strings together. This makes the code easy to read and understand.

    Technically, calling the toString() method is the most efficient and uses the least memory, but the performance of all the methods are virtually identical. Note that concatenating strings can become very slow for repeated concatenations of large strings because it does have to create a new character array and copy over both string's to get the new string.
    If you have to build large strings, the recommended method would be to use the StringBuilder class.

Similar Threads

  1. Converting Hex to Decimal
    By r2ro_serolf in forum Java Theory & Questions
    Replies: 10
    Last Post: September 4th, 2011, 04:29 PM
  2. Converting an array
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2010, 09:58 AM
  3. [SOLVED] Help Converting to an Acronym
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 2nd, 2010, 01:37 PM
  4. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  5. Replies: 4
    Last Post: May 1st, 2009, 03:32 PM