• 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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Doubt on primitives repeated..

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(5.0 == 5L);
System.out.println(5.1 == 5.1F)

the output is true and false respectively.

I want to know whether my concept here is clear or not..
In the first cast long gets promoted to double.
In the second case 5.1 is double and we shorten it to float and there fore cmparison of float and double will return false.
Am i right?
I have also posted my doubts on Covariants and other returns topic..please help me in that also.
Thanks.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kunal Gupta:
System.out.println(5.0 == 5L);
System.out.println(5.1 == 5.1F)

the output is true and false respectively.

I want to know whether my concept here is clear or not..
In the first cast long gets promoted to double.
In the second case 5.1 is double and we shorten it to float and there fore cmparison of float and double will return false.
Am i right?
I have also posted my doubts on Covariants and other returns topic..please help me in that also.
Thanks.



almost.

In the second comparison, both are promoted to double. However, certain decimal values cannot be stored exactly, and so when the float is promoted to a double there is a small difference.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
...certain decimal values cannot be stored exactly...


For details, see Some things you should know about floating-point arithmetic.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always Binary Numeric Promotion rule applies (Khalid Mughal page 46).
When you use 5.0 or 6.0 like numbers, it will always return TRUE, irrespective of primitive data types.
But, when you use 5.1 or 6.1 like numbers, it will always return FALSE,
Checkout this

System.out.println(5.0 == 5.0F);// true
System.out.println(5.1 == 5.1F);// false

Pankaj Shinde
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic