This week's book giveaway is in the Features new in Java 9 forum.
We're giving away four copies of Java 9 Revealed and have Kishori Sharan on-line!
See this thread for details.
Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Method overload  RSS feed

 
Jason Attin
Ranch Hand
Posts: 234
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
Sheriff
Posts: 23026
120
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: 234
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: 11077
173
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: 234
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?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!