1

I am not able to understand the internal working that is happening behind the screen but I am confused because it is printing different result in console than what it is expected to. Could anybody resolve my problem with good explanation?

public class Demo {

public int [] sort(int h[])
{
    int temp;
    for(int i=0;i<h.length;i++)
    {
        for (int j=i;j<h.length;j++)
        {
            if(h[i]>h[j])
            {
                temp=h[i];
                h[i]=h[j];
                h[j]=temp;
            }

        }
    }
    return h;
}
public static void main(String args[])
{
    Demo obj =new Demo();
    int a[]={9,2,3,5,32,1,5,3,7};

    int[] sorted=obj.sort(a);

    /*Code to Display Array a*/
    for(int s :a)
    {   
     System.out.print(s+ "  ");
    }

     System.out.println("");


    /*Code to Display Array sorted*/
    for(int k:sorted)
    {   
     System.out.print(k+"  ");
    }
}

  /*
   Expected output
               9  2  3  5  32  1  5  3  7
               1  2  3  3  5   5  7  9  32  

   Actual output 
               1  2  3  3  5  5  7  9  32  
               1  2  3  3  5  5  7  9  32  
 */ 


}
Tirthraj Barot
  • 2,671
  • 2
  • 17
  • 33

2 Answers2

0

The sort method is working on the actual input parameter that is passed to the method by call-by-reference (I know this is not 100% correct in terms of wording - see here). That is, all changes to the array h in sortare visible in the calling code as well.

If you don't want to change the input parameter, you need to make a copy of the object. In your case you could use System.arrayCopy to do so.

Community
  • 1
  • 1
dpr
  • 10,591
  • 3
  • 41
  • 71
  • Same method I changed ,with an int but this time why it has not passed by reference. public class prac { public int square(int a) { return a*a; } public static void main(String args[]) { prac obj =new prac(); int a=14; int k=obj.square(a); System.out.print(a+ " "); System.out.println(""); System.out.print(k+" "); } } Out put is 14 196 – Dananjaya Gokhale Jun 11 '16 at 06:40
  • Yes, if you would have read the other question on call-by-reference in Java, you'd know that primitives are not passed by reference but by value. Furthermore you are not altering the input parameter `a` in your example but only using it's value for the computation... – dpr Jun 11 '16 at 06:50
  • I tried with Integer it did pass by value. .Integer is not primitive – Dananjaya Gokhale Jun 11 '16 at 07:00
  • As said, you are not altering the value of `a` your example. Why should there be any changes? Furthermore `Integer` is one of many immutable classes. That is once you created an `Integer` it's not possible the make changes to this object anymore. Other immutable classes are `String`, `Byte`, `Short`, ... – dpr Jun 11 '16 at 07:05
  • You're welcome. Would you mind voting up answers that were helpful to you and perhaps marking the "best" answer as correct, to mark this question as answered... – dpr Jun 11 '16 at 08:00
0

That is because when you are passing the array object,the array int []h refers to the same memory location.

Consider int[]a as an object which has bytes representing the address of the array memory.Now when you pass this array, the same bytes get copied into int[]h so now , h also points to the same memory and therefore that memory gets modified.

yash sachdeva
  • 637
  • 5
  • 13
  • Same method I changed ,with an int but this time why it has not passed by reference. public class prac { public int square(int a) { return a*a; } public static void main(String args[]) { prac obj =new prac(); int a=14; int k=obj.square(a); System.out.print(a+ " "); System.out.println(""); System.out.print(k+" "); } } – Dananjaya Gokhale Jun 11 '16 at 06:39
  • See now int a is not an object, it is a primitive, it stores the bit pattern of 14.The same bit pattern gets passed onto the method and it returns the bit pattern of 196 which gets saved in k. A simple way to remember this is , objects get passed by reference and primitives get passed by value – yash sachdeva Jun 11 '16 at 06:49
  • I tried with Integer it is passing by value .Integer is an Object .how it worked? – Dananjaya Gokhale Jun 11 '16 at 06:59
  • Now bro,that is because Integer class is immutable. when you perform an operation on Integer class,it gets unboxed to int, operations are performed and then again it is boxed to Integer. This boxing , unboxing feature came with Java 5. You'll get similar results with String,Double and other immutable wrapper classes – yash sachdeva Jun 11 '16 at 07:07