confusion code

G

gk

public class PowerSupply
{
public PowerSupply(String voltage){
System.out.println ("PowerSupply(String) executed");
}
public PowerSupply(Object voltage){
System.out.println ("PowerSupply(Object) executed");
}
public PowerSupply(){
System.out.println ("No argument constructor execute");
}
public static void main(String[] args) {
PowerSupply ps = new PowerSupply(null);
}
}


what would be the output ?

output: PowerSupply(String) executed\

i dont know why the JVM is choosing the first constructor for
argument null .

please explain...how this output coming ?
 
O

Oliver Wong

gk said:
public class PowerSupply
{
public PowerSupply(String voltage){
System.out.println ("PowerSupply(String) executed");
}
public PowerSupply(Object voltage){
System.out.println ("PowerSupply(Object) executed");
}
public PowerSupply(){
System.out.println ("No argument constructor execute");
}
public static void main(String[] args) {
PowerSupply ps = new PowerSupply(null);
}
}


what would be the output ?

output: PowerSupply(String) executed\

i dont know why the JVM is choosing the first constructor for
argument null .

please explain...how this output coming ?

JLS states that the when selecting between overloaded methods, the "most
specific" method will always be chosen. They then go into great lengths
defining "most specific". Informally, any parameter that could be passed
into the first constructor (String voltage) could also be passed into the
second constructor (Object voltage), but not the other way around, so the
first constructor is "more specific" than the second one.

- Oliver
 
G

gk

thanks ...this helps

Oliver said:
gk said:
public class PowerSupply
{
public PowerSupply(String voltage){
System.out.println ("PowerSupply(String) executed");
}
public PowerSupply(Object voltage){
System.out.println ("PowerSupply(Object) executed");
}
public PowerSupply(){
System.out.println ("No argument constructor execute");
}
public static void main(String[] args) {
PowerSupply ps = new PowerSupply(null);
}
}


what would be the output ?

output: PowerSupply(String) executed\

i dont know why the JVM is choosing the first constructor for
argument null .

please explain...how this output coming ?

JLS states that the when selecting between overloaded methods, the "most
specific" method will always be chosen. They then go into great lengths
defining "most specific". Informally, any parameter that could be passed
into the first constructor (String voltage) could also be passed into the
second constructor (Object voltage), but not the other way around, so the
first constructor is "more specific" than the second one.

- Oliver
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top