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

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.958 :

Post by ETS User »

Hi,
In this eg. when I try
System.out.println(i - f); i.e w/o the typecast, the value returned is 0.0

But when I do try it as (i-(int)f) it returns -46, so can you help me understand whats happening?
Thanks.

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

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

Post by admin »

You might want to check this out.

Although, you are not required to know about this for the purpose of the exam.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Guest

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

Post by Guest »

Thanks Paul!

The_Nick

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

Post by The_Nick »

Well I think it's because if you do not use the typecast. The operation

Code: Select all

(i - f)
will automatically convert

Code: Select all

i
in float rather than

Code: Select all

f
in int. Therefore it will be something like "IntegerWithLossPrecision - IntegerWithLossPrecision" since the loss of precision it's regular and not random it will give back 0.0 as result.

The_Nick

The_Nick

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

Post by The_Nick »

Hi,
Do you know exactly how many digits does the floating point support ( I am not talking about interpolating).
I read in the link above mentioned that is 7. but actually also

Code: Select all

int i = 12345678 float f = i; System.out.println(i-(int)f);
Gives back 0. So it is 8 or there is a fixed number of digits that we can be sure it is supported by the typecasting from float towards int.

Thanks for the response in advance.

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

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

Post by admin »

Yes, there is a fixed number of bits (not digits) that are used to store the most significant digits of a number. You need to go through Section 4.2.3 on JLS: http://docs.oracle.com/javase/specs/jls ... jls-4.html

NOTE: This is NOT required for the exam.
If you like our products and services, please help us by posting your review here.

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

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

Post by javanaut »

Anyways...

It prints 0.0 which is basically 0 if the explicit cast is not inserted.

This question is tough and I still do not fully grasp when the loss of precision will occur even after reading the stackoverflow article. :( :shock:

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

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

Post by JuergGogo »

Normally the compiler complains about a possible loss of precision. For example a conversion from float to int needs an explicit cast. In the opposite direction - int to float, or long to double - there also could be a possible loss of precison and we don't need an explicit cast: float f = i;
The topic is confusing and inconsistent.

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

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

Post by admin »

JuergGogo wrote:In the opposite direction - int to float, or long to double - there also could be a possible loss of precison and we don't need an explicit cast: float f = i;
The topic is confusing and inconsistent.
Not sure why you think so. Can you show an example where assigning an int to float causes loss of precision?
If you like our products and services, please help us by posting your review here.

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

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

Post by JuergGogo »

https://stackoverflow.com/questions/119 ... -float-why

In summary they say, the range matters not the precision. So implicit casting from int to float may result in loss of precision, but float has the higher range than int.

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

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

Post by admin »

Good point.
Paul.
If you like our products and services, please help us by posting your review here.

vilasa
Posts: 7
Joined: Tue Apr 16, 2019 5:40 pm
Contact:

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

Post by vilasa »

For float I understood from above explanation that 9 digit or greater value of int assigned to float causes loss of precision . Please give example of long to float and long double where loss of precision occurs .I am trying couple of values for long to double it prints 0 .

Dreamweaver
Posts: 32
Joined: Mon Dec 29, 2014 4:14 pm
Contact:

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

Post by Dreamweaver »

Are this kind of question really on exam? because in the same time a greater int value result 0

Code: Select all

        int i =2_000_000_000;
        float f = i;
        System.out.println(i -(int)f);
what should we watch out for?

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

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

Post by admin »

You may not get exactly this but yes, you may get questions that require you to understand comparison of float and int for whole numbers.
If you like our products and services, please help us by posting your review here.

anam_m
Posts: 1
Joined: Thu Sep 03, 2020 5:10 pm
Contact:

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

Post by anam_m »

I am confuse about the choices that are given for this question.
Actual answer is -46, and it is not one of the options, so when I chose none of the above, why is it telling that correct answer is "It will not print 0"?

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

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

Post by admin »

Well, you need tp select the best option. Out of "None of the above" and "It will not print 0", "It will not print 0 is better. It is also factually correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 31 guests