• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

Equality of primitives

Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.................
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)


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

byte b = 0x1f;

char c = 'G';

int control = c & b;


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

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



which produces the output:

I hope my post could help you.


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

this is supposed to be a surprise, but it smells like a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic