aspose file tools*
The moose likes Java in General and the fly likes about specific method and compile time type error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "about specific method and compile time type error" Watch "about specific method and compile time type error" New topic
Author

about specific method and compile time type error

Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi Friends,
Consider the following code below and execute it.It prints 3.I cant understand how and why plz help me
thank u
************************************
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");
}
void test(float a, long b, int c) {
System.out.println("6");
}
}
***************************************************
It prints Output as 3 plz help me friends in understanding
with regards
prasath


Thanks,
Prasath

SCJP1.4, SCWCD
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Java won't automatically convert a primitive type to another "smaller" type. So a double literal like 1.0 won't be automatically converted to a float, and a long literal won't be converted to an int, and an int literal won't be converted to a short. Java will, on the other hand, automatically convert primitives to "wider" types -- so all numeric types can be converted to double, for example. Of all the overloads you've defined, the only one that matches the three arguments you've given is the one that takes three doubles -- all the others would require an illegal conversion to a "smaller" type.


[Jess in Action][AskingGoodQuestions]
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
Think about what the JVM has to do at run-time: find the closest fit for the parameters you provided. What were the parameters you provided? (double, long, int).

So, the JVM looks at the first parameter, which is a double. That eliminates all but the 1st and 3rd methods. Next it looks at the 2nd parameter, a long. Both the first and third methods can handle a long as their second parameter (with an automatic cast - both double and long are 64-bits wide). Finally, the JVM looks at the third parameter, an int. This eliminates the 1st method because a short cannot hold an int value without an explicit cast. Even though the number "3" easily fits inside a short, the JVM doesn't care. As far as the JVM is concerned, the compile-time constant "3" is an int.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Fletcher Estes:
Think about what the JVM has to do at run-time:


Fletcher's logic is perfect, except that overloaded methods are resolved by the compiler at compile-time, not by the JVM at runtime.
Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi friends thank u for ur rplies
so the overloaded methods r resolved by compiler not by JVM is it
with regards
prasath
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about specific method and compile time type error
 
Similar Threads
Barry Boone #29
method params!
which method will be called
Why this Output?
Question from New Boone