• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"==" Question from Kathy's Book

 
May-Yoong Cheah
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because of automatic numeric promotion , a long is promoted to a float and the comparison works out to true
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Corey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic