r/javascript • • Jun 18 '17

Pass by reference !== pass by value

3.3k Upvotes

272 comments sorted by

View all comments

Show parent comments

20

u/JB-from-ATL Jun 18 '17

If being correct makes me a snob, okay, but Java is pass by value. "Pass by value" means whatever the variable was is passed. In Java, the non primitive variables are references. So you pass that reference. If it were pass by reference you would pass a reference to that reference. That's not what it does.

-14

u/flygoing Jun 18 '17

So you pass that reference.

So...pass by reference...

12

u/legato_gelato Jun 18 '17

There's a reason stuff like https://msdn.microsoft.com/en-us/library/bb347013(v=vs.110).aspx doesn't exist in java. Hint: that reason is that it does NOT have "pass by reference".. It's not some grammar discussion, there's technical differences.. You can't just redefine it as you wish.. And you're calling other people snobby, come on man..

7

u/JB-from-ATL Jun 18 '17

If I'm passing a number, I still call it pass by value, not pass by number. Just because the value is a reference doesn't make it pass by reference.

2

u/TapedeckNinja Jun 18 '17
public static void Main(String[] args) {
    String str = "foo";
    bar(foo);
    System.out.println(str);
}

public static void bar(String str) {
    str = "bar";
}

What's the output?

In C# you can do ...

public static void Main(string[] args)
{
    string str = "foo";
    bar(ref str);
    Console.WriteLine(str);
}

public static void bar(ref string str)
{
    str = "bar";
}

The Java example outputs "foo" and the C# example outputs "bar."