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?
- f1 == f2
- f1 == f3
- f2 == f1[1]
- x == f1[0]
- f == f1[0]
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.
Parastandon said:
1 decade ago
Why [d] is not correct?
Praveen srivastava said:
1 decade ago
why [D] is not correct?
why f1=f2 not true?
why f1=f2 not true?
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.
" == " 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:
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,
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:
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.
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:
6 years ago
Why option 1 is wrong? Please explain.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers