Overloading in Java and multiple dispatch

前端 未结 6 1360
走了就别回头了
走了就别回头了 2020-11-28 09:16

I have a collection (or list or array list) in which I want to put both String values and double values. I decided to make it a collection of objects and using overloading o

6条回答
  •  执笔经年
    2020-11-28 10:09

    Everything in Java is an Object/object (except primitive types). You store strings and integers as objects, and then as you call the prove method they are still referred to as objects. You should have a look at the instanceof keyword. Check this link

    void prove(Object o){
       if (o instanceof String)
        System.out.println("String");
       ....
    }
    

提交回复
热议问题