File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from New Boone Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from New Boone" Watch "Question from New Boone" New topic
Author

Question from New Boone

Rekha Sivadasan
Greenhorn

Joined: Feb 15, 2001
Posts: 14
The output for the below given code is 3. It could have easily been 1 also. How is this the method choosen here?
class Test {
public static void main(String[] args) {
Test t = new Test();
t.test(1.0, 2L, 3);
}
void test(double a, double b, short c) {
System.out.println("1");
}
void test(float a, byte b, byte c) {
System.out.println("2");
}
void test(double a, double b, double c) {
System.out.println("3");
}
void test(int a, long b, int c) {
System.out.println("4");
}
void test(long a, long b, long c) {
System.out.println("5");
}
}
Thanks
Rekha
Hima Mangal
Ranch Hand

Joined: Feb 25, 2001
Posts: 82
hi rekha..
it couldn't have been 1.. the correct answer is 3) only.. let me explain..
the method signatures for 1) and 3) are :
void test(double a, double b, short c) {
System.out.println("1");
}
void test(double a, double b, double c) {
System.out.println("3");
}
and the method u call is :
t.test(1.0, 2L, 3);
where the first arg is a double, second one is a long and the third one is an integer. Now 1) can't satisfy the method invocation because its third arg is a short and not an int and short cannot be narrowed down to an int implicitly by the compiler EVEN THOUGH IT IS WITHIN PERMISSIBLE LIMITS OF A BYTE LITERAL.
hope this helps..

------------------
Hima


Hima<BR>Sun Certified Java Programmer
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Hi Rekha,
method call is: t.test(1.0, 2L, 3);
I think 3 is actually treated as an int. And signature
void test(double a, double b, short c)
(returns "3")
would be an narrowing conversion.
Corect me if I'm wrong,
Axel
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Sorry I muddled up the two methods. I agree with Hima.
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
u could also go through the following thread for a similar discussion:
http://www.javaranch.com/ubb/Forum24/HTML/008618.html
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question from New Boone