作成日:2003.5.10
List words = new ArrayList();しかし、 ユーザーは String クラスを収めるコンテナ、 自分の作成したクラス収めるコンテナのように、 特定の型に特化したコンテナを必要とすることの方が多い。 従来のコンテナクラスでは、 間違った型の要素を誤ってコンテナに挿入することを防ぐことができなかった。
words.add( word );
String title = ((String) words.get(i)).toUppercase();
List<String> words = new ArrayList<String>();この機能を使うと、 特定クラス(この例では Stringクラス) に特化したコンテナが作成できる。
words.add( word );
String title = words.get(i).toUppercase();
for (Iterator<Strin>; i = c.iterator(); i.hasNext(); ) {このコレクションから要素を取り出すという動作をより明確にするために、 Perl や C# 言語は foreach 構文を持っている。 J2SE 1.5 から、 for に foreach のような機能を実現する 構文が追加された。
String s = i.next();
// Body
}
for (String s : c ) {
// Body
}
int sum = 0;上の表現を下のように書き改めることができる。
for( int i=0 ; i<a.length ; i++ ){
int e = a[i];
sum += e;
}
int sum = 0;
for( int e : a ){
sum += e;
}
enum Day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
for (Day d : Day.VALUES){
// Body ...
}
int i1 = ... ;これと等価な処理が
Integer I1 = new Integer( i1 );
int i2 = I1.intValue();
Integer I1 = (Integer) i1;と書けるようになった。
int i2 = (int) I1;
public enum Coin {
penny(1), nickel(5), dime(10), quarter(25);
Coin(int value) { this.value = value; }
private final int value;
public int value() { return value; }
}
enum の要素は hashCode を持ち比較可能で、
toString も使える((String) Coin.penny == "penny" とか)。public static List<this enum class> VALUES; public final List<this enum class> family(); public static <this enum class> valueOf(String name);この機能は、 拡張された for 構文で利用可能する際に便利で、 以下のように書ける。
enum Day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
for (Day d : Day.VALUES){
// Body ...
}
valueOf() を用いると
Day.Sunday == Day.valueOf( "Sunday" )
のような比較も可能も可能になる。
public abstract enum Operation {
plus {
double eval(double x, double y) { return x + y; }
},
minus {
double eval(double x, double y) { return x - y; }
};
// Perform arithmetic operation represented by this constant
abstract double eval(double x, double y);
public static void main(String args[]) {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
for (Iterator i = VALUES.iterator(); i.hasNext(); ) {
Operation op = i.next();
System.out.println(x + " " + op + " " + y + " = " + op.eval(x, y));
}
}
}
java Operation 2.0 4.0
2.0 plus 4.0 = 6.0
2.0 minus 4.0 = -2.0
public interface Physics {
public static final double AVOGADROS_NUMBER = 6.02214199e23;
public static final double BOLTZMANN_CONSTANT = 1.3806503e-23;
public static final double ELECTRON_MASS = 9.10938188e-31;
}
public class Guacamole implements Physics {
public static void main(String[] args) {
double moles = ...;
double molecules = AVOGADROS_NUMBER * moles;
...
}
}
この implements は概念的には、インターフェイスを継承したのではなく、
Guacamole クラスの名前空間の中に定数集合を取り込むためのものである。
interface の本来の概念からは外れていて好ましくない。
import static org.iso.Physics.*;
class Guacamole {
public static void main(String[] args) {
double molecules = AVOGADROS_NUMBER * moles;
...
}
}
また、Math クラスを static import すると、
Math.abs(x)、Math.sqrt(x) と書いていたコードが abs(x)、sqrt(x) と
書けるようになる。
import javax.xml.rpc.*;
public class CoffeeOrder {
@Remote public Coffee [] getPriceList() {
...
}
@Remote public String orderCoffee(String name, int quantity) {
...
}
}
これをツールが解釈して、
以下のようなコードを生成できるように
することを狙ったている(らしい)。
public interface CoffeeOrderIF extends java.rmi.Remote {
public Coffee [] getPriceList()
throws java.rmi.RemoteException;
public String orderCoffee(String name, int quantity)
throws java.rmi.RemoteException;
}
public class CoffeeOrderImpl implements CoffeeOrderIF {
public Coffee [] getPriceList() {
...
}
public String orderCoffee(String name, int quantity) {
...
}
}