how Passing a string to a function

 

Questions


I would like to ask a really simple question. I am just passing a String object to a function but the result is weird. I supposed that because I am passing an object(by reference) the result should have be ” Here” and not “Hello”. Why is this happening?

public class MainStr
{

/**
 * @param args
 */
public static void main(String[] args)
{
    String str = "Hello!";
    System.out.println(str);
    changeString(str);
    System.out.println(str);


}

static void changeString(String str)
{
    str=" HERE";
}

}

Thanks.

EDITED:

public class MainStr
{

String str;
/**
 * @param args
 */
public static void main(String[] args)
{
    MainStr ms = new MainStr();
    ms.str = "Hello!";
    System.out.println(ms.str);
    changeString(ms);
    System.out.println(ms.str);


}

static void changeString(MainStr ms)
{
    ms.str=" HERE";

}

}

If that is the case then why if I pass it inside a wrapper object is working? The wrapper object it is not by reference?

PS: Why this is working that way? String is an object. Why do I need another wrapper Object to change an OBJECT! What if I want to pass something by reference? Is that possible?

 

 

————————————————-

Answer

Java passes object references by value this means when you call a function the following happens.

 String origStr = "Some String"

 // origStr -> "Some String "

When the object is passed to the value it looks like this

 //origStr -> "Some String " <- passedStr

Then in the function you reset what passedStr points to

 //origStr -> "Some String "
 //passedStr -> "Other String"

The original string still refers to “Some String” but the copy of it now refers to “Other String”

When you return from the function the original string reference still points at “Some String”

// EDIT: Why can you modify objects referred to by another object?

Lets say I have StringWrapper, which is a class which contains a string.

To java that looks like this

origStr -> strWrpA -> "Some String"

When you pass it to a function the following happens just like above

origStrWrp -> strWrpA -> "Some String"
passedStrWrp --/^

Notice how both variables point at strWrpA in memory.
Now if i decided to modify the strWrpA.myString Java will
follow the pointer to the actual string in the wrapper. The pointer
to strWrpA will remain unchanged.
This means I can change the contents of the object even though
I’m accessing it through a copy of the original reference.

                        "Some String "
origStrWrp -> strWrpA -> "I got Changed!"
passedStrWrp --/^

You would have the same problem as you have without a wrapper if you did this

//Does Nothing
function (origStrWrp)
{
origStrWrp=new StringWrapper("Other String") // This only reassigns the copy of the reference and would give you the same problem
}
//Changes internal object
function (origStrWrp)
{
origStrWrp.myString = "Other String" // This doesn't change the reference, it changes the object that it refers to.
}

java

Facebook Comments

Post a comment