• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

long to double conversion

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, here is a confusing question from Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 11362-11365). Enthuware. Kindle Edition. :
What should be the return type of the following method?

Select 1 option
A. int
B. long
C. double
D. float

Correct answer is C. Why? the casr applies only to the byte variable, that's OK, but why is the long automatically converted to double? is that legal? I mean they are 64bits all right, but still...
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,
That's a great question. The long cast only applies to one variable. Not the results of the math.

That method could be re-written as:


We don't normally write it that way because it is verbose. But it shows clearly what is going on!
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne, so basically we're converting the byte to long then because the long is divided by a double java automatically converts that long to a double (just so I get it in my head). Now I seem to understand that a long converted to a double is legal without an explicit cast but I suspect that we couldn't convert a double to a long unless we use an explicit cast.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote: but I suspect that we couldn't convert a double to a long unless we use an explicit cast.

You are correct. Basically, if an assignment would lose information, you have to cast. It tells the compiler you know what you are doing!
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, thanks, sorry I just thought of another thing, in terms of precedence though, is what I said correct in the sense that in (long) by/ d* 3; first by is cast to long, then converted into double - because casting has greater precedence than /?

 
Henry Wong
author
Marshal
Pie
Posts: 22089
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Cool, thanks, sorry I just thought of another thing, in terms of precedence though, is what I said correct in the sense that in (long) by/ d* 3; first by is cast to long, then converted into double - because casting has greater precedence than /?


Since a cast operator has higher precedence than a divide operator (and also the multiply operator), the cast is only applied to the "by" variable, and not to the result of the "by / d * 3" expression.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic