Why does the following code compiles ?

R

Razvan

Hi !




Please take a look at the following code:


public class CDummy
{
public static void main(String args[])
{
System.out.println("CDummy.");

printIt(null);
}

static void printIt(String str) {System.out.println("String: " + str);}

static void printIt(Object obj) {System.out.println("Object: " + obj);}
}

How can the compiler choose one printIt() function over the other ?




Regards,
Razvan
 
J

Joona I Palaste

Razvan said:
Please take a look at the following code:
public class CDummy
{
public static void main(String args[])
{
System.out.println("CDummy.");
printIt(null);
}

static void printIt(String str) {System.out.println("String: " + str);}
static void printIt(Object obj) {System.out.println("Object: " + obj);}
}
How can the compiler choose one printIt() function over the other ?

There is a rule in the JLS about this. In the case of two or more
possible methods matching, the one that is selected is the one whose
parameter types most closely match the argument types in the method
call. In the case of the argument being null without a typecast, the
method with the most specific parameter types is selected.
In your code, String is more specific than Object, because String is a
subclass of Object. So therefore the compiler chooses printIt(String).
If you had two methods whose parameter types were equally specific,
for example printIt(String) and printIt(List), you would get a compiler
error. Likewise if you had two methods with two parameters, and neither
had both more specific than the other. For example printIt(Object,
String) and printIt(String, Object).
 
S

Steve W. Jackson

:Hi !
:
:
:
:
: Please take a look at the following code:
:
:
:public class CDummy
:{
: public static void main(String args[])
: {
: System.out.println("CDummy.");
:
: printIt(null);
: }
:
: static void printIt(String str) {System.out.println("String: " + str);}
:
: static void printIt(Object obj) {System.out.println("Object: " + obj);}
:}
:
: How can the compiler choose one printIt() function over the other ?
:
:
:
:
:Regards,
:Razvan

Because you explicitly gave it a String literal. So it's a simple
matter of finding a printIt method that accepted a parameter of the type
provided.

= Steve =
 
J

Joona I Palaste

Steve W. Jackson said:
:Hi !
:
: Please take a look at the following code:
:
:
:public class CDummy
:{
: public static void main(String args[])
: {
: System.out.println("CDummy.");
:
: printIt(null);
: }
:
: static void printIt(String str) {System.out.println("String: " + str);}
:
: static void printIt(Object obj) {System.out.println("Object: " + obj);}
:}
:
: How can the compiler choose one printIt() function over the other ?
Because you explicitly gave it a String literal. So it's a simple
matter of finding a printIt method that accepted a parameter of the type
provided.

I'm sorry, but that's wrong. Plain unadorned null isn't "explicitly a
String literal" in any version of Java *I* know.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'It can be easily shown that' means 'I saw a proof of this once (which I didn't
understand) which I can no longer remember'."
- A maths teacher
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top