• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sachin Kapoor
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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 ]
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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 ]
 
Radha Kamesh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic