Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Why String is immutable in java?

    Hi,

    I am Harikrishan.

    please any one tell me.

    string is immutable in java why?
    i know string is immutable in java and meaning of immutable.

    but why string is immutable in java.



    please help me.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why String is immutable in java?

    For one thing: a String couldn't serve as a key in a Map<String, ?> nor can substrings share their char buffers.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Why String is immutable in java?

    Also, how would using String literals make sense? What happens if you do this?

    Java Code:
    System.out.println("cats"); //prints cats, okay
    "cats".add(" and more cats"); //since Strings are mutable, this appends the String?
    System.out.println("cats"); //this now prints out something other than the literal?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Why string is immutable in java?
    By jessie143143 in forum Reviews / Advertising
    Replies: 0
    Last Post: 10-15-2011, 03:47 PM
  2. String Immutable
    By jomypgeorge in forum New To Java
    Replies: 18
    Last Post: 12-17-2010, 12:54 PM
  3. Strings and Immutable
    By al_Marshy_1981 in forum New To Java
    Replies: 19
    Last Post: 06-18-2010, 08:22 AM
  4. What is Immutable in String
    By elektronika in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 01:58 PM
  5. What is an Immutable Class
    By maheshkanda in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 09:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •