File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ""=="  Question from Kathy Watch ""=="  Question from Kathy New topic

"==" Question from Kathy's Book

May-Yoong Cheah

Joined: Apr 02, 2003
Posts: 8
Given the following,
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? (Choose three)
A. f1 == f2
B. f1 == f3
C. f2 == f1[1]
D. x == f1[0]
E. f == f1[0]
The answers are B,D and E.
The question is why D, that is why you are allow to compare a long with a float?
preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
because of automatic numeric promotion , a long is promoted to a float and the comparison works out to true
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Preeti is right. In this case, we have what is known as binary numeric promotion. You can read all about it in the JLS, sect;5.6.2 Binary Numeric Promotion.

SCJP Tipline, etc.
I agree. Here's the link:
subject: "==" Question from Kathy's Book
jQuery in Action, 3rd edition