Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Double class  RSS feed

 
AJKC Shekhawat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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 ]
 
Tilo Hemp
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!