Chapter 4 - Methods and Encapsulation Flashcards
1
Q
What is the order of precedence for overloading methods?
A
- The exact primitive type of the argument
- If the argument is a byte, short, char, int, or long:
- The next larger size whole number primitive
available (byte, short, int, and long)
- The smallest size decimal primitive available.
(float, double) - If the argument is a float:
- A double primitive - A wrapper class type (has to be an exact match)
- Varargs (exact primitive type)
If the argument is a byte, short, char, int, or long: - The next larger size whole number varargs available (byte, short, int, and long)
- The smallest size decimal varargs available
(float, double) - If the argument is a float:
- A double varargs
-Varargs (exact Wrapper class)
- A double varargs
Example:
public class MethodPicker { // Method A public void pickMe(int x) { System.out.println ("int"); }
// Method B public void pickMe(long x) { System.out.println ("long"); }
// Method C public void pickMe(Integer x) { System.out.println ("Integer"); }
// Method D public void pickMe(int... x) { System.out.println ("int..."); }
// Method E public void pickMe(long... x) { System.out.println ("long..."); }
public static void main(String[] args) { MethodPicker mp = new MethodPicker(); int x = 5; mp.pickMe(x); } }