• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Why is the output not [HeLLo,Hello,HeLLo]

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please can someone kindly explain why the output is not [HeLLo,Hello,HeLLo].

import java.util.ArrayList;
import java.util.List;

/* Consider below code:
* What will be the result of compiling and executing Test class?
* A) [HeLLo, Hello, Hello]
* B) [Hello, Hello, Hello]                CORRECT ANSWER
* C) [HeLLo, HeLLo, HeLLo]
* D) [HeLLo, Hello, HeLLo]             WRONG ANSWER
*/



public class _27_Question_Manipulating_ArrayList {
   public static void main(String[] args) {
       String s = new String("Hello");
       List<String> list = new ArrayList<>();
       list.add(s);                                            // obj ref 1
       list.add(new String("Hello"));                  // obj ref 2
       list.add(s);                                            // obj ref 1
       s.replace("l", "L");

       System.out.println(list);
   }
}

Thanks for any help that you can give.
 
Rancher
Posts: 4775
50
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think the replace method call does?
The documentation might help you:
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#replace(char,char)
 
Clemonte Johnstone
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its more complicated than that, or I would not have asked. Does anyone have the patience please to explain why the two different String objects are not modified using the replace method please?
 
Marshal
Posts: 7933
548
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Raphael Qt wrote:

Dave Tolls wrote:What do you think the replace method call does?
The documentation might help you:
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#replace(char,char)



Its more complicated than that, or I would not have asked. Does anyone have the patience please to explain why the two different String objects are not modified using the replace method please?


Did you take time to read what Dave Tolls showed you? I think you should be able to assemble the reasoning.

Some hints:
1. Strings are immutable
2. Return result not being stored anywhere

One could argue they are answers actually.
 
Clemonte Johnstone
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, of course it is. Thank you for the reminder I had forgotten. Also you are right, the result is not stored anywhere. Thanks again.
 
Dave Tolls
Rancher
Posts: 4775
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do notice the link didn't work too well, as it seems to have dropped off the last bracket when you click on it...consequently it doesn't go to the method directly.

Also note I linked to the char version of replace and not the CharSequence version (which is the one in your code).
The reason for that is that (annoyingly) the CharSequence one does not actually talk about creating a new String and returning that new one, and the method therefore having no effect on the original String.
 
Marshal
Posts: 71630
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . . the link didn't work too well, as it seems to have dropped off the last bracket . . . .

I always use the URL button; I only have such problems if the method takes an array as a parameter. The preview button should make the problem appear. Then I have to change the [ to &lbrack; and it works. At least I think that is the trick I employ.
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic