Java? Wrapper?


I’ve done some researching on the net on some mock question for the Java Certification Exam (1.5 / 1.6) and came across this one. Give it a try if you want, just reply in this post for your answer (and i hope there’s an explanation why you choose that answer).

Given:

class Eggs {
  int doX(Long x, Long y) { return 1; }
  int doX(long... x) { return 2; }
  int doX(Integer x, Integer y) { return 3; }
  int doX(Number n, Number m) { return 4; }
  public static void main(String[] args) {
    new Eggs().go();
  }
  void go () {
    short s = 7;
    System.out.print(doX(s,s) + " ");
    System.out.println(doX(7,7));
} }

What is the result?

  1. 1 1

  2. 2 1

  3. 3 1

  4. 4 1

  5. 2 3

  6. 3 3

  7. 4 3

make a choice!

One thought on “Java? Wrapper?

  1. G) 4 3. The Short value was wrapped to Integer.. Integer is a Number (Sub Class of number Class). Widening is applied for wrapper class if a IS-A relationship exist so the first 1 would be 4. the second one is implicitly an integer therefore the method that accepts an Integer is chosen (3).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s