This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method overload

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I modified slightly some code found on Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Location 25753). Enthuware. Kindle Edition and I'm trying different combinations, so I will probably keep posting in this thread as things come up.
First and foremost here is the code so far:



First question is about this line : that prints in float, and I frankly don't understand why. If it's passing 2 long values should it print in double?
 
Henry Wong
author
Marshal
Pie
Posts: 22114
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: I frankly don't understand why. If it's passing 2 long values should it print in double?


When two methods needs to be disambiguated, it uses the "most specific method" rule*. In other words, it will choose the method with data types as narrow as possible -- while still can be implicitly cast.

Henry

* Actually, the rule is a bit more complex than that, but this is the only one that applies here.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had no idea you could fit a long into a float, I mean even with implicit narrowing, I thought there had to be a cast. Is implicit casting is in the exam?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here and here you'll find great explanations (with code snippets) about how/why a long fits into a float. Implicit and explicit casting are topics on the exam. So you need to know when e.g. an assignment will compile successfully and when an explicit cast is required.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, implicit casting and implicit narrowing are two separate things aren't they?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic