• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String modified?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am doing applied reasoning java's mock exam. came up with this question:
String a="pay";
String b ="lay";
a.concat("2");
a.replace("p","b");
b="no";
a+=b.toUpperCase();
Q:the content of a:
the answer is "payNO".
SO, what are we saying here, String is somehow able to be modified?
Indy
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Indy:
my understanding is that you are referring to a different
string object with a reference variable 'a' which now refers
to the string "payNO".
You may say the object created on line 1 which was referring
to the string "pay" is now eligible for GC.
Correct me if I am wrong.
Regds.
- satya
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that a refers to a new String after the final assignment but, why don't the String methods concat() and replace() create a new String referenced by a when they return? ie
public class StringTest{

String a= "pay";
String b ="lay";


public StringTest(){
a.concat("2");
System.out.println("a = " + a);
a.replace('p','b');
System.out.println("a = " + a);
b="no";
a+=b.toUpperCase();
System.out.println("a = " + a);
}
public static void main (String args[]){
StringTest st = new StringTest();
}
}
prints out:
a = pay
a = pay
a = payNO
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott:
Golden Rule: Strings are immutable.
the concat() and replace() return a new string object
(they will NOT modify the original object).
But in the code above, the return string is not assigned
to anything.
Try replacing those lines with the following lines:

This should give you the result you expect.
ps: I haven't tested this, but IMO, it should work.
Regds.
- satya
 
Scott Mueller
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks satya,
That does what I expected. So, although the String methods return strings, since they were not assigned to the variable a, a still references the original string. Yes?
Scott
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes. You are right.
regds
maha anna
 
reply
    Bookmark Topic Watch Topic
  • New Topic