aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 1) double variable storing hexadecimal value 2)  catching runtime exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "1) double variable storing hexadecimal value 2)  catching runtime exception " Watch "1) double variable storing hexadecimal value 2)  catching runtime exception " New topic
Author

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

Sachin Kapoor
Greenhorn

Joined: Jul 01, 2007
Posts: 19
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
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

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

Joined: May 19, 2007
Posts: 33
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 ]
 
jQuery in Action, 2nd edition
 
subject: 1) double variable storing hexadecimal value 2) catching runtime exception