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;
    }
}
true true
false true
true false
false false
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 2 of 2.

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.

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.

Ashok said:   1 decade ago
Even it is declared as boolean in fix(),it is defined as an ordinary variable.so it couldn't reflect the value in start(). Got it guys

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?

Emna said:   10 years ago
I have a doubt between both, I didn't understand the notion of reference object, while the result isn't true?

Amanpreet said:   6 years ago
@All.

How both boolean are different if both have the same name?

fix(boolean b1){
b1=true
return b1
}
(1)

Parthiban said:   1 decade ago
Anybody have a solution?


Post your comments here:

Your comments will be displayed after verification.