• 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

Double class

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double d = new Double(1/0.);

please tell why are we using decimal after writing 1/0
if i don't use it an error is generated.why?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because 1/0 is an integer literal, and class Double does not have a constructor that takes an integer literal. If you add a decimal point, it's a floating point literal, and Double does have a constructor that takes a double.

Edit: Hmmm, it seems I'm mistaken. Both 1/0 and 1/0. compile, but when you run the program, the version with 1/0 gives an ArithmeticException, while 1/0. does not (it prints "Infinity" when you print the value of the double. (I'm using Java 1.5).
[ January 10, 2006: Message edited by: Jesper de Jong ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the type of the expression (int / int) is also int.
division by zero does not have an int representation, which is a problem... the java designers decided to handle this situation with a runtime exception.

the type of the expression (int / double) is double.
in the double, there is a dedicated representation for infinity (Double.POSITIVE_INFINITY). in this case, the language designers decided to use this as the result for dividing by zero (although to my knowledge, this is mathematically incorrect; but I might be mistaken).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic