Page 1 of 1

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

Posted: Mon Dec 10, 2012 11:07 am
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.

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

Posted: Mon Dec 10, 2012 4:31 pm
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.

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

Posted: Mon Dec 10, 2012 7:16 pm
by Guest
Wow, didn't know that. Thank you

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

Posted: Mon Jan 20, 2014 3:07 am
by kecker
Yes but wouldn't it implicitly promote the float to a double?

Same as if you wrote:
double i = 1;

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

Posted: Mon Jan 20, 2014 3:21 am
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.

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

Posted: Mon Jan 20, 2014 12:38 pm
by kecker
Ok I think that answers my confusion.

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

Posted: Thu Mar 06, 2014 12:12 am
by fasty23
Isn't it an infinite loop?

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

Posted: Thu Mar 06, 2014 12:29 am
by admin
Yes, it is :) Although not relevant to the question but it should be i++ instead of t++.

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

Posted: Sun May 24, 2015 2:05 pm
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?

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

Posted: Sun May 24, 2015 7:51 pm
by admin
int doesn't have any decimals, so why do you think assigning it to a double will lose precision?

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

Posted: Tue May 26, 2015 1:45 pm
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.

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

Posted: Tue May 26, 2015 7:43 pm
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.

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

Posted: Mon Oct 02, 2017 11:15 am
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?

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

Posted: Mon Oct 02, 2017 11:57 pm
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.