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?