Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Can you explain this code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can you explain this code?" Watch "Can you explain this code?" New topic

Can you explain this code?

shoeb sayyed
Ranch Hand

Joined: Mar 14, 2010
Posts: 48

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

STILL NOTHING IS REPLACED? What happens exactly?

Baiju Scariah
Ranch Hand

Joined: Mar 17, 2005
Posts: 33
In Java String is immutable - meaning not modifiable. You may modify the String reference and store a new string there.

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;


in main() as given below

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

This would give you desired result.
shoeb sayyed
Ranch Hand

Joined: Mar 14, 2010
Posts: 48
thanks Baiju,

I understood. That was a perfect answer

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15003

Also, variables are passed by value in Java. Assigning another value to t in line 6 and 12 has no effect.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Can you explain this code?
jQuery in Action, 3rd edition