Java Programming - Operators and Assignments - Discussion

Discussion :: Operators and Assignments - Pointing out the correct statements (Q.No.5)


import java.awt.Button;
class CompareReference 
    public static void main(String [] args) 
        float f = 42.0f;
        float [] f1 = new float[2];
        float [] f2 = new float[2];
        float [] f3 = f1;
        long x = 42;
        f1[0] = 42.0f;
which three statements are true?
  1. f1 == f2
  2. f1 == f3
  3. f2 == f1[1]
  4. x == f1[0]
  5. f == f1[0]

[A]. 1, 2 and 3
[B]. 2, 4 and 5
[C]. 3, 4 and 5
[D]. 1, 4 and 5

Answer: Option B


(2) is correct because the reference variables f1 and f3 refer to the same array object.

(4) is correct because it is legal to compare integer and floating-point types.

(5) is correct because it is legal to compare a variable with an array element.

(3) is incorrect because f2 is an array object and f1[1] is an array element.

Parastandon said: (Oct 19, 2010)  
Why [d] is not correct?

Praveen Srivastava said: (Sep 16, 2011)  
why [D] is not correct?
why f1=f2 not true?

Ameen said: (Jul 9, 2013)  
@Praveen f1=f2 in java " = " is an assignment operator.

" == " is used to compare in java.

While speaking logically f2 creates a new reference so f1 is not equal to f2.

I hope you have got it.

Pramod Pai said: (Jul 2, 2014)  
f1 == f3 is wrong because.
After f3 = f1 statement,
f1[0] is made change and equal to 42.0f,

f1[0] = 42.0f,
But f3[0] = 0.0f only,

Bhargavi said: (Jul 31, 2014)  
No pramod you are wrong.
Here f1==f3.

So f1 and f3 both are referring to same object i.e., float array object.

If a value is changed in float array. Then also both f1 and f3 are referring same object know so both are equal.

Aishu said: (Aug 5, 2019)  
Why option 1 is wrong? Please explain.

Priyanka said: (Apr 28, 2020)  
f1 and f2 both are different objects which is stored in the heap area. So the address of both objects will be different. That's why they are not equal.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.