• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Math class

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the following expression return
����������� Math.max(Float.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);

�a.Float.POSITIVE_INFINITY
�b.Double.POSITIVE_INFINITY
�c.runtime Exception

On execution I found that the output is simple 'INFINITY'. Please can anybody tell me why it is so.
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aparna!
Here is a small research program (SRP):

Confusing, no?
There are a few overloaded max methods in Math class, but we are interested in two:
float max (float, float) and
double max (double, double)
When we call max method as Math.max (float, double) float parameter is promoted to double and max (double, double) method is called (double parameter cannot be automatically converted to float, that�s why max (float, float) cannot be called). Type of return parameter is double, that�s why line 2 is printed. When we compare Float.POSITIVE_INFINITY with Double.POSITIVE_INFINITY, float is promoted to double, that�s why first line is printed.
Or more explicitely

To have better idea how INFINITY is represented:

Hope this help a little...
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic