Java Programming - Operators and Assignments - Discussion

Discussion Forum : Operators and Assignments - Finding the output (Q.No. 13)
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;
    }
}
null null 42
0 0 42
0 42 42
0 0 0
Answer: Option
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.

Discussion:
21 comments Page 1 of 3.

Siva said:   6 years ago
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();
}

}

Anup singh said:   1 decade ago
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.

Prashantp224 said:   8 years ago
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.

CyxouD said:   9 years ago
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.

Rakesh said:   1 decade ago
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 .

Mohan said:   8 years ago
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.

Sherif said:   1 decade ago
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).

Ellen said:   9 years ago
I don't understand why the second number is 42? Isn't JAVA pass by value, why did t.x change?

Clarify it.

Hussain said:   1 decade ago
In java all primitive types have there default vale like for int is 0, boolean is false so is with byte.

KSL said:   8 years ago
t.x is made 42 and tt.x also 42. So, t is returned as 42, as well as tt.


Post your comments here:

Your comments will be displayed after verification.