• Post Reply Bookmark Topic Watch Topic
  • New Topic

Possible lossy conversion from int to byte  RSS feed

 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

From Mala Gupta's OCAJP 8 book, page 213:

Q3-3. Which of the following methods correctly accepts three integers as method
arguments and returns their sum as a floating-point number?

Answer: B, D

When I try to replicate the code from option B, I get the following compilation error (the same happens:


CastDouble.java:6: error: incompatible types: possible lossy conversion from int to byte
                   double result = castDouble.addUp(1, 2, 3, 4);
                                                                    ^

CastDouble.java:12: error: incompatible types: possible lossy conversion from int to byte
                   return sum;
                             ^


My code:


Can anyone explain how answers B and D are considered correct, when they don't compile? Is there something I am overlooking?

Thanks in advance for your help

Regards,
Shane
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction: the addUp() method looks like this:


Also, I added a long argument to my method to check whether that would work as well.

PS. I was meaning to say 'the same happens when I replicate answer D'.
 
Tamas Szekeres
Greenhorn
Posts: 6
6
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shane,

The problem's not with the method but the way you're trying to invoke it. If you say 4, Java will think, okay, this is an int. If you say 4L, Java will say, I see you want to have a long, so that's going to be a long. If you say 4.0, Java will think, okay, this is a double. So when you're calling what Java sees is this:And yes, this is incorrect as the addUp method is expecting a byte, an int, a short and a long respectively. Casting up is okay, so an int is implicitly going to be cast to a long, when needed. However, casting down requires an explicit cast. So there are two ways to go here. Your fist option is that you do the downcasts when you call the method:The second is to declare the variables with the appropriate type:It's important to be aware of the fact that when you're assinging a value to a variable, like in lines 1-4 in the code snippet above, Java knows that you want the number 1 to be a byte, and number 3 to be a short. It even does range verification, so these lines will not compile:But when you're passing a raw number to a function as a parameter, the number is not going to be aligned up with the type in the function's signature.
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Tamas! I had not thought of it like that at all. Very clear and detailed explanation.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Still, this doesn't really explain why the answers are correct... the multiple choice question, doesn't mention that the values would be casted, or that there will be an assignment to a variable whose type matches, prior to the call.

Perhaps, in this case, the word "integers" doesn't actually mean the int data type, but more in the general sense, meaning accepting a whole number value?

Henry
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Still, this doesn't really explain why the answers are correct... the multiple choice question, doesn't mention that the values would be casted, or that there will be an assignment to a variable whose type matches, prior to the call.

Perhaps, in this case, the word "integers" doesn't actually mean the int data type, but more in the general sense, meaning accepting a whole number value?

Henry

Good point!

I can't imagine it means 'whole number'. That would be very confusing.

Does anyone have ideas?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the description should have been something like

"when invoked with three integers of the correct type"

I don't know this book though, so it is possible that the intention is obvious, given the context.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not on the official errata overview of the study guide, so I asked the author if she could share here thoughts on this question. Hopefully to be continued!
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:It's not on the official errata overview of the study guide, so I asked the author if she could share here thoughts on this question. Hopefully to be continued!

Hi Roel,

Sorta related question: Do you happen to know where I can find the errata overview for Mala's book?

Thanks again

Shane
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The official study guide webpage (on the publisher's website) contains a link to the (I assume) official errata overview.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!