0
class insertionSort
{
    public void print(int arr[])
    {   
        arr[0]=999999999;
    }
}

public class Sorting 
{
    private  int[] arr=   {0,56,8,75,45,21,68,9,54,2,65,87,52,65,8,5,20,0,2,2};

    public static void main(String[] arg)
    {
        Sorting sort = new Sorting();
        insertionSort inSort = new insertionSort();
        inSort.print(sort.arr);
        System.out.println(sort.arr[0]);
    }
}

Although Sort.arr is a private variable , why inSort object is able to manipulate arr[0] ?

I know the parameter is passing by reference or address.

In a function how can I pass by value ?

enamel
  • 81
  • 6
  • output is : 999999999 – enamel Oct 03 '16 at 09:41
  • Java is always pass by value, however `arr` is a reference, and this reference is passed by value. You don't get the array copied. – Peter Lawrey Oct 03 '16 at 09:42
  • but the original Sort object arr[] value is changed. – enamel Oct 03 '16 at 09:45
  • http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value will answer your question – Vimal Bera Oct 03 '16 at 09:48
  • You didn't create a copy of it and it won't automagically copy it for you. It will create a copy of the reference to the array, but there is only one array. – Peter Lawrey Oct 03 '16 at 09:48
  • arrays are actually pointers --- `arr[]` is internally `*(arr+0)` --- `arr` is just the address of first element `&arr[0]` ---- when you pass `arr` you are actually passing address of the first element (pass by reference); So what ever Changes you make will be made on that memory directly – mohanen b Oct 03 '16 at 09:49
  • The value is the int[] references. So you are accessing the same object, that why I prefer to write `int[] arr` to see that I pass an array and not a integer. – AxelH Oct 03 '16 at 09:50
  • @mohanenb so, how can I pass this array "by values" ? or copied array – enamel Oct 03 '16 at 09:55
  • create a copy of that array and send it to the functions ---cause in java all object and array variables are reference variables – mohanen b Oct 03 '16 at 10:08
  • @markrotteveel: No, this is not a duplicate of http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value. Despite appearances, the question isn't about pass-by-value/pass-by-reference at all. – T.J. Crowder Oct 03 '16 at 11:06
  • @T.J.Crowder IMHO that is the basic issue of this question. – Mark Rotteveel Oct 03 '16 at 12:00
  • @MarkRotteveel: No. The question is asking how the array entry can be changed by code that isn't in the class where the `arr` member is declared, even though `arr` is `private`. The OP mentioned pass-by-value, etc., but is mistaken about that. It's about object references, nothing to do with passing conventions at all. – T.J. Crowder Oct 03 '16 at 12:17

1 Answers1

2

In a function how can I pass by value?

Java is always pass-by-value, never pass-by-reference (see this question and its answers). The value being passed in your example is the object reference1 that points to the array.

Your issue has nothing to do with pass-by-value vs. pass-by-reference, it has to do with that fact that you're passing an object reference (which is a value) into a method. That gives the method access to the object that reference refers to. So naturally it can modify the state of that object; in your case, it can modify what's stored at index 0 of the array.

Separately, it doesn't matter that the instance variable (field) you're getting that object reference from is private; the code inSort.print(sort.arr); is in the Sorting class, so it has access to sort.arr, reads its value (an object reference), and passes that value (the object reference) into the method.


1 It's important to realize that the word "reference" in "object reference" and "pass-by-reference" is talking about two very different things. In "object reference" it's talking about a reference to an object (a pointer to it). In "pass-by-reference" it's talking about a reference (pointer) to a variable. Java doesn't have pass-by-reference.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875