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

    Default Why String is immutable in java?


    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 online now Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    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,

    Build a wall around Donald Trump; I'll pay for it.

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

    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 - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Why string is immutable in java?
    By jessie143143 in forum Reviews / Advertising
    Replies: 0
    Last Post: 10-15-2011, 02: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, 07: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