Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can you explain this code?

 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



in the //////////////SOME CODE of i write this


STILL NOTHING IS REPLACED? What happens exactly?
 
Baiju Scariah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java String is immutable - meaning not modifiable. You may modify the String reference and store a new string there.

Ex:
Modify StringReplace() as give below..

public static String StringReplace(String t)
{
t=t.replace("j","i"); //It should have replaced "j" from "java" with an "i"
return t;
}

and

in main() as given below

s = StringReplace(s); //what happens over here?

This would give you desired result.
 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Baiju,

I understood. That was a perfect answer

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, variables are passed by value in Java. Assigning another value to t in line 6 and 12 has no effect.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic