Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Strings are immutable?

  1. #1
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Strings are immutable?

    Q4-8. What is the output of the following code?
    class EJavaGuruString2 {
    public static void main(String args[]) {
    String ejg = "game".replace('a', 'Z').trim().concat("Aa");
    ejg.substring(0, 2);
    System.out.println(ejg);
    }
    }
    a gZmeAZ
    b gZmeAa
    c gZm
    d gZ
    e game
    Answer: b

    Q4-9. What is the output of the following code?
    class EJavaGuruString2 {
    public static void main(String args[]) {
    String ejg = "game";
    ejg.replace('a', 'Z').trim().concat("Aa");
    ejg.substring(0, 2);
    System.out.println(ejg);
    }
    }
    a gZmeAZ
    b gZmeAa
    c gZm
    d gZ
    e game
    Answer: e


    WHY? the first one when u use methods such as replace, it changes the string, but the second example still uses replace but nowwww it saying that stringes are immutable!

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: Strings are immutable?

    Methods replace() and substring() return the new values, they don't change the original String. Since the result is not assigned to any variable, nothing happens. It would be different if it looked like this:
    Java Code:
    class EJavaGuruString2 {
       public static void main(String args[]) {
          String ejg = "game";
          ejg = ejg.replace('a', 'Z').trim().concat("Aa");
          ejg = ejg.substring(0, 2);
          System.out.println(ejg);
       }
    }
    The result of the operations is assigned to ejg again. Now ejg prints gZ
    Last edited by SurfMan; 07-23-2014 at 10:07 AM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Strings are immutable?

    A little shorter example, this:

    Java Code:
    String theText = "this is a game";
    theText.replace("game", "simulation");
    
    System.out.println(theText);
    does not change the value of the String object pointed to by theText.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Strings are immutable?

    ooohhhh, thank you!

  5. #5
    Matrice is offline Member
    Join Date
    Jul 2014
    Location
    Mililani
    Posts
    37
    Rep Power
    0

    Default Re: Strings are immutable?

    I have recently learned that Strings are:

    Immutable

    Not secure for sensitive data, passwords

    Can change if given the command TypeupperCase(); or TypelowerCase(); but it still remains the same.

    It usually mean " " words inside of quotation marks

    String appears in the worthless but well-known Computer given Public.static.void(String[] args) thing

    String is a Type of declaration

    String can be a possible return value (answer)

    A Sting defines what a common behavior is






    So, check out my knowledge of a String and tell me what you guys think! I hope I've been learning well over these last couple of days.

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: Strings are immutable?

    Make it a habit not to hijack other threads for this. If you want to verify your thoughts, please start a new thread.

    Quote Originally Posted by Matrice View Post
    I have recently learned that Strings are:
    Not secure for sensitive data, passwords
    And do you know why?

    Quote Originally Posted by Matrice View Post
    Can change if given the command TypeupperCase(); or TypelowerCase(); but it still remains the same.
    These methods do not exist on a String. toUpperCase() and toLowerCase() do. Still, a String does *not* change when calling these methods. They merely return the new String that is the result of the operation.

    Quote Originally Posted by Matrice View Post
    String appears in the worthless but well-known Computer given Public.static.void(String[] args) thing
    If you don't understand why it's there, don't call it worthless.

    public static void main(String[] args) (without all the crazy periods in between) is the entry point of a Java application. The parameter args, which is an array of Strings, contains the commandline arguments.

    Quote Originally Posted by Matrice View Post
    String is a Type of declaration
    String can be a possible return value (answer)
    Yes, you can declare a String field/variable/parameter and also return it from a method. A String is just a sequence of characters. Nothing more, nothing less.

    Quote Originally Posted by Matrice View Post
    A Sting defines what a common behavior is
    This has nothing to do with "common behaviour", whatever that may be.
    jim829 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Strings are immutable
    By Nazneen Ali in forum New To Java
    Replies: 1
    Last Post: 10-11-2013, 05:34 PM
  2. Strings are Immutable
    By allaudin in forum New To Java
    Replies: 6
    Last Post: 07-01-2013, 01:41 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 an Immutable Class
    By maheshkanda in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 09:12 PM
  5. Strings are immutable yet they can be changed ?
    By anjanesh in forum New To Java
    Replies: 4
    Last Post: 05-19-2007, 04:08 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
  •