This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

Since K&B book talks about toHexSTring() i think toHexString is for exam, But it talks only wrt Integer and Long which are fairly simple to understand. But when i ran the below code

Can someone explain what does 'p' stand for and how did this convertion happen? I did read Java DOC for Double but there is no clear explanation about the conversion. Thanks Deepak

You definitely don't need to know this for the SCJP. Not that that'll stop me from answering your question anyway, though getting the notation right will be hard without superscripts and subscripts.

Firstly, it'll help if you have a general understanding of two things: the IEEE 754 representation for floating-point numbers, and general n-ary fractions. Here are the most essential facts:

You can think of a positive floating-point number in IEEE 754 form as a value of the form (m x 2^e), where m is the significand (a.k.a. mantissa) and e is the exponent.

In base n, the number ab.cdef means (a x n^1) + (b x n^0) + (c x n^(-1)) + (d x n^(-2)) + (e x n^(-3))

Java represents all floating-point values (i.e. float and double) in IEEE 754 form. Therefore, Double.toHexString()'s output will tell you the significand and exponent. "0x1.8p1" means a significand of 1.8 (in hexadecimal fraction form) and an exponent of 1 (hexadecimal integer). 1.8 in hexadecimal is (1 x 16^0) + (8 x 16^(-1)) == 1.5 in decimal. Therefore, 1.8p1 == 1.5 x 2^1 == 2.

Similarly, "0x1.cp2" == ( (1 x 16^0) + (12 x 16^(-1)) ) x 2^2 == 1.75 x 4 == 7

To make this clearer, here are the IEEE 754 signifcand-exponent representations of integers 1 to 10 expressed in binary, decimal, and hexadecimal form (note that only the significands are expressed as non-decimals):

Hopefully this will also give you some idea of how non-integers are represented in IEEE 754. [ November 22, 2007: Message edited by: Kelvin Lim ]