Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 2)
2.
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
Answer: Option
Explanation:
The boolean b1 in the fix() method is a different boolean than the b1 in the start() method. The b1 in the start() method is not updated by the fix() method.
Discussion:
17 comments Page 1 of 2.
Sidh said:
5 years ago
See, the b1 in the start block is assigned to the b1 (in printing), but in b2=fix (b1) the b1 value of fix block is assigned and so in printing first false is printed and then true is printed.
(1)
Amanpreet said:
6 years ago
@All.
How both boolean are different if both have the same name?
fix(boolean b1){
b1=true
return b1
}
How both boolean are different if both have the same name?
fix(boolean b1){
b1=true
return b1
}
(1)
Sijos said:
9 years ago
a1 is the reference object of the long array. It will change the value by fix method.
b1 is datatype it will not change the value by fix method.
Look at the difference between the reference object and datatype.
b1 is datatype it will not change the value by fix method.
Look at the difference between the reference object and datatype.
Gurpreet Kaur said:
4 years ago
I think the difference is because b1 was a local datatype variable of the start method. So the change of the value of the variable in one method won't affect its value in the other method.
While a1 was an array object and so its value changed.
While a1 was an array object and so its value changed.
Akhilabanala said:
7 years ago
The main difference is b1 in one method is local variable of that method. Similarly the other b1. So the change in local variable of one method will not affect the variable of another method.
Sanoj said:
8 years ago
But my question is java won't support pass by reference how can a reference variable pass to a method effect an original array?
I also have the same doubt. Please clarify clearly.
I also have the same doubt. Please clarify clearly.
Kalpesh somani said:
8 years ago
The difference between the definition of an array and ordinary variable is the array is always declared, initialized, and accessed using subscript whereas ordinary variable does not have any subscript.
For that reason in the previous example we are referring to object & in this example, we are referring to the ordinary variable.
So that reason. Array is the set of multiple values where as a variable can store a single value at a time.
For that reason in the previous example we are referring to object & in this example, we are referring to the ordinary variable.
So that reason. Array is the set of multiple values where as a variable can store a single value at a time.
Jayanth said:
9 years ago
But my question is java won't support pass by reference how can a reference variable passed to a method effect an original array?
Pratik said:
9 years ago
class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
Here, b1 is primitive data type, not an object so when passing it through fix its value will not change.
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
Here, b1 is primitive data type, not an object so when passing it through fix its value will not change.
Priya said:
1 decade ago
Priya: the variable after start() is assigned to be false.
Here many b1s here & there makes confusion. b1 which is the parametre of fix()is entirley different from previous one.
Here many b1s here & there makes confusion. b1 which is the parametre of fix()is entirley different from previous one.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers