为什么使用int参数的方法考虑数值?

nov*_*ice 3 java

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

输出为:int.为什么jvm会考虑使用int参数的方法?

Mat*_*Mat 9

因为整数文字int在Java 中是类型的.如果你想打电话给其他人,你需要一个明确的演员表.(L如果要调用该long版本,请添加后缀.)

有关详细信息,请参阅JLS词汇结构 §3.10.1 整数文字.