Page 1 of 1

About Question enthuware.ocajp.i.v7.2.984 :

Posted: Tue Sep 25, 2012 8:25 pm
by ETS User
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 :

Posted: Tue Sep 25, 2012 8:43 pm
by Javanaut
I found this from the java language specification (JLS) -
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). :o

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 :

Posted: Sun Sep 30, 2012 3:39 pm
by Michailangelo
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?

Re: About Question enthuware.ocajp.i.v7.2.984 :

Posted: Sun Sep 30, 2012 5:46 pm
by admin
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.
There is a finally block in the code. The println is coming from there.
Also, I want to ask the commands that occur after the finally block are unreachable?
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.
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 :

Posted: Wed Dec 19, 2012 12:50 pm
by Deepa
Hi,

I don't know how to write hexadecimal floating literals... is it important to learn for the exam..?
Please advise...

Re: About Question enthuware.ocajp.i.v7.2.984 :

Posted: Wed Dec 19, 2012 12:59 pm
by deepa.patre
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?

Re: About Question enthuware.ocajp.i.v7.2.984 :

Posted: Wed Dec 19, 2012 2:53 pm
by admin
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?
No, what you said is not correct. This is exactly I explained in my previous post.

Re: About Question enthuware.ocajp.i.v7.2.984 :

Posted: Wed Dec 19, 2012 2:54 pm
by admin
Deepa wrote:Hi,

I don't know how to write hexadecimal floating literals... is it important to learn for the exam..?
Please advise...
No, it is not important for the exam.

Re: About Question enthuware.ocajp.i.v7.2.984 :

Posted: Wed Nov 13, 2013 11:52 am
by Saloni R Dangwal
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 ??

Re: About Question enthuware.ocajp.i.v7.2.984 :

Posted: Wed Nov 13, 2013 12:54 pm
by admin
yes, that sounds right. But I suggest you try out multiple test programs to verify this.
-Paul.

Re: About Question enthuware.ocajp.i.v7.2.984 :

Posted: Mon Nov 07, 2016 4:32 am
by nikitos
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.

Re: About Question enthuware.ocajp.i.v7.2.984 :

Posted: Mon Nov 07, 2016 10:40 am
by admin
That is indeed marked as the correct answer.
-Paul.

Re: About Question enthuware.ocajp.i.v7.2.984 :

Posted: Wed Dec 21, 2016 7:37 am
by heleneshaikh
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?

Re: About Question enthuware.ocajp.i.v7.2.984 :

Posted: Thu Dec 22, 2016 1:30 am
by admin
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.