Warning: Trying to access array offset on value of type bool in /home/web13c/bistro.site/public_html/wp-content/themes/luxeritas/inc/json-ld.php on line 120

methodオーバーロード第2段

public class Rei17 {
    public static int mymax(int x, int y){
        return (x>y) ? x : y; //三項演算
    }
    public static double mymax(double x, double y){
        return (x>y) ? x : y;
    }
    public static String mymax(String x, String y){
        return (x.compareTo(y)>0) ? x : y;
    }
    public static void main(String[] args) {
        System.out.println(mymax(30,60));
        System.out.println(mymax(3.5,1.2));
        System.out.println(mymax("Rolla","Lisa"));
    }
}

オーバーロードはメソッドの機能追加なのね。上書きではないのね。
実行結果:

60
3.5
Rolla

compareTo()は、二つオブジェクトの値を比較するメソッド

Java

Posted by bistro