• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

explain me (double)

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when the following code gets executed:

class TechnoSample
{
public static void main(String[] args)
{
double d1 = 1.0;
double d2 = 0.0;
byte b =1;
d1 = d1/d2;
b = (byte) d1;
System.out.print(b);
}
}

(1) It results in the throwing of an ArithmeticExcepiton
(2) It results in the throwing of a DivideByZeroException
(3) It displays the value 1.5
(4) It displays the value �1


Answer : -------------------------
4

Please explain me answer.

Pankaj Shinde
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is a special case that will probably not be on the exam.

The question is how to narrow a double value of Infinity (which results from double division by zero) to type byte. The process is described under JLS 5.1.3 Narrowing Primitive Conversions.

Basically, the double value is first converted to type int. Infinity is a special case in which the int's maximum value is used. In binary, this is 0111 1111 1111 1111 1111 1111 1111 1111. Next, the int is converted to a byte by discarding all but the 8 rightmost bits. This leaves 1111 1111, which is -1.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!