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;
}
}
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();
}
}
{
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();
}
}
Ashish said:
8 years ago
string is immutable in java.
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.
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.
{
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.
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.
Likhith said:
8 years ago
Here, both t.x and tt.x are changed to 42.
Sanjyoti said:
9 years ago
Can anyone explain it step by step, please?
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.
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.
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.
Clarify it.
Sowmya said:
10 years ago
Can you explain clearly regarding the logic?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers