-3
public class test{
    public static void main(String args[]){
       int val = 10;
       System.out.println(val);
       Obj obj = new Obj();
       obj.def(val);
       System.out.println(val)
    }
 }
 public class OBJ{
    public void def(int val){
       val = 1;
    }
 }

that result is same (10 , 10) however..

public class test{
    public static void main(String args[]){
       double val[] = {0, 1, 2, 3, 4, 5};
       Obj obj = new Obj();
       for(int i = 0; i < val.length; i++){
          System.out.println(val[i];      
       }   
       obj.def(val);
       for(int i = 0; i < val.length; i++){
          System.out.println(val[i];      
       }

    }
 }
 public class OBJ{
    public void def(double[] val){
       for(int i = 0; i < val.length; i++){
          val[i] = 1;
       }
 }

that is different, first print is 0 ~ 5, however, second print is 1 , 1, 1.... I don't know what is different,

in java, Array ( it mean likes int []..) use address?? like pointer in C?

P.S : Sorry for indentation.. above code write in web,

SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
KIM
  • 233
  • 1
  • 3
  • 10
  • 4
    Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – SomeJavaGuy Jan 12 '16 at 10:06
  • When you test some code, then it is often _very_ clever to copy and paste your tested code instead of trying to rewrite it here. If you're not on a PC where you can copy it from, then wait until you are there. – Tom Jan 12 '16 at 10:13
  • Since Java is case-sensitive I'll have to ask is OBJ class relevant to your question, you are not using it in test class or vice versa? – Olga Jan 12 '16 at 10:38

2 Answers2

0

The answere is quite simple looking at the first code snippet:

  public class test{
    public static void main(String args[]){
       int val = 10;                        //variable var in main method
       System.out.println(val);             // prints var of main method
       Obj obj = new Obj();
       obj.def(val);
       System.out.println(val)             // prints var of main method
  }
 }
  public class OBJ{
   public void def(int val){
      val = 1;                // variable val in OBJ method
   }
 }

As you might see. both variables are called "var" but only one is printed out. The var variable in your OBJ method is never used.

now the other Case:

    public class test{
    public static void main(String args[]){
       double val[] = {0, 1, 2, 3, 4, 5};         //array is an object, variable var is a pointer to the location of that object in  your heap
       Obj obj = new Obj();
        for(int i = 0; i < val.length; i++){

            System.out.println(val[i];      // you print out every number located at the space in your heap your pointer is pointing at
 }

       obj.def(val);
       for(int i = 0; i < val.length; i++){

            System.out.println(val[i];      //prints out the overwritten values
 }

  }
 }
  public class OBJ{
   public void def(double[] val){    //you give the method the pointer to the location of your numbers in heap
      for(int i = 0; i < val.length; i++
     val = 1;   //you have the exact pointer to the exact objects in your heap therefore you overwrite them
   }
 }

Now to this part. Your var variable is a pointer to your var[] object in your heap (yeah arrays are objects). You give your def method the pointer to your object. Therefore the object can be accessed and overwritten.

To put it in a nutshell. In your first snippet you create two int variables with no connection to each other. In your second snipped you ceate an object and a variable that is pointing to it. In that case the object can be accessed via the pointer. You could also do something like this:

int[] sameArray = var;

In this case "sameArray" would NOT be another object, but a pointer to your var[] object.

I am sorry for my terrible english, I'll correct it as soon as possible

Tom Wellbrock
  • 2,982
  • 1
  • 13
  • 21
0

In java primitives, like the int are directly passed by value. Object, which arrays belong to aswell, are passed by value aswell, just that in this case the reference is passed as value. This means that you are working on the same instance of the array in your case.

As you can see in your example passing the int[] and changing the values in it are also affecting the original int[] that was passed to it. The true meaning of what is written above, that the reference is passed as value is that changing the reference of the object wont reflect in a change in the original value.

Here is a tiny example with comments demonstrating this.

public class TestObj {
    private int val;

    public TestObj(int value) {
        this.val = value;
    }

    public static void main(String[] args) {
        int value = 1;
        int [] values = {1,2,3,4};
        TestObj obj = new TestObj(15);
        System.out.print("Print single int: ");
        print(value); 
        System.out.println("Print int array:");
        print(values);
        System.out.print("Print TestObj val: ");
        print(obj);
        System.out.print("Changing single int value: ");
        changeValue(value); //  no effect
        print(value);
        System.out.println("Changing array int values: ");
        changeValues(values); // effected
        print(values);
        System.out.println("Changing array value of reference: ");
        changeRefValues(values); // no effect
        print(values);
        //
        System.out.println("Changing val of TestObj");
        changeVal(obj); // effected
        print(obj);
        System.out.println("Changing TestObj value of reference");
        changeRef(obj); // not effected
        print(obj);
    }

    static void changeValue(int value){
        value *= 2; // Primitives are directly passed as value, so this wont effect the passed value. 
    }

    static void changeValues(int[] values){
        for(int i = 0;i<values.length;++i) {
            values[i] *= 2; //You are working on the value of the reference that is passed. The passed int[] is effected
        }
    }

    static void changeRefValues(int[] values){
        values = new int[]{0,0,0,0}; // you change the value of the reference that is passed. The passed int[] is not effected
    }

    static void changeVal(TestObj obj) {
        obj.val *= 2; // You are working on the value of the reference that is passed. The passed TestObj is effected
    }

    static void changeRef(TestObj obj) {
        obj = new TestObj(30); // You change the reference, but since it is passed as value it has no effect on the passed TestObj
    }

    // Only used to print values from here
    static void print(int[] values) {
        for (int i : values) {
            print(i);
        }
    }

    static void print(int i) {
        System.out.println(i);
    }

    static void print(TestObj obj) {
        System.out.println(obj.val);
    }

}

output :/

Print single int: 1
Print int array:
1
2
3
4
Print TestObj val: 15
Changing single int value: 1
Changing array int values: 
2
4
6
8
Changing array value of reference: 
2
4
6
8
Changing val of TestObj
30
Changing TestObj value of reference
30
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33