Kodrill

演算子と制御構造

Java SE 25 Silver (1Z0-827-JPN) 試験

if・switch 文/繰り返し文と break・continue/switch 式と型パターン・レコード・パターン/算術式・boolean 式と演算子の優先順位/Math API

問題数
32 問
合格ライン
65%
実行して確認済み
30 問
この分野の要点を読む6 章・約 15 分。実行して確かめたコード例と、試験でよくある引っかけをまとめています

ログインなしで試す

無料の 5 問を、1問ごとに正誤と解説を見ながら解けます。解答は保存されません。

5 問を試す

無料登録して学習を記録する

学習履歴・ダッシュボード・ブックマーク・模試モードが使えます。

★ 残り 27 問はプレミアム会員限定です

無料プランでは先頭 5 問まで解けます。プレミアムなら全問題・苦手問題の復習・広告非表示が使えます。
料金プランを見る

問題と解説を見る (無料)

登録しなくても、次の5問は問題文と解説をそのまま読めます。

演算子と制御構造の要点

内容の確認日: 2026-11-26

この分野では、式の値を正しく計算できるかと、if・switch・繰り返し文の流れを追えるかが問われます。出力を答える問題がほとんどなので、紙の上でコードを 1 行ずつ追う練習が効果的です。

SE 25 では「算術式および boolean 式の評価、演算子の優先順位および Math API の使用」が出題項目に入り、switch の項目が「switch 式および型パターン、レコード・パターンを使用した switch の使用」に広がりました。case にパターンを書く switch は、SE 17 版にはなかった新しい出題範囲です。

演算子の優先順位と評価の順番

*・/・% は +・- より先に計算され、同じ優先順位の演算子は左から計算されます。整数同士の / は小数点以下を切り捨て (0 の方向)、% の結果の符号は割られる数と同じです。

オペランド (演算の対象) は左から順に評価されます。x += 式 は、先に左辺の x の値を読み出してから右辺を計算します。後置の x++ は増やす前の値を、前置の ++x は増やした後の値を返します。

Main.java
public class Main {    public static void main(String[] args) {        int x = 5;        x += x++ * 2 + --x;        System.out.println(x + " " + (7 + 3 % 4 * 2) + " " + -7 / 2 + " " + -7 % 2);    }}
ターミナル
java Main
20 13 -3 -1

&& と || は、左側だけで結果が決まると右側を評価しません (短絡評価)。& と | は両側を必ず評価します。+= などの複合代入は結果を左辺の型に自動でキャストしますが、b = b + 1 のような通常の代入では、byte や short の計算結果が int になるのでキャストが必要です。

Main.java
public class Main {    public static void main(String[] args) {        int a = 0, b = 0;        if (a++ > 0 && b++ > 0) { }        if (a++ > 0 | b++ > 0) { }        byte s = 120;        s += 10;        System.out.println(a + " " + b + " " + s);    }}
ターミナル
java Main
2 1 -126

Math API

Math のメソッドはすべて static で、Math.abs(-3) のようにクラス名で呼びます。戻り値の型に注意しましょう。floor (それ以下で最大の整数) と ceil (それ以上で最小の整数) と pow・sqrt は double を返し、round は引数が double なら long を返します。round は 0.5 を足して floor した値なので、-2.5 は -2 になります。

floorDiv と floorMod は、負の無限大の方向に切り捨てた商と、それに対応する余りを返します。/ と % とは負の数で結果が変わります。

Main.java
public class Main {    public static void main(String[] args) {        System.out.println(Math.round(-2.5) + " " + Math.floor(-1.5) + " " + Math.ceil(1.1));        System.out.println(Math.floorDiv(-7, 2) + " " + Math.floorMod(-7, 2) + " " + Math.pow(3, 2) + " " + Math.max(2, 1.5));    }}
ターミナル
java Main
-2 -2.0 2.0
-4 1 9.0 2.0

Math.random() は 0.0 以上 1.0 未満の double を返します。max・min・abs は引数の型ごとにオーバーロードされていて、int と double を混ぜると double 版が呼ばれます。

分岐と繰り返し

do-while 文は、条件を判定する前に本体を 1 回実行します。for 文の初期化部と更新部にはカンマで複数の式を書けます。continue は残りの処理を飛ばして次の繰り返し (for 文なら更新部) に進み、break は繰り返しを抜けます。ラベルを付けると、外側の繰り返しを抜けたり、外側の次の繰り返しに進んだりできます。

Main.java
public class Main {    public static void main(String[] args) {        StringBuilder sb = new StringBuilder();        outer:        for (int i = 1; i <= 3; i++) {            for (int j = 1; j <= 3; j++) {                if (j == 2) continue outer;                if (i == 3) break outer;                sb.append(i).append(j).append(' ');            }        }        System.out.println(sb.toString().trim());    }}
ターミナル
java Main
11 21

switch 式

case 値 -> の形の case は、次の case に処理が移りません (フォールスルーしない)。case 1, 2 -> のようにカンマで値をまとめられます。switch 式は値を返すので、すべての値を網羅する必要があり、int や String では default が必要です。ブロック { } から値を返すときは return ではなく yield を使います。

Main.java
public class Main {    public static void main(String[] args) {        int day = 6;        String type = switch (day) {            case 1, 2, 3, 4, 5 -> "weekday";            case 6, 7 -> {                String w = "week";                yield w + "end";            }            default -> "invalid";        };        System.out.println(type);    }}
ターミナル
java Main
weekend

switch の条件式に long・float・double・boolean は使えません (SE 25 ではプリミティブ型のパターンがプレビュー機能のため、通常のコンパイルではエラーになります)。

型パターンとレコード・パターンの switch

case には型パターン (case Integer i) を書けます。値がその型なら変数に入れて使えます。when の後に条件 (ガード) を書くと、条件を満たすときだけ一致します。レコード・パターン (case Point(int x, int y)) は、レコードのコンポーネントを取り出して変数に入れます。使わないコンポーネントは _ で読み飛ばせます。

Main.java
public class Main {    record Point(int x, int y) { }​    static String describe(Object o) {        return switch (o) {            case Integer i when i > 10 -> "big";            case Integer i -> "int" + i;            case Point(int x, int y) when x == y -> "diag";            case Point(var x, _) -> "x" + x;            case null -> "null";            default -> "other";        };    }​    public static void main(String[] args) {        System.out.println(describe(42) + " " + describe(3) + " " + describe(new Point(1, 1)) + " " + describe(new Point(2, 5)) + " " + describe(null) + " " + describe("s"));    }}
ターミナル
java Main
big int3 diag x2 null other

case は上から順に調べられます。前の case がすべて受け取ってしまい、決して一致しない case を書くとコンパイルエラーです (前の case に支配される)。ガードのない case Integer i の後に case Integer i when ... を書いた場合や、case CharSequence cs の後に case String s を書いた場合が当たります。

Main.java
public class Main {    static String test(Object o) {        return switch (o) {            case Integer i -> "int";            case Integer i when i > 0 -> "positive";            default -> "other";        };    }​    public static void main(String[] args) {        System.out.println(test(1));    }}
ターミナル
javac Main.java

コンパイルエラー (5 行目): 前の case Integer i がすべての Integer に一致するので、この case は決して選ばれない

パターンを使う switch は、switch 文であってもすべての値を網羅する必要があります。シール・インタフェース (sealed) なら、permits で許可したすべての型の case があれば default は要りません。また、パターンの switch に限らず、switch に null を渡すと、case null がない限り NullPointerException が発生します (default があっても同じです)。

Main.java
public class Main {    sealed interface Shape permits Circle, Square { }    record Circle(int r) implements Shape { }    record Square(int side) implements Shape { }​    static int area(Shape s) {        return switch (s) {            case Circle c -> 3 * c.r() * c.r();            case Square(int side) -> side * side;        };    }​    public static void main(String[] args) {        System.out.println(area(new Circle(1)) + " " + area(new Square(4)));    }}
ターミナル
java Main
3 16

試験でよくある引っかけ

  • 複合代入の評価の順番

    x += x++ + ++x は、左辺の x の値を先に読み出してから右辺を計算します。

  • Math の戻り値の型

    floor・ceil・pow・sqrt は double を返すので .0 が付きます。round(double) は long です。

  • 負の数の割り算

    -7 / 2 は -3、-7 % 2 は -1 です。Math.floorDiv と Math.floorMod では -4 と 1 になります。

  • case の支配

    より広い型やガードのない case を先に書くと、後ろの case がコンパイルエラーになります。

  • パターンの switch 文の網羅性

    case にパターンを使うと、switch 文でも網羅しないとコンパイルエラーです。シール型ならすべての許可された型を書けば default は不要です。

  • switch と null

    case null がない switch に null を渡すと、default があっても NullPointerException です。

問題で確認する

この分野の問題を、解説つきで読めます (登録不要)。

出典・参考

コード例は、試験の対象バージョンで実際にコンパイル・実行して結果を確認しています。