methodオーバーロード第2段

</p>
<p>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"));
}
}</p>
<p>

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

60
3.5
Rolla

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

Java

Posted by bistro