0
0

Javaで「メソッドのオーバーロード」の動作を確認してみた

Posted at

概要

Javaで「メソッドのオーバーロード」の動作を確認してみました。
以下のページを参考にしました。

実装

以下のファイルを作成しました。

JSample8_1.java
class JSample8_1{
  public static void main(String args[]){
    System.out.println(plus(10, 7));
    System.out.println(plus(3.2, 4));
    System.out.println(plus(7, 1.223));
    System.out.println(plus(5.08, 2.4));
  }

  private static int plus(int n1, int n2){
    System.out.println("int + int");
    return n1 + n2;
  }

  private static double plus(int n1, double d1){
    System.out.println("int + double");
    return n1 + d1;
  }

  private static double plus(double d1, int n1){
    System.out.println("double + int");
    return n1 + d1;
  }

  private static double plus(double d1, double d2){
    System.out.println("double + double");
    return d1 + d2;
  }
}

以下のコマンドを実行しました。

$ javac JSample8_1.java 
$ java JSample8_1 
int + int
17
double + int
7.2
int + double
8.223
double + double
7.48

まとめ

何かの役に立てばと。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0