Java Programming - Operators and Assignments - Discussion

13. 

What will be the output of the program?

class Two 
{
    byte x;
}

class PassO 
{
    public static void main(String [] args) 
    {
        PassO p = new PassO();
        p.start();
    }

    void start() 
    {
        Two t = new Two();
        System.out.print(t.x + " ");
        Two t2 = fix(t);
        System.out.println(t.x + " " + t2.x);
    }

    Two fix(Two tt) 
    {
        tt.x = 42;
        return tt;
    }
}

[A]. null null 42
[B]. 0 0 42
[C]. 0 42 42
[D]. 0 0 0

Answer: Option C

Explanation:

In the fix() method, the reference variable tt refers to the same object (class Two) as the t reference variable. Updating tt.x in the fix() method updates t.x (they are one in the same object). Remember also that the instance variable x in the Two class is initialized to 0.


Aara said: (Jun 16, 2011)  
Are the objects passed by reference? Java is pass by value right?

Ajaxx said: (Oct 15, 2011)  
Yes Dude, Objects are passed by Reference.

Rakesh said: (Aug 31, 2012)  
There is no concept of pass by reference in java . JAVA is purely pass by value . when objects are passed they are also passed by value . since in the called method and calling method the object the references are pointing is same you get the values reflected in calling method .

Aditi said: (Jun 4, 2013)  
Where is the instance variable x initialized to 0?

Hussain said: (Jun 26, 2013)  
In java all primitive types have there default vale like for int is 0, boolean is false so is with byte.

Anup Singh said: (Aug 2, 2013)  
Q. Why values are not updating here?

class PassB
{
public static void main(String [] args)
{
PassB p = new PassB();
p.start();
}

void start()
{
String name1 = "hello";

String name2 = afix(name1);

System.out.println(name1+" "+name2);
}

String afix(String nm)
{
nm = "bye";
return nm;
}
}

Result: hello bye.

Sherif said: (Mar 23, 2014)  
Two t = new Two();
Two t2 = new Two();
t2=t;

This mean there are two different objects with two different references.
in this case the output will be (0 0 42).

Lavanya said: (Oct 25, 2015)  
Can any explain this program?

Karthi said: (Nov 15, 2015)  
Explain me?

Adhikari said: (Dec 9, 2015)  
Don't understand, can you explain the logic?

Sowmya said: (Feb 16, 2016)  
Then how do we get 42?

Sowmya said: (Feb 16, 2016)  
Can you explain clearly regarding the logic?

Ellen said: (May 8, 2016)  
I don't understand why the second number is 42? Isn't JAVA pass by value, why did t.x change?

Clarify it.

Cyxoud said: (Jun 29, 2016)  
Yes, it pass by value, it passed the REFERENCE VALUE, so created COPY OF THIS VALUE, and it's reference to the same object, when we change it, the object is changed too.

If it would be pass by reference, then, as I understand, we could create a new object: tt = new Two() and then t in the start method would refer to a new object, same as tt.

Sanjyoti said: (Jul 31, 2016)  
Can anyone explain it step by step, please?

Likhith said: (Mar 9, 2017)  
Here, both t.x and tt.x are changed to 42.

Ksl said: (Mar 9, 2017)  
t.x is made 42 and tt.x also 42. So, t is returned as 42, as well as tt.

Prashantp224 said: (Apr 4, 2017)  
class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}

void start()
{
String name1 = "hello";

String name2 = afix(name1);

System.out.println(name1+" "+name1);
}

String afix(String nm)
{
nm = "bye";
return nm;
}
}


In above example, why name1 is not getting updated to "bye".

Why the result is : hello hello?

It should be bye bye.

Mohan said: (Sep 5, 2017)  
Yes, it pass by value, it passed the REFERENCE VALUE, so created COPY OF THIS VALUE, and it's reference to the same object, when we change it, the object is changed too.

Ashish said: (Sep 21, 2017)  
string is immutable in java.

Siva said: (Nov 13, 2019)  
public class Two
{
byte x;

void start()
{
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);//t2=t
System.out.println(t.x + " " + t2.x);//t.x=42 "" t2=42
}

Two fix(Two tt) //fix(two tt)===> Two tt = new Two();===>x=42
{
tt.x = 42;
return tt;
}
public static void main(String [] args)
{
Two p = new Two();
p.start();
}

}

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.