Java Programming - Operators and Assignments - Discussion

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;
    }
}

[A]. 12 15
[B]. 15 15
[C]. 3 4 5 3 7 5
[D]. 3 7 5 3 7 5

Answer: Option B

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


Nagarjuna said: (Jun 30, 2010)  
Can any one tell me why a1[] will also change.

Ram said: (Dec 25, 2010)  
The method fix()is fixed the is a3[1]=7, so that value is assing by the position of a[1]=7.

Vasu said: (Jan 4, 2011)  
While program is executing
ofter second step
the method of fix(_)
then it comes again back to 3 step.
so it changes the array elements in the position of 1
i.e (a1[1])

Vagmita said: (Jul 1, 2011)  
I cant understand fix... method...

Sainath said: (Jul 8, 2011)  
Can anyone explain me in clear way?

Gopi said: (Jul 12, 2011)  
@Naga. It's because of reflection in referenced object.

Vijay said: (Aug 13, 2011)  
3 elements should be printed know why only 2 elements are being printed
When I give like this

System.out.print("a1[0]"+a1[0] +"a1[1]"+ a1[1] +"a1[2]"+ a1[2] + " ");

Then it will give the result as
a1[0] = 3
a1[1] = 7
a1[2] = 5 this is why.

Prudhvi Kumar said: (Aug 30, 2011)  
Please any one explain me output for this program and fix method in-detail.

Sarang said: (Sep 11, 2011)  
a1[0]=3,a1[1]=7,a1[2]=5;a2[0]=3,a2[1]=7,a2[2]=5
a1[0]+a1[2]+a1[3]=15 & a2[0]+a2[2]+a2[3]=15 ..

Arnab said: (Oct 27, 2011)  
I didn't understand....the arguement in fix() in the calling function is a1 whose values are copied in a3. So why the hell a1 gonna be updated if a3 is changed! Besides the return keyword returns the updated value to a2 not to a1. .....!!!!!!

Debojyoti Bose said: (Jan 17, 2012)  
The situation, when working with arrays, is somewhat different. If we were to make copies of arrays to be sent to methods, we could potentially be copying very large amounts of data.
Not very efficient!
Arrays are passed-by-reference. Passing-by-reference means that when an array is passed as an argument, its memory address location is actually passed, referred to as its "reference". In this way, the contents of an array CAN be changed inside of a method, since we are dealing directly with the actual array and not with a copy of the array.

int [ ] num = {1, 2, 3};
testingArray(num); //Method call
System.out.println("num[0] = " + num[0] + "\n num[1] = " + num[1] + "\n num[2] =" + num[2]);
. . .

//Method for testing
public static void testingArray(int[ ] value)
{
value[0] = 4;
value[1] = 5;
value[2] = 6;
}
Output:
num[0] = 4
num[1] = 5
num[2] = 6
(The values in the array have been changed.
Notice that nothing was "returned".)
You will need to be careful when sending an array to a method. Remember that any changes made to the array in the method will change the data in the original array. Be sure that your intention is to change the original data (thus losing the original data).

Nadeem said: (Mar 5, 2012)  
How is 15 printed even if we are not performing addition of a1 and a2 elements anywhere?

Veerraju said: (Mar 14, 2012)  
Please tell me fix() method from which class and which package.

Abdul Sattar said: (Mar 19, 2012)  
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..........?

Suresh said: (Jul 16, 2012)  
@Abdul.

Numeric values will be added in the print statements.

Jegathees said: (Dec 28, 2012)  
Why & how to change a1[1] value is 7?

Vikas Verma said: (Jan 24, 2013)  
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?

Pranjal Shukla said: (Jun 27, 2013)  
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;
}
}

Ramesh said: (Feb 22, 2014)  
It's all because of the cloning of the same array object reference this is the concept.

Shobu said: (Jun 5, 2014)  
In java + is used for concatenation. Then why will the algebraic addition of the elements in array take place?

Anandi said: (Aug 13, 2014)  
The answer is 375 375. Then how it is 15 15 please explain me?

Mk Dino said: (Sep 14, 2014)  
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.

Beeram Siva Damodar Reddy said: (Jun 18, 2015)  
//**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**//.

Meera said: (Jun 21, 2016)  
Thank you all for the given explanations.

Lavanya said: (Jun 27, 2016)  
Out put: 3 4 5 3 7 5.

Because + are concatenate operator.

Riya said: (Jul 7, 2016)  
Please help me out that when a (+) operator is used for concatenating and for addition purpose.

Anjali said: (Jul 18, 2016)  
When both operands are of the numerical type then the operation is an addition.

If both operands are of string type then it is a concatenation.

Gaurav Gupta said: (Aug 29, 2016)  
Long [] a1 = {3, 4, 5};

Long [] a2 = fix (a1) ;

As we know an array itself is a pointer. Here the address of a1 is stored at a2. Therefore when the a1 is changed it will be reflected globally and therefore both have same data.

Krishna Murthy said: (Oct 3, 2016)  
In C, we have pointers to get the address of a variable. But in java pointers concept is not there but java has indirectly using pointers such as objects. Objects are nothing but references. So the array is a class of object in java. That's why a1 affected by the method fix().

Sarah Glory Olivia said: (Oct 6, 2016)  
Here, fix(a1) is for a2, so why a1[1] in a[1] change to 7 while a1 doesn't use fix method?

Navneet said: (Jun 30, 2017)  
What is role of fix()?

Navneet said: (Jun 30, 2017)  
What is role of fix()?

Navneet said: (Jun 30, 2017)  
What is role of fix()?

Mahesh said: (Jul 5, 2017)  
According to me,

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);// In this line we are sending the reference variable of a1.
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}

long [] fix(long [] a3) // Now a3 also starts pointing to the same array as a1 is pointing.
{
a3[1] = 7;// Any modification done now will reflect on both a1..
return a3;//After returning this value array a2 starts will start to point a1 array as well.
}
}

Suraj Kumar said: (Jul 14, 2017)  
fix() is from which package? Please explain.

Prashant said: (Jul 26, 2017)  
long[] fix(long[] a3) { // a3=a1 and a1= {3,4,5}
a3[1] = 7; // a3[1]=7 means a1[1] will become 7(a1[1]=7), now a1={3,7,5}
return a3;// return a1
}

Rishikesh Joshi said: (Nov 10, 2017)  
How the value of A1[1] has changed? explain.

Krishna Mohan said: (Feb 1, 2018)  
@ALL.

Refer this code.

public class PassA {

public static void main(String[] args) {
PassA p = new PassA();
p.start();
}
void start()
{
long a1[]={3,4,5};
for(long i:a1)
System.out.print(i+"" );
System.out.println(" ");
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 [] a1)
{
a1[1] = 7;
for(long i:a1)
System.out.print(i);
System.out.println(" ");

return a1;
}
}

Output
345
375
15 15

Hari said: (Feb 6, 2018)  
Can anyone explain fix method in detail?

Rajni said: (Feb 9, 2018)  
Please tell me how to work bubble sort method with easy program? Please explain with an example.

Salik Khan said: (Mar 13, 2020)  
It's because in java array is passed by reference, so any changes in the method reflect in the actual array.

Sowmya said: (Mar 15, 2020)  
long[] a1={3,4,5};
long[] a2=a1;

This is equal to,
a1[0]=a2[0] and a1[1]=a2[1] and a1[2]=a2[2].

And in fix method, a3 is method local variable it is not an array.
So changes made to a1 is also reflected in a2 so the answer is 15 15.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.