String replace

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Praveensb
    New Member
    • Jan 2007
    • 4

    #1

    String replace

    Hi , i want the reason for the following output of replace function in java
    1> System.out.prin tln("String".re place('g','g') == "String".replac e('g','g'));

    2> System.out.prin tln("String".re place('g','G') == "String".replac e('g','G'));

    output:
    1>true
    2>false
    what is the reason for the above output.?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Praveensb
    Hi , i want the reason for the following output of replace function in java
    1> System.out.prin tln("String".re place('g','g') == "String".replac e('g','g'));

    2> System.out.prin tln("String".re place('g','G') == "String".replac e('g','G'));

    output:
    1>true
    2>false
    what is the reason for the above output.?
    First you should start a new thread if you have a problem.
    In your problem the issue is more to do with == versus .equals than with the replace method.

    String.replace returns a new String only if there was a replacement. In
    ("String".repla ce('g','g'), there is no replacement so "String" is returned with no new String being constructed. The right hand side of == is identical to this so again the literal "String" is returned. Now since both are literals and no new String has been created, the == comparison returns true.

    In the second one "String".replac e('g','G', a replacement is done and so a new String is created. The new String with literal value is a new object that is created and stored in memory. When the replace is called again at the RHS of == the same also happens. A new String is created. The new object created is also stored in memory at a different position than where the first one was stored. This means you now have two strings with value "StrinG" but stored at different memory locations. Now the comparison == compares references not values and since the references are different you get false. If however, you use the .equals method of String which compares values you will get true because this actually compares values.
    Code:
     System.out.println("String".replace('g','g') == "String".replace('g','g'));
      System.out.println("String".replace('g','G') == "String".replace('g','G'));
      System.out.println(("String".replace('g','G')).equals(("String".replace('g','G'))));
    This will be covered in more detail in the next Java class of http://www.thescripts.com/forum/thread581237.html

    Comment

    • Praveensb
      New Member
      • Jan 2007
      • 4

      #3
      Originally posted by r035198x
      First you should start a new thread if you have a problem.
      In your problem the issue is more to do with == versus .equals than with the replace method.

      String.replace returns a new String only if there was a replacement. In
      ("String".repla ce('g','g'), there is no replacement so "String" is returned with no new String being constructed. The right hand side of == is identical to this so again the literal "String" is returned. Now since both are literals and no new String has been created, the == comparison returns true.

      In the second one "String".replac e('g','G', a replacement is done and so a new String is created. The new String with literal value is a new object that is created and stored in memory. When the replace is called again at the RHS of == the same also happens. A new String is created. The new object created is also stored in memory at a different position than where the first one was stored. This means you now have two strings with value "StrinG" but stored at different memory locations. Now the comparison == compares references not values and since the references are different you get false. If however, you use the .equals method of String which compares values you will get true because this actually compares values.
      Code:
       System.out.println("String".replace('g','g') == "String".replace('g','g'));
        System.out.println("String".replace('g','G') == "String".replace('g','G'));
        System.out.println(("String".replace('g','G')).equals(("String".replace('g','G'))));
      This will be covered in more detail in the next Java class of http://www.thescripts.com/forum/thread581237.html

      Thanks for the solution , but when the replace function encounters the old character will it check with the replacing character before doing the actual replacement ?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Praveensb
        Thanks for the solution , but when the replace function encounters the old character will it check with the replacing character before doing the actual replacement ?
        Yes it compares with the new character using == because char is a primitive type there are no problems with the == comparison and no object is created there.

        Comment

        • amitrighthere
          New Member
          • Jan 2007
          • 1

          #5
          Originally posted by r035198x
          Yes it compares with the new character using == because char is a primitive type there are no problems with the == comparison and no object is created there.

          Just to add more information I am including one more case-

          Intern Case :

          System.out.prin tln("String".re place('g','G'). intern() == "String".replac e('g','G').inte rn());

          will print "true".

          Comment

          Working...