[java] Is Java "pass-by-reference" or "pass-by-value"?

Shortest answer :)

  • Java has pass-by-value (and pass-reference-by-value.)
  • C# also has pass-by-reference

In C# this is accomplished with the "out" and "ref" keywords.

Pass By Reference: The variable is passed in such a way that a reassignment inside the method is reflected even outside the method.

Here follows an example of passing-by-reference (C#). This feature does not exist in java.

class Example
{
    static void InitArray(out int[] arr)
    {
        arr = new int[5] { 1, 2, 3, 4, 5 };
    }

    static void Main()
    {
        int[] someArray;
        InitArray(out someArray);

        // This is true !
        boolean isTrue = (someArray[0] == 1);
    }
}

See also: MSDN library (C#): passing arrays by ref and out

See also: MSDN library (C#): passing by by value and by reference

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to methods

String method cannot be found in a main class method Calling another method java GUI ReactJS - Call One Component Method From Another Component multiple conditions for JavaScript .includes() method java, get set methods includes() not working in all browsers Python safe method to get value of nested dictionary Calling one method from another within same class in Python TypeError: method() takes 1 positional argument but 2 were given Android ListView with onClick items

Examples related to parameter-passing

How to pass parameter to a promise function Check number of arguments passed to a Bash script How to pass event as argument to an inline event handler in JavaScript? Passing Parameters JavaFX FXML Invoke a second script with arguments from a script How can I pass a member function where a free function is expected? Passing variables, creating instances, self, The mechanics and usage of classes: need explanation In Javascript/jQuery what does (e) mean? How to write a bash script that takes optional input arguments? Passing Objects By Reference or Value in C#

Examples related to pass-by-reference

Passing an integer by reference in Python Does JavaScript pass by reference? Object passed as parameter to another class, by value or reference? change values in array when doing foreach Call-time pass-by-reference has been removed C++ pass an array by reference Passing Objects By Reference or Value in C# Pass variables by reference in JavaScript JavaScript by reference vs. by value How to do the equivalent of pass by reference for primitives in Java

Examples related to pass-by-value

Passing an integer by reference in Python Does JavaScript pass by reference? Passing Objects By Reference or Value in C# JavaScript by reference vs. by value How to pass objects to functions in C++? Are arrays in PHP copied as value or as reference to new variables, and when passed to functions? Is JavaScript a pass-by-reference or pass-by-value language? Why should I use the keyword "final" on a method parameter in Java? Pass by Reference / Value in C++ What's the difference between passing by reference vs. passing by value?