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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »

The right answer (double only) follows an explanation:
"There is no need for analyzing the whole code. XXX amount = 1000.0; will be valid only if XXX is double."

Could you elaborate on float and double, I thought float is valid too. Seems I am missing something important.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

When you write a floating point number without 'f', it is considered a double. In this case, 1000.0 is actually a double and not a float, so it can't be assigned to a variable of type float. To assign it to a float variable, you need to write 1000.0f or 1000.0F
HTH,
Paul.

Guest

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

Post by Guest »

Wow, didn't know that. Thank you

kecker
Posts: 18
Joined: Mon Jan 13, 2014 5:39 am
Contact:

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

Post by kecker »

Yes but wouldn't it implicitly promote the float to a double?

Same as if you wrote:
double i = 1;

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

kecker wrote:Yes but wouldn't it implicitly promote the float to a double?

Same as if you wrote:
double i = 1;
It is not clear what you mean.
Do you mean float amount = 1000.0; ?
If so, as explained above, 1000.0 is a double, which is bigger than float. Implicit promotion happens from smaller to bigger, not the other way round.

kecker
Posts: 18
Joined: Mon Jan 13, 2014 5:39 am
Contact:

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

Post by kecker »

Ok I think that answers my confusion.

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

Isn't it an infinite loop?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Yes, it is :) Although not relevant to the question but it should be i++ instead of t++.

crucidal
Posts: 2
Joined: Sun May 17, 2015 3:05 pm
Contact:

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

Post by crucidal »

Within the loop: I thought that arithmetic operations like these all implicitly cast the variables to ints.
Even though an int fits into a double... it doesn't store any decimals. Don't you lose precision by performing an operation like this?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

int doesn't have any decimals, so why do you think assigning it to a double will lose precision?

crucidal
Posts: 2
Joined: Sun May 17, 2015 3:05 pm
Contact:

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

Post by crucidal »

Code: Select all

double x = 10.0;
double y = 3.0;
double d = x/y;
if a division is implicitly casted to an int then the following block of code is equal to the one up above

Code: Select all

double x = 10.0;
double y = 3.0;
double d = (int)(x/y);
However, 10.0/3.0 = 3.33 and the implicit cast would make the outcome 3...thus assigning

Code: Select all

 double d = 3
the loss of precision happens during the implicit cast from double to int.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

I am not sure I understand your question. The two blocks are not equivalent. The first one is a division involving doubles. There is no implicit cast to int there. The value of x/y in this case is a double.
In second, by applying (int), you are doing an explicit cast. You are telling the compiler that you are fine with losing the value after decimal.

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

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

Post by JuergGogo »

Regarding implicit casting from small to big: long (64bit) --> float(32bit) is working fine.

long l = 1_000_000_000;
float f = l;

Why does this code work?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Because as per section 5.1.2 of JLS, it is permitted.
The JLS does not mention the reason so one can contemplate about the reasons. There are many such cases where an an argument can be made for or against allowing or disallowing something. Ultimately, the language designers have to take a call after weighing all the arguments.
We try to form some logical explanation to make it easy to remember the rule in our mind so that we can answer questions in the exam, but ultimately, in many cases, the reason is simply because the JLS says so :)

HTH,
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests