Page 1 of 1
[HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]
Posted: Sun Nov 25, 2018 1:43 pm
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?)
Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]
Posted: Sun Nov 25, 2018 9:52 pm
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.
Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]
Posted: Mon Nov 26, 2018 11:45 pm
by Username987654
Thanks
Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]
Posted: Sun Feb 03, 2019 8:09 am
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.
Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]
Posted: Sun Feb 03, 2019 9:25 am
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.
Pg 71, Sec. 3.3.3
Posted: Mon Apr 08, 2019 9:42 am
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;
Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]
Posted: Mon Apr 08, 2019 10:30 am
by admin
You are right. L is missing. Added to errata.
thank you for your feedback!
Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]
Posted: Tue Apr 09, 2019 8:10 am
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.
Re: [HD Pg 89, Sec. 3.3.3 - assigning-float-to-int-or-double-to-long-and-vice-versa]
Posted: Tue Apr 09, 2019 8:57 am
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.