public static String getNumber(String label, Number number) { return "2: " + label + "=" + number.intValue(); }
public static void main(String[] args) { try{ System.out.println(getNumber("Sum", null)); }catch(NullPointerException ss){ System.out.println("I get printed out from catch block"); } } }
Prints I get printed out from catch block.
When I change the method call as System.out.println(getNumber("Sum", 10));, the method with Integer argument is called as it is the most relevant one. and prints out 1: Sum=10
NullPointerException will be thrown. From Valentin's question, one should also ask themself if one pass 123, 123.2, long, or etc. instead of "null" which method is going to invoke and why.
It will print out Integer: 1, provided that your class Main is public and also you change the Number n = 1 to Integer = 1. It complains at the line Number = 1 as required an int.
Regards, Jothi Shankar Kumar. S [ October 30, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
sai vargheese
Greenhorn
Joined: Sep 24, 2006
Posts: 2
posted
0
Hi All, A simple question. Which method would get invoked??? one with Integer or Number, Which method would be invoked. I tried it this code, the method with Integer got invoked, But i would like to know how this happens??
Somebody help me out.
Eddy Lee Sin Ti
Ranch Hand
Joined: Oct 06, 2005
Posts: 135
posted
0
I think the code will works and print out "Number:1"
the line "Number n = 1;" is a tricky one and my understanding is that the value 1 will be boxed up into an instance of Integer and cast to the abstract class Number.
SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="http://eddyleesinti.blogspot.com" target="_blank" rel="nofollow">http://eddyleesinti.blogspot.com</a>
I think the code will works and print out "Number:1"
the line "Number n = 1;" is a tricky one and my understanding is that the value 1 will be boxed up into an instance of Integer and cast to the abstract class Number.
For me the declaration Number n = 1 says, found : int required: javaapplication19.Number Number n = 1; 1 error BUILD FAILED (total time: 0 seconds)
Why is that and how come that for you it prints Number : 1? Did you try it out? Please let me know. It's a good question, a real brainteaser.
A simple question. Which method would get invoked??? one with Integer or Number, Which method would be invoked. I tried it this code, the method with Integer got invoked, But i would like to know how this happens??
The method with the most significant argument will be invokde. Here Interger which is a subclass of Number is the most significant. So it gets invoked.
Regards, Jothi Shankar Kumar. S
Burkhard Hassel
Ranch Hand
Joined: Aug 25, 2006
Posts: 1274
posted
0
Hi ranchers,
Jothi Shankar Kumar Sankararaj posted October 30, 2006 04:34 AM I
t will print out Integer: 1, provided that your class Main is public and also you change the Number n = 1 to Integer = 1. It complains at the line Number = 1 as required an int.
That't true - for Java 1.4 But it wouldn't compile in Java 1.4 anyway because of the vararg in the main method. I thought that it should be clear then, that it's Java5.
Classes don't have to be public to run their main methods!
Only if you like to start their main method from a different package.
Special service: the same for java 1.3 - runs 1.4 as well )-;
(and further down the thread)
The method with the most significant argument will be invokde. Here Interger which is a subclass of Number is the most significant. So it gets invoked.
Ha! Got you!
No. reference type of n is Number, so the number version of the overloaded method is called. Object type doesn't matter here. Object type only matters in overridden methods.
Now: 1) how about modifying the call to getNumber as follows: getNumber("Sum", new Integer(1)); 2) how about modifying the call to getNumber as follows: getNumber("Sum", new Number(1));
What gets printed out in each case ? Try compiling and executing mentally before throwing it in the compiler!
Rancy Chadha
Ranch Hand
Joined: Jul 12, 2006
Posts: 135
posted
0
Hi Valentine,
If we change add following lines
1) getNumber("Sum", new Integer(1)); It will compile and will give output 1: sum=1
2) getNumber("Sum", new Number(1)); Addition of this line will result in COMPILATION ERROR The reason it happens is pretty obvious Number class is abstract class and abstract class cannot be instantiated.
Thanks, Rancy
Thanks,<br />-Rancy
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
Good job guys !!
The key here was to know how methods get resolved when executing an overloaded method.