assert boolean式; または assert boolean式: メッセージ;
-source 1.3 assert文を無効にする(デフォルト) -source 1.4 assert文を有効にするTop
有効(*1) | 無効(*3) | |
---|---|---|
すべてのクラス | -ea(*2) | -da(*4) |
指定クラス | -ea:MyClass | -da:MyClass |
カレント・ディレクトリー内の無名パッケージ | -ea:... | -da:... |
指定パッケージとすべてのサブパッケージ | -ea:myPackage... | -da:myPackage... |
java -ea:p1... -da:B -da:E A
static fainal boolean ASSERTS = false; if (ASSERTS) assert文; // assert文は無効になります。Top
void clearAssertionStatus() | このクラスローダのデフォルト宣言ステータスを false に設定し、クラスローダに関連付けられたデフォルトパッケージおよびクラス宣言ステータス設定をすべて破棄します。 |
void setClassAssertionStatus (String className, boolean enabled) |
このクラスローダおよび内部の入れ子構造クラス内の名前付きのトップレベルクラスに対して、目的の宣言ステータスを設定します。 |
void setDefaultAssertionStatus(boolean enabled) | このクラスローダのデフォルト宣言ステータスを設定します。 |
void setPackageAssertionStatus (String packageName, boolean enabled) |
名前付きパッケージのデフォルト宣言ステータスを設定します。 |
- static int aRemainder(int x, int y) {
- assert y != 0: "Zero divide."; // 事前条件(ゼロ除算)
- int r = 0;
- if (x >= 0) {
- r = x % y;
- } else if (x < 0 ) {
- r = x % y + Math.abs(y);
- } else {
- assert false: "Execution should never reach this point."; // 不変条件(到達不能)
- }
- assert r >= 0 && r < y: "Remainder is out of range."; // 事後条件(余りが範囲外)
- return r;
- }
throw new AssertionError("Execution should never reach this point.");Top
Top
- static int aRemainder(int x, int y) {
- assert y != 0: "Zero divide."; // 事前条件(ゼロ除算)
- int r = 0;
- if (x >= 0) {
- r = x % y;
- } else if (x < 0 ) {
- r = x % y + Math.abs(y);
- } else {
- assert false: "Execution should never reach this point."; // 不変条件(到達不能)
- }
- assert r >= 0 && r < y: "Remainder is out of range."; // 事後条件(余りが範囲外)
- return r;
- }
Top
- static int aRemainder(int x, int y) {
- assert y != 0: "Zero divide."; // 事前条件(ゼロ除算)
- int r = 0;
- if (x >= 0) {
- r = x % y;
- } else if (x < 0 ) {
- r = x % y + Math.abs(y);
- } else {
- assert false: "Execution should never reach this point."; // 不変条件(到達不能)
- }
- assert r >= 0 && r < y: "Remainder is out of range."; // 事後条件(余りが範囲外)
- return r;
- }
- public class Assertion51 {
- static int aRemainder(int x, int y) {
- assert y != 0: "Zero divide."; // 事前条件(ゼロ除算)
- int r = 0;
- if (x >= 0) {
- r = x % y;
- } else if (x < 0 ) {
- r = x % y + Math.abs(y);
- } else {
- assert false: "Execution should never reach this point."; // 不変条件(到達不能)
- }
- assert r >= 0 && r < y: "Remainder is out of range."; // 事後条件(余りが範囲外)
- return r;
- }
- public static void main(String[] args) {
- int x = Integer.parseInt(args[0]);
- int y = Integer.parseInt(args[1]);
- System.out.print("aRemainder(" + x + ", " + y + ") = " + aRemainder(x, y));
- System.out.println(", " + x + " % " + y + " = " + (x % y));
- System.out.print("aRemainder(" + x + ", " + -y + ") = " + aRemainder(x, -y));
- System.out.println(", " + x + " % " + -y + " = " + (x % -y));
- System.out.print("aRemainder(" + -x + ", " + y + ") = " + aRemainder(-x, y));
- System.out.println(", " + -x + " % " + y + " = " + (-x % y));
- System.out.print("aRemainder(" + -x + ", " + -y + ") = " + aRemainder(-x, -y));
- System.out.println(", " + -x + " % " + -y + " = " + (-x % -y));
- }
- }
$ javac -source 1.4 Assertion51.java $ java -ea Assertion51 10 3 aRemainder(10, 3) = 1, 10 % 3 = 1 aRemainder(10, -3) = 1, 10 % -3 = 1 aRemainder(-10, 3) = 2, -10 % 3 = -1 aRemainder(-10, -3) = 2, -10 % -3 = -1 $ java -ea Assertion51 10 0 Exception in thread "main" java.lang.AssertionError: Zero divide. at Assertion51.aRemainder(Assertion51.java:3) at Assertion51.main(Assertion51.java:20)