posted 6 years ago

Equality of primitives..............

output is true.............

It is known to me when doing == it will compare the bit patterns for 5.0 and 5L............

but only the concern is whether the bit pattern for two values are same...........as one is double and other is long.................

output is true.............

It is known to me when doing == it will compare the bit patterns for 5.0 and 5L............

but only the concern is whether the bit pattern for two values are same...........as one is double and other is long.................

SCJP6.0,My blog Ranchers from Delhi

Paulo Cesar Dias Lima

Greenhorn

Posts: 12

posted 6 years ago

Dear Phil Sohar in these case your are comparing 5.0 (double) with (5.0) since 5L was promoted cause binary numeric promotion.

So comparing 5.0 == 5.0 , both double the result is true;

Follows details of explanation form JLS Book reference:

"5.6.2. Binary Numeric Promotion

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary:

If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then:

If either operand is of type double, the other is converted to double.

Otherwise, if either operand is of type float, the other is converted to float.

Otherwise, if either operand is of type long, the other is converted to long.

Otherwise, both operands are converted to type int.

After the type conversion, if any, value set conversion (§5.1.13) is applied to each operand.

Binary numeric promotion is performed on the operands of certain operators:

The multiplicative operators *, / and % (§15.17)

The addition and subtraction operators for numeric types + and - (§15.18.2)

The numerical comparison operators <, <=, >, and >= (§15.20.1)

The numerical equality operators == and != (§15.21.1)

The integer bitwise operators &, ^, and | (§15.22.1)

In certain cases, the conditional operator ? : (§15.25)

An example of binary numeric promotion appears above in §5.1. Here is another:

class Test {

public static void main(String[] args) {

int i = 0;

float f = 1.0f;

double d = 2.0;

// First int*float is promoted to float*float, then

// float==double is promoted to double==double:

if (i * f == d)

System.out.println("oops");

// A char&byte is promoted to int&int:

byte b = 0x1f;

char c = 'G';

int control = c & b;

System.out.println(Integer.toHexString(control));

// Here int:float is promoted to float:float:

f = (b==0) ? i : 4.0f;

System.out.println(1.0/f);

}

}

which produces the output:

7

0.25"

I hope my post could help you.

Regards,

Paulo Lima.

JLS Book Reference:

Java™ Language Specification, Third Edition, The

By James Gosling, Bill Joy, Guy Steele, Gilad Bracha

...............................................

Publisher: Addison Wesley Professional

Pub Date: June 14, 2005

ISBN: 0-321-24678-0

So comparing 5.0 == 5.0 , both double the result is true;

Follows details of explanation form JLS Book reference:

"5.6.2. Binary Numeric Promotion

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary:

If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then:

If either operand is of type double, the other is converted to double.

Otherwise, if either operand is of type float, the other is converted to float.

Otherwise, if either operand is of type long, the other is converted to long.

Otherwise, both operands are converted to type int.

After the type conversion, if any, value set conversion (§5.1.13) is applied to each operand.

Binary numeric promotion is performed on the operands of certain operators:

The multiplicative operators *, / and % (§15.17)

The addition and subtraction operators for numeric types + and - (§15.18.2)

The numerical comparison operators <, <=, >, and >= (§15.20.1)

The numerical equality operators == and != (§15.21.1)

The integer bitwise operators &, ^, and | (§15.22.1)

In certain cases, the conditional operator ? : (§15.25)

An example of binary numeric promotion appears above in §5.1. Here is another:

class Test {

public static void main(String[] args) {

int i = 0;

float f = 1.0f;

double d = 2.0;

// First int*float is promoted to float*float, then

// float==double is promoted to double==double:

if (i * f == d)

System.out.println("oops");

// A char&byte is promoted to int&int:

byte b = 0x1f;

char c = 'G';

int control = c & b;

System.out.println(Integer.toHexString(control));

// Here int:float is promoted to float:float:

f = (b==0) ? i : 4.0f;

System.out.println(1.0/f);

}

}

which produces the output:

7

0.25"

I hope my post could help you.

Regards,

Paulo Lima.

JLS Book Reference:

Java™ Language Specification, Third Edition, The

By James Gosling, Bill Joy, Guy Steele, Gilad Bracha

...............................................

Publisher: Addison Wesley Professional

Pub Date: June 14, 2005

ISBN: 0-321-24678-0

Don't get me started about those stupid light bulbs. |