Java Programming - Operators and Assignments - Discussion

Discussion Forum : Operators and Assignments - Finding the output (Q.No. 1)
1.
What will be the output of the program?
class PassA 
{
    public static void main(String [] args) 
    {
        PassA p = new PassA();
        p.start();
    }

    void start() 
    {
        long [] a1 = {3,4,5};
        long [] a2 = fix(a1);
        System.out.print(a1[0] + a1[1] + a1[2] + " ");
        System.out.println(a2[0] + a2[1] + a2[2]);
    }

    long [] fix(long [] a3) 
    {
        a3[1] = 7;
        return a3;
    }
}
12 15
15 15
3 4 5 3 7 5
3 7 5 3 7 5
Answer: Option
Explanation:

Output: 15 15

The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object.

So Output: 3+7+5+" "3+7+5

Output: 15 15 Because Numeric values will be added

Discussion:
43 comments Page 3 of 5.

Beeram Siva Damodar Reddy said:   1 decade ago
//**Here when we call fix (a1) method then control will goes to fix method 7 execute it what the result is a1 is passed as a parameter to the fix (long[] a3).

So a3 is replaced by a1 & a3[1] = a1[1] = 7 value will be returned to its calling method. So, finally we will get a1[1] = 7.

I hope my explanation is easy to understand**//.

MK Dino said:   1 decade ago
The reason why is because arrays are not primitive types.

Primitive types: pass copy of variable to parameter.

Objects(user defined types): pass by reference variable/objects as parameter.

There are exceptions to arrays(int[],long[], etc).

Formally speaking, an array is a reference type, though you cannot find such a class in the Java APIs.

Anandi said:   1 decade ago
The answer is 375 375. Then how it is 15 15 please explain me?

Shobu said:   1 decade ago
In java + is used for concatenation. Then why will the algebraic addition of the elements in array take place?

Ramesh said:   1 decade ago
It's all because of the cloning of the same array object reference this is the concept.

Pranjal shukla said:   1 decade ago
Please try this code, then you will come know that all array a1, a2, a3 having same memory reference which is display by,

sys.out.println("a1="+a1);

That's why when we change value of second element (1 index) to array a3 it will reflected to all other array objects a1 and a2.

Because memory address or reference to a1, a2, a3 is same..for me it is,

a3 = [J@1f33675.
a1 = [J@1f33675.
a2 = [J@1f33675.

And as we know all writing operation perform to some memory location.so we write some memory location for a3.

That's why value 7 is updated to all arrays a1, a2, a3 because we write at some memory location and a1, a2, a3 pointing the same memory location.

So key point is "Reference is same".

Now o/p is.

a1 = 3,7,5
a2 = 3,7,5
a3 = 3,7,5

Hence o/p is 15, 15.

class PassA
{
public static void main(String [] args)
{

PassA p = new PassA();
p.start();
}

void start()
{
long [] a1 = {3,4,5};

long [] a3=a1;
System.out.println("a3=" +a3);
System.out.println("a1=" +a1);

long [] a2 = fix(a1);
System.out.println("a2=" +a2);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}

long [] fix(long [] a3)
{

a3[1] = 7;
return a3;
}
}

Vikas verma said:   1 decade ago
How can the value of a1[1] is change in this prog ?

And a1[] is not given equal to a3[] anywhere in this program.

Can any one Explain this?

Jegathees said:   1 decade ago
Why & how to change a1[1] value is 7?

Suresh said:   1 decade ago
@Abdul.

Numeric values will be added in the print statements.

Abdul Sattar said:   1 decade ago
If we write this:

long [] a2 = a1;

which means the address of a1 is being assigned to a2,therefore a2 also contains same elements as a1.

long [] a2 = fix(a1);

the fix(a1); is only replacing the element at a1[1] which is 4 by 7, and assigning the address of a1 to a2n now both a1 and a2 contains same data elements.

up to this point a1 should contains {3,7,5} not {3,4,5}

so the output should be 375 375

why 15 15..........?


Post your comments here:

Your comments will be displayed after verification.