Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

explain me (double)

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic