Hi, Give following class: class{ private long val; public AClass(long v){val=v;} public static void main(String args[]){ AClass x=new AClass(10L); AClass y=new AClass(10L); AClass z=y; long a=10L; int b=10; } } which expression result is true? A.a==b; B.a==x; C.y==z; D.x==y; E.a==10.0; the correct answer are: ACE I counldn't understand AE are correct. why? thanks Krussi
If the operands of an equality operator are both of primitive numeric type, binary numeric promotion is performed on the operands (�5.6.2). If the promoted type of the operands is int or long, then an integer equality test is performed; if the promoted type is float or double, then a floating-point equality test is performed.
So, to sum up, a promotion is performed to make the two operands the same data type and then the equality operator is evaluated. That's why A & E are correct. I hope that helps, Corey
When you compare the two values of primitive type (int, long, double, float etc) and if they are of different type, the smaller one is promoted to the biger one and then the comparison is done. In case of choice A variable b is promoted to long (and becomes 10L) and then comparison is done which obviously returns true. Similarly in case of E, variable a is promoted to double. This is also why you can compare two primitives of different types if (10.0>9) { //something } Hope this helps Chintan
I know it�s only a detail, but the class declaration is missing its name, it should be "class AClass {" and not "class {". Francisco
Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank">here</a> to see an example.
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
a bit of art, as a gift, the permaculture playing cards