• 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

1) double variable storing hexadecimal value 2) catching runtime exception

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two doubts

1) I thought the following statement would be illegal but it isn't:
double d = 0x12345678;
The K&B book says nothing about double variables holding hexadecimal values. It just says, quote

All three integer literals (octal, decimal, and hexadecimal) are defined as int by default, but they may also be specified as long by placing a suffix of L or 1 after the number


If double can hold hexadecimal values, shouldn't the Double (wrapper) class's valueOf method be overloaded to take base/radix as argument?

2) My second question concerns this:

What letters get written to the standard output with the following code?
class Unchecked {

public static void main(String[] args) {
try {
method();
} catch (Exception e) {
}
}

static void method() {
try {
wrench();
System.out.println("a");
} catch (ArithmeticException e) {
System.out.println("b");
} finally {
System.out.println("c");
}
System.out.println("d");
}

static void wrench() {
throw new NullPointerException();
}
}

Select all valid answers.

1. "a"
2. "b"
3. "c"
4. "d"
5. none of these

The answer is 3 (only "c" is printed), but my question is that since NullPointerException is a RuntimeException, it doesn't have to be caught. So shouldn't the output be "a", "c" and "d"?
[ September 01, 2007: Message edited by: Sachin Kapoor ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If double can hold hexadecimal values, shouldn't the Double (wrapper) class's valueOf method be overloaded to take base/radix as argument?



There is no concept of storing hexidecimal numbers, even for integers. Integer variables stores integer numbers -- the compiler just allows you to define the integer in hexidecimal format.

For doubles, the compiler allows you to implicitedly cast from int to double. So in this case, you are casting an int literal (defined in hexidecimal format) to a double, and storing it in a double variable.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The answer is 3 (only "c" is printed), but my question is that since NullPointerException is a RuntimeException, it doesn't have to be caught. So shouldn't the output be "a", "c" and "d"?



The runtime exception isn't caught by the method() method. That's why "a" and "d" weren't printed. The exception forced the early return. Only the finally clause was able to execute.

Henry
[ September 01, 2007: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry Wong,

That was a very clear explanation..Thanks for the info.. felt really confused on seeing the questions.. now its all clear..

Radha
[ September 03, 2007: Message edited by: Radha Kamesh ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic