Java Programming - Operators and Assignments - Discussion

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;
    }
}

[A]. true true
[B]. false true
[C]. true false
[D]. false false

Answer: Option B

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.


Priya said: (Jul 21, 2011)  
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.

Parthiban said: (Sep 28, 2011)  
Anybody have a solution?

Ashok said: (Jan 5, 2012)  
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

Durgaprasad said: (Jan 6, 2012)  
Executing starting from main().

Created object of Test class and calling of start() of Test class.control goes to start() and in start()asign value of b1=false after calling fix(boolean b1) method see below
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
/// b1 local verible of fix()method and it return b1 .store this value into another boolean variaable b2. control goes to in start()method and print the b1 and b2 values .
out put is b1=false, b2= true

Siva said: (Jun 9, 2014)  
Hi @Durgaprasad,

I got you but I have one more doubt regarding the following fragment,

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

void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}

long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}

Ramu said: (Jul 12, 2014)  
Hi @Siva,

long [] a1 is a reference array object means al is not a data type (or) primitive data type.

data types and primitive data types will not change values when you modify in fix method but reference object values will change like

void start()
{
StringBuffer b1 = new StringBuffer("Hello, world");
StringBuffer b2 = fix(b1);
System.out.println(b1 + " : " + b2);
}

StringBuffer fix(StringBuffer b1)
{
b1.append(" Hey");
return b1;
}

Out put : Hello, world Hey : Hello, world Hey // here both are same because b1 is reference object.

Hope you got :)

Jansi said: (Mar 2, 2015)  
I have one more doubt regarding the following fragment,

How you find reference object this program please?

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

void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}

long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}

Emna said: (Feb 9, 2016)  
I have a doubt between both, I didn't understand the notion of reference object, while the result isn't true?

Sijos said: (May 20, 2016)  
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.

Pratik said: (Feb 10, 2017)  
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.

Jayanth said: (Mar 10, 2017)  
But my question is java won't support pass by reference how can a reference variable passed to a method effect an original array?

Kalpesh Somani said: (May 3, 2017)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.