Java: passing an argument with a different type to a function

前端 未结 4 794
长情又很酷
长情又很酷 2020-12-06 11:50

In Java, suppose we have a function with the parameter double a. Does it work if I pass an integer as argument? (I mean, is there an implicit conversion?) And i

4条回答
  •  心在旅途
    2020-12-06 12:25

    Because you can set a double to an integer, then integer as argument is ok to function with double as parameter. Other way round fails. In that case you need to cast the double to an int. Same applies to normal assignents eg..

      int i = 6;
      double d = 0;
      d = i;  /* ok
      i = d ; /* not ok
    

提交回复
热议问题