Distinguish overloaded methods by return value

   As long as the compiler can unambiguously determine the semantics based on the context, overloaded methods can indeed be distinguished. However, sometimes you don't care about the return value of the method, and you want other effects of the method call (called for side effects), in which case you may call the method and ignore its return value.

At this time, how does Java determine which overloaded method to call? How should others understand this code? So distinguishing overloaded methods based on their return value won't work

-- Excerpted from Java programming ideas

 

   For example, there are 2 methods,

 void f(List list) { }

   int f(List list) { return 1; }

 

   When you call the int f(List list) method, you just need the method to process the list, but don't need the return value. Therefore, when calling, it will be directly written as f(List list); instead of int i = f(List list).

   At this time, the compiler finds that there are 2 f(List list), and cannot know which one you are calling. When others look at the code you write, it is not easy to know which method you are calling.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325291052&siteId=291194637