简体   繁体   English

Java的。重载方法

[英]Java. Overloading method

for example, i have this class: 例如,我有这个类:

public class Col {

static void test(int a)
    {
        System.out.println("int");
    }
    public static void main(String args[])
    {
        Col.test(12);  //1

        Col.test((byte)12); //2

        Col.test((long)100); //3

    }
}

and now me intresting how algoritm work this code. 现在我有兴趣了解algoritm如何使用这段代码。 I think, that this steps: 我想,这个步骤:

1 line - all correct call method with int param, perfect. 1行 - 所有正确的调用方法与int param,完美。

2 line - call method with byte param...oooops. 2行 - 带字节参数的调用方法... oooops。 what do? 做什么? Java try widening byte to int? Java尝试将byte扩展为int? Its true? 这是真的?

3 line call method with long param... again ooops. 3线呼叫方法与长参数...再次ooops。 what do? 做什么? convert long to int java can't, because loss of accuracy. 转换long到int java不行,因为精度损失。 its try? 它的尝试? And in result - Exception. 结果 - 例外。

Than I add this: 比我添加这个:

 public static void test(Object a)
    {
        System.out.println("Object");
    }

and if a call: 如果是电话:

Col.test((long)100);

all correct, no Exception so, what the relation between primitive type long and Object? 所有正确,没有异常所以,原始类型long和Object之间的关系是什么?

Yes, there's an implicit conversion from byte to int , but no implicit conversion from long to int (because of the likelihood of losing information). 是的,有一个从byteint的隐式转换,但没有从longint隐式转换(因为丢失信息的可能性)。

In the third case, you're using autoboxing which will convert a long (primitive type) to a Long (class type). 在第三种情况下,你使用的自动装箱 ,这将转换为long (基本型)到Long (类类型)。

You can see that by changing the body of test to: 您可以通过将test主体更改为:

public static void test(Object a)
{
    System.out.println(a.getClass());
}

It will then print out class java.lang.Long . 然后它将打印出class java.lang.Long

Your first example shows conversion of primitive types. 您的第一个示例显示了基元类型的转换。 The second shows boxing and unboxing, which is - in brief - a convenient conversion between primitive type (like long ) and their wrapper classes ( java.lang.Long in this case). 第二个显示装箱和拆箱,简而言之 - 原始类型(如long )和它们的包装类(在本例中为java.lang.Long )之间的方便转换。

Overloading is implementing methods that have the same name but different parameters . 重载是实现具有相同名称但不同参数的方法 Here we have two methods 这里我们有两种方法

static void test(int a){}
static void test(Object a){}

and call it with test((long) 100) . 并用test((long) 100)调用它。 The first method can't be called, because the JVM won't narrow a long to an int without explicit casting. 无法调用第一个方法,因为如果没有显式转换,JVM不会将long缩小到int But the JVM (Version 1.5+) can convert the long value to a Long (autoboxing) and test(Long.valueOf((long) 100)) is a good match for the second method. 但是JVM(版本1.5+)可以将long值转换为Long (自动装箱)并且test(Long.valueOf((long) 100))是第二种方法的良好匹配。

This is because auto-boxing feature. 这是因为自动装箱功能。 Actually you have promoted the primitive to long and while calling test method automatically it is seaching for its equivalent type hence it is calling test(Object a).You can see like this Col.test(new Integer(12));this will also call test(Object a).Also you can refer this link Determining if an Object is of primitive type 实际上你已经将原语提升为long并且在自动调用test方法时它正在为其等效类型进行搜索,因此它调用test(Object a)。你可以看到这样的Col.test(new Integer(12));这也是调用测试(对象a)。也可以参考此链接确定对象是否是基本类型

public static void test(Object obj) {
      if (obj instanceof Integer) {
          System.out.println("Integer");
      } else if (obj instanceof Double) {
          System.out.println("Double");
      } else if (obj instanceof Float) {
          System.out.println("Float");
      } else if (obj instanceof Long) {
          System.out.println("Long");
      } 
  }

All java primitives have corresponding boxed "types" that are actual classes. 所有java原语都有相应的盒装“类型”,它们是实际的类。 In you example, long has a corresponding class Long. 在你的例子中,long有一个对应的类Long。 This class extends from Object. 此类从Object扩展。

What you have experienced is boxing and unboxing. 你经历的是拳击和拆箱。

It is a feature introduced in Java 5. Its called Autoboxing. 它是Java 5中引入的一个功能。它称为Autoboxing。 In this a primitive type is converted to Object (in your case long to Long). 在这种情况下,基本类型将转换为Object(在您的情况下为long to Long)。 See this link for details on Autoboxing. 有关Autoboxing的详细信息,请参阅此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM