Chapter 4 - Methods and Encapsulation Flashcards

1
Q

What is the order of precedence for overloading methods?

A
  1. The exact primitive type of the argument
  2. 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)
  3. If the argument is a float:
    - A double primitive
  4. A wrapper class type (has to be an exact match)
  5. Varargs (exact primitive type)
    If the argument is a byte, short, char, int, or long:
  6. The next larger size whole number varargs available (byte, short, int, and long)
    - The smallest size decimal varargs available
    (float, double)
  7. If the argument is a float:
    • A double varargs
      -Varargs (exact Wrapper class)

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); 
    }  
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly