Page 1 of 1

[HD Pg 182, Sec. 8.1.2 - returning-values-of-different-types-from-a-method]

Posted: Wed Jun 03, 2020 4:48 pm
by addy12345
Hi ,
need some clarity on below sentence from the book .
"Java allows a return value to be a reference to a wrapper class if the return type is of a primitive type of the same or smaller type" .

short m1()
{
return new Integer(1); //compile error
}
above sentence telling that , we can return a wrapper class reference if the return type is primitive type of smaller type? but actually it is not possible .am i missing something?

Re: [HD Pg 182, Sec. 8.1.2 - returning-values-of-different-types-from-a-method]

Posted: Wed Jun 03, 2020 11:27 pm
by admin
You are right. That's a mistake. It should say same or larger type.
The next statement is also incorrect. It should say, "The reverse is also allowed but only when the method's return type is a wrapper of the same type as the type of the primitive value being returned -"

Adding to errata.

thank you for your feedback!