Page 1 of 1
About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Thu Nov 10, 2016 5:43 am
by nikitos
How does it parse directly double value?
Float.valueOf("12.3");
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Thu Nov 10, 2016 10:59 am
by admin
Not sure I understand your question. "12.3" is not a double. It is a String.
Paul.
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Mon Jan 30, 2017 2:18 am
by qmwuzapz
admin wrote:Not sure I understand your question. "12.3" is not a double. It is a String.
Paul.
i have the same question as nikitos.
because you cannot do like
EDIT:
found this:
Float (Java Platform SE 7 ) wrote:The numerical value of the input string is converted directly to the target floating-point type. In general, the two-step sequence of conversions, string to double followed by double to float, is not equivalent to converting a string directly to float. For example, if first converted to an intermediate double and then to float, the string
"1.00000017881393421514957253748434595763683319091796875001d"
results in the float value 1.0000002f; if the string is converted directly to float, 1.0000001f results.
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Fri Sep 22, 2017 6:24 am
by amit4sun
Need some help with the same concept.
How does compiler catch this :- Integer intObj = 123.3;
But not this :-
Float f = null;
f = Float.valueOf("12.3");
String s = f.toString(); int i = Integer.parseInt(s); // One reason i can think of is, output of toString() is not decided till runtime ?
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Fri Sep 22, 2017 7:13 am
by admin
Yes, compiler doesn't know the value of s because the compiler can't execute any code. That is why the compiler does not know that s points to a String that cannot be parsed into an int.
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Thu Feb 01, 2018 9:09 pm
by Pierce
Gentlemen,
I'll dare to ask this question again since there were no answer provided yet.
Yes, we understand that "12.3" is not a double. It is a String.
But the value it represents - 12.3 - is double, not float.
How come that during the convertion from string 12.3 it becomes a float?
Thank you.
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Thu Feb 01, 2018 10:24 pm
by admin
Because the method that parses the String argument and converts it to float is coded to accept a String that doesn't end with f. Here is what the
JavaDoc API description of Float.valueOf (which is what Float.parseFloat invokes internally) says:
Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (1.0f is a float value; 1.0d is a double value), do not influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type.
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Sun Feb 11, 2018 8:53 pm
by Pierce
Okay, now it's more clear.
Thank you.
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Fri Mar 16, 2018 11:05 am
by Rick84
This is another question where the answers help find the solution;
The first two answers are:
12
13
But the code says:
So those answers are never correct. I think they should be:
i = 12
i = 13
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Mon Mar 19, 2018 7:50 am
by Meghana
Will the value of f not be unreachable from the catch block? (There were qs in other tests in which we try to print a variable declared in the try block in the catch block and it doesn't compile in these cases.) So I marked trouble: null!
Am I missing something?
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Mon Mar 19, 2018 8:13 am
by admin
Why do you think it is unreachable?
An unreachable statement causes compilation error. So even if you think it is unreachable, why do you think it will print trouble: null?
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Mon Mar 19, 2018 9:50 am
by Meghana
An unreachable statement causes compilation error.
Yes Yes
1. I thought it would print trouble:null because f is initialized to null outside the try block (and so is an instance field).
2. There were questions similar to this in the previous tests.. I don remember the q number, though; where the answer was "compile time error" I guess when we try to access a value from the catch block which was in try. The scenario might have been slightly different.
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Mon Mar 19, 2018 10:34 am
by admin
If there is a compilation error, how will the program run to produce any output?
You really need to go through a good book before attempting mock questions.
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Mon Mar 19, 2018 11:38 am
by Meghana
I actually got your point. Thank you..!
And I have read Kathy Sierra's complete OCA syllabus!

Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Mon Mar 19, 2018 12:30 pm
by admin
You might want to try the one by Jeanne Boyarsky or Mala Gupta.
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Fri Mar 23, 2018 8:24 am
by Meghana
Yupp yup. Thank you!
Re: About Question enthuware.ocajp.i.v8.2.1350 :
Posted: Tue Nov 26, 2019 12:33 am
by creigelde
It is a compile-time error if a statement cannot be executed because it is unreachable. This means that the control flow of your program can't get to that statement, but you assume that they would be. The compiler analyzes the flow, and reports these statements to you as error messages. It is a reliable indicators of logical error in your program.
When the compiler reports an unreachable statement, it typically points you to the statement. When that happens, you can follow the flow of control from top to bottom to discover why the statement can never be reached. There are quite strict rules when statements are reachable in java. These rules are design to be easily evaluated and not to be 100% accurate. It should prevent basic programming errors.