Gyh's Braindump

Java Overloading

tag
Java, overloading
source
DT-On Java 8
source
stackify-polymorphism-in-java

Methods

positioneaseboxintervaldue
front2.5036.002021-01-26T23:34:06Z

Multiple methods with the same name in the same class Meet one of the requirements below:

  • Different number of parameters
  • Different types of parameters
  • Different order of parameters

Why not distinguish between methods based on return values:

  • compile can not determine which method to call when calling a method for its side effect
    • call a method and ignore the return value: int f(int x) called with f(x)

Primitive Arguments

Primitive Types

Widening Conversion

boolean not support casting

  • constant value is treated as int
  • for all primitive types supporting narrowing, they would be casted to the function with the next larger type
    • void f(short x), void f(int x), void f(long x)
    • a variable byte b = 0, call function f(b)
    • void f(short x) would actually be called
  • char is an exception: if it doesn’t find an exact char match, it is promoted to int

Narrowing Conversion

If an argument is wider than what the method expects, explicit narrowing conversion must be performed.

Links to this note