Any Reason for this result ...................

A

abdul82

Please go through this simple program it should show " Number ... n"
but it is showing
"float a"at run time. I am using jdk 5.0

public class VarArg1 {

public void take()
{
System.out.println("no arg");

}
public void take(Number ... n)
{
System.out.println("Number ... n");
}
public void take(Double ... n)
{
System.out.println("Double ...n");
}

public void take(float a)
{
System.out.println("float a");
}
public void take(double a)
{
System.out.println("double a");
}

public static void main(String[] args) {

new VarArg1().take(2); // it Should show Number ... n but is is
showing float a
}
}

Abdul Mohsin
(e-mail address removed)
 
M

Mark Thomas

Please go through this simple program it should show " Number ... n"
but it is showing
"float a"at run time. I am using jdk 5.0

public class VarArg1 {

public void take()
{
System.out.println("no arg");

}
public void take(Number ... n)
{
System.out.println("Number ... n");
}
public void take(Double ... n)
{
System.out.println("Double ...n");
}

public void take(float a)
{
System.out.println("float a");
}
public void take(double a)
{
System.out.println("double a");
}

public static void main(String[] args) {

new VarArg1().take(2); // it Should show Number ... n but is is
showing float a
}
}

Abdul Mohsin
(e-mail address removed)
The literal '2' is an int - there is no take(int), so it is upcast to a
float.

Double is a wrapper for double and although Java 5 allows for automatic
'boxing', but it won't happen here. Number is the abstract superclass
of Double.

Mark
 

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

Forum statistics

Threads
474,266
Messages
2,571,079
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top