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 5 of 5

Thread: When to use new String("abc")

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default When to use new String("abc")

    Can anyone give a scenario of when to use String aaa = new String("abc") against String bbb="abc".

  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: When to use new String("abc")

    I have never seen someone use new String("abc"), that seems somewhat unnecessary.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: When to use new String("abc")

    Java API has provided this functionality. So there has to be a reason behind for it.

  4. #4
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: When to use new String("abc")

    I presume you've already read the Javadocs? It is not usually needed. The example you show auto-creates a String with the characters "abc" to pass to the String constuctor. So, as the Javadocs say:

    Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.
    Sometimes the reason is for completness, sometimes it is an oversight.
    Need Java help? Check out the HotJoe Java Help forums!

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: When to use new String("abc")

    Quote Originally Posted by tcstcs View Post
    Java API has provided this functionality. So there has to be a reason behind for it.
    A portion of the API for this method:

    Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.
    One example where one might wish to copy: suppose you read a large File into a String. You then parse that String into smaller String's by calling substring, and explicitly keep references to these but not to the String of the full file. The String objects created from using substring still contain a reference to the original String they were parsed from (substring actually causes a new String to be created which references the original char array and the index values of the substring) - so although one might think the original String (actually its underlying char array) representing the full contents of the File is capable of being removed from memory, it is not. To avoid hanging onto these references, one can use the new String("") constructor to create copies of the substring, rather than referencing the original.
    Last edited by copeg; July 2nd, 2012 at 10:01 AM. Reason: Clarification

  6. The Following User Says Thank You to copeg For This Useful Post:

    tcstcs (July 3rd, 2012)

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  2. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  3. Need something like a "Hashtable<String[], Integer>" kind of data type @@
    By Albretch Mueller in forum Java Theory & Questions
    Replies: 2
    Last Post: November 27th, 2010, 10:24 PM
  4. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM