This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Method-Call Conversion with Overloaded Methods

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the JQuest Mock Exam, the following question puzzles me:
Consider the following class
1. class Tester {
2. void test (int i) { System.out.println ("int version"); }
3. void test (String s) { System.out.println ("String version"); }
4.
5. public static void main (String args[]) {
6. Tester c = new Tester ();
7. char ch = 'p';
8. c.test (ch);
9. }
10. }
Which of the following statements below is true?(Choose one.)
a. Line 3 will not compile, because void methods cannot be overridden.
b. Line 8 will not compile, because there is no conversion of test() that takes a char argument.
c. The code will compile and produce the following output "int version"
d. The code will compile and produce the following output "String version"
The answer is 'The code will compile and produce the following output "int version"'
I checked it in a test class, and it's correct, but why is this true? How does Java decided to go to the int version and not the String version?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JLS:


5.1.2 Widening Primitive Conversion
The following 19 specific conversions on primitive types are called the widening primitive conversions:
byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double

 
Lance Finney
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Is it also true that there would never be a method-call conversion from a primitive to an object?
Also, what if the two options were long and float?
Thanks
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, primitives can not be widened into objects.
A char will be widened automatically to a long, float or double if the code required it.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh, I get the question now.
If given a choice of two methods that both can be automatically widened to the system will choose them in the order presented above, so long would be chosen over float.
 
Lance Finney
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. That answers my questions!
 
It means our mission is in jeapordy! Quick, read this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic