Results 1 to 3 of 3
  1. #1
    lymphocyte is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default Strings can change?

    so i have this book and it says
    "A String object is immutable its contents cannot be changed"

    but Strings can change? at least in this code what does it mean by Strings cant be changed?
    Java Code:
    //String problem 
    public class string{
    
    public static void main(String[] arg)
    {
    
    String Cheese = "THIS SHOULD NOT CHANGE STRINGS ARE IMMUTABLE";
    System.out.println(Cheese);
    
    Cheese = "Sally Mcsalad";
    System.out.println(Cheese);
    
    }
    }

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Strings can change?

    Strings are immutable in terms of memory.

    this string changes because you told it to with the "="

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Strings can change?

    Quote Originally Posted by lymphocyte View Post
    so i have this book and it says
    "A String object is immutable its contents cannot be changed"

    but Strings can change? at least in this code what does it mean by Strings cant be changed?
    Java Code:
    //String problem 
    public class string{
    
    public static void main(String[] arg)
    {
    
    String Cheese = "THIS SHOULD NOT CHANGE STRINGS ARE IMMUTABLE";
    System.out.println(Cheese);
    
    Cheese = "Sally Mcsalad";
    System.out.println(Cheese);
    
    }
    }
    You're making a common mistake: you're confusing the variable with the object. Please understand that Cheese is a String variable, and not the object itself. All you are doing in your code above is assigning different String objects to that variable. The String objects themselves are immutable or unchanging.

    By the way, what type of lymphocyte are you? B? T?
    Last edited by Fubarable; 12-02-2012 at 03:45 AM.

Similar Threads

  1. Replies: 12
    Last Post: 04-20-2012, 05:43 PM
  2. need help with strings
    By iron_fist in forum Java Applets
    Replies: 10
    Last Post: 02-15-2012, 05:16 PM
  3. Replies: 1
    Last Post: 04-17-2011, 03:59 PM
  4. Strings
    By zoala001 in forum Java Applets
    Replies: 9
    Last Post: 01-02-2011, 10:26 AM
  5. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 05:30 AM

Posting Permissions

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