class Test {
void m1(byte b) {
System.out.print("byte");
}
void m1(short s) {
System.out.print("short");
}
void m1(int i) {
System.out.print("int");
}
void m1(long l) {
System.out.print("long");
}
public static void main(String [] args) {
Test test = new Test();
test.m1(2);
}
}
The output is : int. why does jvm consider the method with int parameter?
Because integer literals are of type
intin Java. You’ll need an explicit cast if you want to call the other ones. (Or add aLsuffix if you want to call thelongversion.)See the JLS Lexical Structure §3.10.1 Integer Literals for the details.