[HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]

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

Moderator: admin

Post Reply
Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

[HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]

Post by Username987654 »

i = f1; //will not compile
should be
i = 1f; //will not compile
? (What's f1? Although that could be the basis of why it won't compile, but I don't think that was the intent here?)

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

Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]

Post by admin »

You are right, that line shouldn't have been there. Also, the comments should say widening instead of narrowing when assigning int to float and long to double.
Added to errata.
If you like our products and services, please help us by posting your review here.


flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]

Post by flex567 »

Can you maybe explain a bit more the part that is bold and add a simple example?
I don't see how could you lose info when assigning an int to double.

The reverse, however, is a different story. Although float and double also do lose information
when you assign an int or a long to them respectively
, Java allows such assignments without a
cast because it is possible to get back the exact same int or long value from a float or a double if
you round them off.

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

Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]

Post by admin »

To understand this, you need to look at the bit patterns of int and float storing the same value. See this code:

Code: Select all

int i1 = Integer.MAX_VALUE;
System.out.println(Integer.toHexString(i1));
int i2 = Integer.MAX_VALUE-1;
System.out.println(Integer.toHexString(i2));

float f1 = i1;
System.out.println(Float.toHexString(f1));
float f2 = i2;
System.out.println(Float.toHexString(f2));
Output:

Code: Select all

7fffffff
7ffffffe
0x1.0p31
0x1.0p31
Observe that the bit pattern of the two int values are different (because they are different numbers) but when you assign them to a float variable, the bit patterns of both the floats is same. This means, float lost information.


Also, this statement in the book that you referred was changed a couple of months back as follows because the "getting back the same value after rounding off" part was incorrect:
"The reverse, however, is a different story. Although float and double also do lose information when you assign an int or a long to them respectively, Java allows such assignments without a cast nonetheless. In other words, Java allows implicit widening of int and long to float and double
respectively.
JLS contains the following statement in section 5.1.2 regarding this:
A widening primitive conversion from int to float , or from long to float , or from long to double , may result in loss of precision - that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).
So, the actual reason why Java allows this assignment without a cast even though there is a loss of information is not really very clear. It is just how Java designers decided it to work.

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

natasci
Posts: 6
Joined: Fri Mar 15, 2019 4:40 am
Contact:

Pg 71, Sec. 3.3.3

Post by natasci »

Hello, in my version of a book (build 16.0, 4th Mar 2019), page 71, there is a line and it does not compile. "L" after literal lacks, I think.
long g = 922337203685477580; //Long.MAX_VALUE;

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

Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]

Post by admin »

You are right. L is missing. Added to errata.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]

Post by flex567 »

Does everything mentioned in chapter 3.3 is true for wrapper objects as well?
For example explicit casting for Float to Integer wrapper class is required?
I didn't find that mentioned anywhere.

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

Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]

Post by admin »

Section 3.3.3 - " "Assigning value of one type to a variable of another type" says the following on page 68 -
In all of the cases listed above, I showed you how to assign a value of one type to a variable of the same type, i.e., an int value to an int variable or a Student object to a Student variable. But it is possible to assign a value of one type to a variable of another as well. This topic is too broad to be
covered fully in this chapter because the rules of such assignments touch upon multiple concepts. I will cover them as and when appropriate. Let me list them here first:

1. ....

2. ....

3. Assignments involving reference types - This expands the scope of casting to reference types. I will discuss this in the “Working with Inheritance - II” chapter.
So, the concepts discussed in the “Working with Inheritance - II” chapter will be applicable to explicit casting for Float to Integer wrapper classes (because they are reference types). Based on the concepts discussed here, you cannot assign an Integer object to a Float variable.

May be this is something that can be explained explicitly.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests