• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

long to double conversion

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 /?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Happily living in the valley of the dried frogs with a few tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic