Java Programming - Operators and Assignments - Discussion

Discussion Forum : Operators and Assignments - Pointing out the correct statements (Q.No. 5)
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]
1, 2 and 3
2, 4 and 5
3, 4 and 5
1, 4 and 5
Answer: Option
Explanation:

(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.

Discussion:
7 comments Page 1 of 1.

Bhargavi said:   1 decade ago
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.

Ameen said:   1 decade ago
@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.

Priyanka said:   5 years ago
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.

Pramod pai said:   1 decade ago
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,

Praveen srivastava said:   1 decade ago
why [D] is not correct?
why f1=f2 not true?

Aishu said:   6 years ago
Why option 1 is wrong? Please explain.

Parastandon said:   1 decade ago
Why [d] is not correct?

Post your comments here:

Your comments will be displayed after verification.