• 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

replace() method in strings.......

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source: http://www.geocities.com/SiliconValley/Orchard/9362/java/javacert/poddar.html



options are:
a) arit
amit
false
true
b) arit
arit
false
true
c) amit
amit
false
true
d) arit
amit
true
true

can you guess answer?
can anyone explain me what is hapenning in line 1 and line 2?
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ganesh,

output is arit,amit,false,true.
i also wondering at this...(s2==s3). it would be great if anyone clear this.
any explanation given for that question?

Preparing for scjp1.5
Preetha
[ December 19, 2008: Message edited by: Preetha Arun ]
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any one to explain this?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wild guess: When you are comparing 2 variables, your actually passing the bits of the current variable your comparing. s2 has the bit representation value of the s1 with a value of "arit" and s3 has the bit representation value of the string "arit" only..

When you are directly using a value (example: String j = "po"), i would recommend to use == for comparison and if you are actually comparing the string object (ex: String j = new String("po")), it would be advisable to use .equals() method (which is overriden from the object class).
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Remmember following points:

  • s1.replace('m','r') returns a new String Object which gets printed by the code you have written.


  • s1 is still refering to the previously created String Object(of value amit).

    Note: You are not keeping any refrence to this newly created String Object. So it will be lost as soon as it is created and will be eligible for gc.
  • s3 and s4 are refering to the same String object as you are not using new keyword. So s3==s4 will be true.
  • In the following code you are doing the same thing as done in 2nd line of your code but this time you are preserving the reference of the newly created String object.


  • Moreover remmeber that the function replace(oldChar, newChar) always returns a newly created Strng Object.





    If you got all this then you can easily get the logic of the question.

    HTH
    [ December 20, 2008: Message edited by: Himanshu Gupta ]
     
    Ganeshkumar cheekati
    Ranch Hand
    Posts: 362
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    yes replace() method create new string object.

    but in my code it creates an object with content "arit" which is already exists in pool and it is referenced by s3.

    IS s2 should point to the same object(referenced by s3) in pool or not?
     
    Himanshu Gupta
    Ranch Hand
    Posts: 598
    Android Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    See the code of the repalce again. It is using new operator.



    So it will create a new Object.

    About the question that why it is not taking the Object from String pool, I think that before making an Object how can it be decided that what object is going to be created and also the parameters can change at runtime. Whenever we have any operation on string objects a new object is created, like even if you concatenate two string



    It will give you false.

    hth
    [ December 20, 2008: Message edited by: Himanshu Gupta ]
     
    My honeysuckle is blooming this year! Now to fertilize this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic