About Question enthuware.ocajp.i.v7.2.984 :
Moderators: Site Manager, fjwalraven
About Question enthuware.ocajp.i.v7.2.984 :
How is a floating point hexadecimal value written? I thought 0x1 could be represented as a floating point number? Isn't 0x1 equal to 1 in base 10 decimal? If it is 1 in base 10 would its floating point representation be 1.0f? 0_o
Re: About Question enthuware.ocajp.i.v7.2.984 :
I found this from the java language specification (JLS) -
When I put a P and some arbitrary number and end the hex String with the f for floating point it runs and does not encounter a number format exception (NFE).
For hexadecimal floating-point literals, at least one digit is required (in either the whole number or the fraction part), and the exponent is mandatory, and the float type suffix is optional. The exponent is indicated by the ASCII letter p or P followed by an optionally signed integer.
When I put a P and some arbitrary number and end the hex String with the f for floating point it runs and does not encounter a number format exception (NFE).

Code: Select all
// modified code to run and experiment with these concepts
// :D
public class Zfloat {
public static void main(String[] args) {
System.out.println(new Zfloat().parseFloat("0xfP2f"));
}
public float parseFloat(String s) {
float f = 0.0f;
try {
f = Float.valueOf(s).floatValue();
return f;
}
catch (NumberFormatException nfe) {
System.out.println("Invalid input " + s);
f = Float.NaN;
return f;
}
finally { System.out.println("finally");return f; }
}
}
Re: About Question enthuware.ocajp.i.v7.2.984 :
How come the command "return f ;" is executed before the println command? I thought when an exception occurs the control goes to a catch block without executing the rest of the code in the try block.
Also, I want to ask the commands that occur after the finally block are unreachable?
Also, I want to ask the commands that occur after the finally block are unreachable?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.984 :
There is a finally block in the code. The println is coming from there.Michailangelo wrote:How come the command "return f ;" is executed before the println command? I thought when an exception occurs the control goes to a catch block without executing the rest of the code in the try block.
No, not necessarily. It depends on the code in try and catch blocks. For example, in this question, the try and catch both have return statements. Therefore, there is no way anything after the finally block can be executed.Also, I want to ask the commands that occur after the finally block are unreachable?
But in the following example, statement after finally is not unreacheable:
Code: Select all
public void m1(){
try{
System.out.println("in try");
}finally{
System.out.println("in finally");
}
System.out.println("after finally...reachable.");
}
Re: About Question enthuware.ocajp.i.v7.2.984 :
Hi,
I don't know how to write hexadecimal floating literals... is it important to learn for the exam..?
Please advise...
I don't know how to write hexadecimal floating literals... is it important to learn for the exam..?
Please advise...
-
- Posts: 15
- Joined: Thu Dec 13, 2012 9:44 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.984 :
Hi all,
I just wanted to clear my concept, so there should not be any code or block of statements after finally block. Incase if it exists the code will not compile. Is it generalised?
I just wanted to clear my concept, so there should not be any code or block of statements after finally block. Incase if it exists the code will not compile. Is it generalised?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.984 :
No, what you said is not correct. This is exactly I explained in my previous post.deepa.patre wrote:Hi all,
I just wanted to clear my concept, so there should not be any code or block of statements after finally block. Incase if it exists the code will not compile. Is it generalised?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.984 :
No, it is not important for the exam.Deepa wrote:Hi,
I don't know how to write hexadecimal floating literals... is it important to learn for the exam..?
Please advise...
-
- Posts: 1
- Joined: Fri Oct 18, 2013 7:26 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.984 :
So does this mean that when return statement is given in the finally block the code after the finally block will become unreachable and the compiler will complain whereas if return statement is there only in try and catch blocks then the code will compile smoothly, finally will be executed and the value from try or catch will be returned ??
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.984 :
yes, that sounds right. But I suggest you try out multiple test programs to verify this.
-Paul.
-Paul.
-
- Posts: 21
- Joined: Mon Oct 24, 2016 6:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.984 :
I have to say, that for me code does not compile, it complaints on unreachable return statement at the end (jdk 1.8).
I choose answer - code will not compile. And I think it's right answer.
I choose answer - code will not compile. And I think it's right answer.
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.984 :
That is indeed marked as the correct answer.
-Paul.
-Paul.
-
- Posts: 24
- Joined: Wed Sep 02, 2015 3:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.984 :
I had the answer correct, but for a different reason. The answer states that the code would compile if the "return" after the "finally" block wasn't there. However, the parseFloat method only takes in a String as a parameter, and in the possible answers only ints and doubles are passed to the method. Wouldn't that cause a compilation error as well?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.984 :
The options are not code fragments but just English statements mentioning the data that is being passed to the method. But I understand why it might cause confusion and I have updated the options to include quote marks around the input number.
thank you for your feedback!
Paul.
thank you for your feedback!
Paul.
Who is online
Users browsing this forum: No registered users and 11 guests