-1

Hi Have a java class with two methods as below.

public class HelloWorld
{
    public static  void main(String[] args)
    {
        String a=null;
        returnOriginalString(a);
        System.out.println(a);
        a="Hello";
        System.out.println(a);
    }

    private static void returnOriginalString(String a)
    {
        a="Hello World";
    }


}

when i print the above message i still get String a as null even after changing the string in second method.And one more thing if i update the string in same method it returns the correct value.How can i update the value of a without using any String methods.?

user207421
  • 305,947
  • 44
  • 307
  • 483
Rocky4Ever
  • 828
  • 3
  • 12
  • 35

2 Answers2

0

Why does this happen?

Java is 'pass by value' - when you pass a parameter to a method, the value of that parameter is passed. In the case of an object, the value being passed is the reference to the object.

This is where it gets interesting - you'd think that if you changed the contents of the string, then the change would also be seen outside the method - due to the reference being passed in. And if this was some other type of object (like List, for example), that would be true.

But Strings are immutable. When you create a new one (as in a="Hello World";), you get a new reference - the existing String object (with the reference that was passed into the method) does not have its contents changed.

How do I fix it?

It can be fixed by returning the new String from the returnOriginalString method and assigning it to a in the main method:

a = returnOriginalString();
Jason
  • 11,744
  • 3
  • 42
  • 46
  • Nothing to do with it. The question is why the variable `String a` in `main()` isn't updated, and this isn't the reason. – user207421 Feb 25 '20 at 02:58
  • If you disagree that your original answer had nothing to do with it, why did you feel a need to change it? You can't have it both ways. And the fact that Strings are immutable continues to have exactly nothing to do with it. The same would apply to any object. – user207421 Feb 26 '20 at 09:34
  • As I've already said, the same is true of `a = `. It's an assignment, and it loses the previous value of `a`. Immutably has nothing to do with it. And I've never actually met anyone who should think that `a = ` is the same as `a,set(...)`. It's not a 'common mistake' at all, it's just something you dreamed up yourself. – user207421 Feb 27 '20 at 05:14
-1

Updating the method parameter value does not update the value of String a of returnString(). You need to return a String from returnOriginalString(String a) and then assign the string to a.

class HelloWorld
{
   public String returnString()
  {
    String a=null;
    a=returnOriginalString();
    System.out.println(a)
      a="Hello"
    System.out.println(a)
   }

   public static String returnOriginalString()
   {
     return "Hello World;"
   }
} 
Jamiul alam khan
  • 159
  • 1
  • 10
  • 1
    Dunno who would downvote this, but it's the correct answer. – user207421 Feb 25 '20 at 02:53
  • I didn't downvote, but this only explains one way to fix it - not why it happens in the first place. – Jason Feb 25 '20 at 21:23
  • 1
    @Jason I agree, it doesn't, but neither does your statement that Strings are immutable. The real reason is that Java is pass-by-reference, which neither of you stated. It was me who did that, by closing it as a duplicate. – user207421 Feb 26 '20 at 09:36