10 % 3 = 1 10 % -3 = 1 -10 % 3 = -1 -10 % -3 = -1
- public class ArithmeticOperators01 {
- public static void main(String[] args) {
- int i = 37;
- int j = 42;
- double x = 27.475;
- double y = 7.22;
- System.out.println("i + j = " + (i + j));
- System.out.println("x + y = " + (x + y));
- System.out.println();
- System.out.println("i - j = " + (i - j));
- System.out.println("x - y = " + (x - y));
- System.out.println();
- System.out.println("i * j = " + (i * j));
- System.out.println("x * y = " + (x * y));
- System.out.println();
- System.out.println("i / j = " + (i / j));
- System.out.println("x / y = " + (x / y));
- System.out.println();
- System.out.println("i % j = " + (i % j));
- System.out.println("x % y = " + (x % y));
- System.out.println();
- System.out.println("j + y = " + (j + y));
- System.out.println("i * x = " + (i * x));
- }
- }
□ 実行結果
$ java ArithmeticOperators01 i + j = 79 x + y = 34.695 i - j = -5 x - y = 20.255000000000003 i * j = 1554 x * y = 198.36950000000002 i / j = 0 x / y = 3.805401662049862 i % j = 37 x % y = 5.815000000000002 j + y = 49.22 i * x = 1016.575Top
第1オペランド | 第2オペランド | 結果 |
---|---|---|
正の無限大 | 0 | 正の無限大 |
正の有限で0でない数 | 0 | 正の無限大 |
0 | 0 | 非数 |
負の有限で0でない数 | 0 | 負の無限大 |
負の無限大 | 0 | 負の無限大 |
- public class ZeroDivide {
- public static void main(String[] args) {
- System.out.println("+INFINITY / 0 = " + (Double.POSITIVE_INFINITY / 0));
- System.out.println(" +100.0 / 0 = " + ( 100.0 / 0));
- System.out.println(" 0.0 / 0 = " + ( 0.0 / 0));
- System.out.println(" -100.0 / 0 = " + ( -100.0 / 0));
- System.out.println("-INFINITY / 0 = " + (Double.NEGATIVE_INFINITY / 0));
- }
- }
□ 実行結果
$ java ZeroDivide +INFINITY / 0 = Infinity +100.0 / 0 = Infinity 0.0 / 0 = NaN -100.0 / 0 = -Infinity -INFINITY / 0 = -InfinityTop
- public class ArithmeticOperators02 {
- public static void main(String[] args) {
- int i = 10000;
- int j = 1000000;
- int k;
- k = i / j * j;
- System.out.println("i / j * j = " + k);
- k = i * j / j;
- System.out.println("i * j / j = " + k);
- }
- }
□ 実行結果
$ java ArithmeticOperators02 i / j * j = 0 i * j / j = 1410
□ 実行結果
$ java ArithmeticOperators03 i / j * j = 0 i * j / j = 10000
- public class Bmi01 {
- public static void main(String[] args) {
- double weight = Double.parseDouble(args[0]);
- double hight = Double.parseDouble(args[1]);
- //
- // ここにコードを記述してください。
- //
- }
- }
□ 実行結果
$ java Bmi01 60 1.7 BMI = 20.761245674740486
- public class Bmi02 {
- public static void main(String[] args) {
- int weight = Integer.parseInt(args[0]);
- int hight = Integer.parseInt(args[1]);
- //
- // ここにコードを記述してください。
- //
- }
- }
□ 実行結果
$ java Bmi02 60 170 BMI = 20
- public class Area01 {
- public static void main(String[] args) {
- double a = Double.parseDouble(args[0]);
- double b = Double.parseDouble(args[1]);
- //
- // ここにコードを記述してください。
- //
- }
- }
□ 実行結果
$ java Area01 10 20 円の面積 = 314.0 三角形の面積 = 100.0 四角形の面積 = 200.0 $ java Area01 1.1 2.2 円の面積 = 3.7994000000000008 三角形の面積 = 1.2100000000000002 四角形の面積 = 2.4200000000000004
- public class Area02 {
- public static void main(String[] args) {
- double a = Double.parseDouble(args[0]);
- double b = Double.parseDouble(args[1]);
- //
- // ここにコードを記述してください。
- //
- }
- }
□ 実行結果
$ java Area01 1.1 2.2 円の面積 = 4 三角形の面積 = 1 四角形の面積 = 2
- public class Length01 {
- public static void main(String[] args) {
- double a = Double.parseDouble(args[0]);
- double b = Double.parseDouble(args[1]);
- //
- // ここにコードを記述してください。
- //
- }
- }
□ 実行結果
$ java Length01 1 1 円周の長さ = 6.283185307179586 三角形の周の長さ = 3.23606797749979 四角形の周の長さ = 4.0
a += b;は
a = a + b;を簡略に記したものです。ただし、型変換部分では異なります。 Top
- public class Exchange01 {
- public static void main(String[] args) {
- int a = Integer.parseInt(args[0]); // コマンドラインからの引数をint型に変換します。
- int b = Integer.parseInt(args[1]); // コマンドラインからの引数をint型に変換します。
- int t = a; // 一時的な変数 t を交換用に使用
- a = b;
- b = t;
- System.out.println("引数を交換して表示: " + a + ", " + b);
- }
- }
□ 実行結果
$ java Exchange01 2 5 引数を交換して表示: 5, 2 $ java Exchange01 2 -5 引数を交換して表示: -5, 2
前置 | 後置 | |
---|---|---|
++ | x = ++a; ... ++a; x = a; | x = a++; ... x = a; a++; |
-- | x = --a; ... --a; x = a; | x = a--; ... x = a; a--; |
- public class Increment {
- public static void main(String[] args) {
- int i = 0, j = 0;
- int p = 10, q = 10;
- System.out.println("before increment");
- System.out.println(" i = " + i + ", p = " + p);
- System.out.println(" j = " + j + ", q = " + q);
- System.out.println();
- i = p++;
- j = ++q;
- System.out.println("after increment");
- System.out.println(" i = " + i + ", p = " + p);
- System.out.println(" j = " + j + ", q = " + q);
- }
- }
□ 実行結果
$ java Increment before incriment i = 0, p = 10 j = 0, q = 10 after incriment i = 10, p = 11 j = 11, q = 11Top
演算子 | boolean 型を除く基本データ型 | boolean 型 | 参照型 |
---|---|---|---|
> (大きい) | v | - | - |
>= (以上) | v | - | - |
< (小さい) | v | - | - |
<= (以下) | v | - | - |
== (等しい) | v | v | v |
!= (等しくない) | v | v | v |
instanceof | - | - | v |
- public class RelationalOperators {
- public static void main(String[] args) {
- int i = Integer.parseInt(args[0]);
- int j = Integer.parseInt(args[1]);
- System.out.println(" i > j = " + (i > j));
- System.out.println(" j > i = " + (j > i));
- System.out.println();
- System.out.println(" i >= j = " + (i >= j));
- System.out.println(" j >= i = " + (j >= i));
- System.out.println();
- System.out.println(" i < j = " + (i < j));
- System.out.println(" j < i = " + (j < i));
- System.out.println();
- System.out.println(" i <= j = " + (i <= j));
- System.out.println(" j <= i = " + (j <= i));
- System.out.println();
- System.out.println(" i == j = " + (i == j));
- System.out.println(" j == i = " + (j == i));
- System.out.println();
- System.out.println(" i != j = " + (i != j));
- System.out.println(" j != i = " + (j != i));
- }
- }
□ 実行結果
$ java RelationalOperators 10 10 i > j = false j > i = false i >= j = true j >= i = true i < j = false j < i = false i <= j = true j <= i = true i == j = true j == i = true i != j = false j != i = falseTop
- public class EvenOdd01 {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]);
- System.out.println( /* ここにコードを記述してください */);
- }
- }
□ 実行結果
$ java EvenOdd01 4 true $ java OddEven01 5 false
a | b | a & b, a && b | a | b, a || b | a ^ b | !a | |
---|---|---|---|---|---|---|
false | false | false | false | false | true | |
false | true | false | true | true | true | |
true | false | false | true | true | false | |
true | true | true | true | false | false | |
- public class Range01 {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]);
- System.out.println(n >= 0 && n <= 100);
- }
- }
□ 実行結果
$ java Range01 -1 false $ java Range01 20 true $ java Range01 1000 false
- public class Range011 {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]); // コマンドラインからの引数をint型に変換します。
- System.out.println(" 例題: " + (n >= 0 && n <= 100));
- // ここにコードを記述してください。
- }
- }
□ 実行結果
$ java Range01 -1 例題: false ! を利用: true || を利用: true $ java Range01 20 例題: true ! を利用: false || を利用: false $ java Range01 1000 例題: false ! を利用: true || を利用: true
- public class LeapYear21 {
- public static void main(String[] args) {
- int year = Integer.parseInt(args[0]);
- // ここにコードを記述してください。
- }
- }
□ 実行結果
$ java LeapYear21 2004 true
- public class Range21 {
- public static void main(String[] args) {
- int a = Integer.parseInt(args[0]);
- int b = Integer.parseInt(args[1]);
- // ここにコードを記述してください。
- }
- }
□ 実行結果
$ java Range21 10 20 true
- public class Triangle21 {
- public static void main(String[] args) {
- int a = Integer.parseInt(args[0]);
- int b = Integer.parseInt(args[1]);
- int c = Integer.parseInt(args[2]);
- // ここにコードを記述してください。
- }
- }
□ 実行結果
$ java Triangle21 2 3 4 true
a | b | a & b | a | b | a ^ b | ~a | |
---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 1 | |
0 | 1 | 0 | 1 | 1 | 1 | |
1 | 0 | 0 | 1 | 1 | 0 | |
1 | 1 | 1 | 1 | 0 | 0 |
-x = ~x + 1
- public class BitOperators {
- public static void main(String[] args) {
- int i = 0x3; // 0000....0011
- int j = 0x5; // 0000....0101
- System.out.println("0011 & 0101 = " + Integer.toBinaryString(i & j));
- System.out.println("0011 | 0101 = " + Integer.toBinaryString(i | j));
- System.out.println("0011 ^ 0101 = " + Integer.toBinaryString(i ^ j));
- System.out.println(" ~ 0101 = " + Integer.toBinaryString( ~ j));
- }
- }
□ 実行結果
$ java BitOperators 0011 & 0101 = 1 0011 | 0101 = 111 0011 ^ 0101 = 110 ~ 0101 = 11111111111111111111111111111010Top
- public class Exchange11 {
- public static void main(String[] args) {
- int a = Integer.parseInt(args[0]); // コマンドラインからの引数をint型に変換します。
- int b = Integer.parseInt(args[1]); // コマンドラインからの引数をint型に変換します。
- a ^= b;
- b ^= a;
- a ^= b;
- System.out.println("引数を交換して表示: " + a + ", " + b);
- }
- }
□ 実行結果
$ java Exchange11 2 5 引数を交換して表示: 5, 2 $ java Exchange11 2 -5 引数を交換して表示: -5, 2
- public class BitOperators02 {
- public static void main(String[] args) {
- int a = Integer.parseInt(args[0], 16);
- int onMask = 0x00000100; // 00000000 00000000 00000001 00000000
- int offMask = 0xFFFFFEFF; // 11111111 11111111 11111110 11111111
- int notMask = 0x00000100; // 00000000 00000000 00000001 00000000
- System.out.println(" args = " + Integer.toBinaryString(a));
- System.out.println("bit 8 on = " + Integer.toBinaryString(a | onMask));
- System.out.println("bit 8 off = " + Integer.toBinaryString(a & offMask));
- System.out.println("bit 8 not = " + Integer.toBinaryString(a ^ notMask));
- }
- }
□ 実行結果
$ java BitOperators02 ffff args = 1111111111111111 bit 8 on = 1111111111111111 bit 8 off = 1111111011111111 bit 8 not = 1111111011111111 $ java BitOperators02 f0000 args = 11110000000000000000 bit 8 on = 11110000000100000000 bit 8 off = 11110000000000000000 bit 8 not = 11110000000100000000
- public class BitOperators03 {
- public static void main(String[] args) {
- int a = Integer.parseInt(args[0], 16);
- int onMask = 0x00000100; // 00000000 00000000 00000001 00000000
- System.out.println(" args = " + Integer.toBinaryString(a));
- System.out.println("bit 8 is on : " + ((a & onMask) != 0));
- }
- }
□ 実行結果
$ java BitOperators03 ffff args = 1111111111111111 bit 8 is on : true $ java BitOperators03 f000 args = 11110000000000000000 bit 8 is on : false
算術シフト | 論理シフト | ||
---|---|---|---|
符号つき | 符号なし | ||
左シフト | 2n倍 | m << n | なし |
右シフト | 1/2n倍 | m >> n | m >>> n |
- public class ShiftOperators {
- public static void main(String[] args) {
- int i = -20;
- int j = 3;
- System.out.println(Integer.toBinaryString(i) + " << " + j + " = " + Integer.toBinaryString(i << j) + " (" + (i << j) + ")");
- System.out.println(Integer.toBinaryString(i) + " >> " + j + " = " + Integer.toBinaryString(i >> j) + " (" + (i >> j) + ")");
- System.out.println(Integer.toBinaryString(i) + " >>> " + j + " = " + Integer.toBinaryString(i >>> j) + " (" + (i >>> j) + ")");
- }
- }
□ 実行結果
$ java ShiftOperators 11111111111111111111111111101100 << 3 = 11111111111111111111111101100000 (-160) 11111111111111111111111111101100 >> 3 = 11111111111111111111111111111101 (-3) 11111111111111111111111111101100 >>> 3 = 11111111111111111111111111101 (536870909)Top
- public class ShiftOperators02 {
- public static void main(String[] args) {
- int a = Integer.parseInt(args[0], 16);
- int n = Integer.parseInt(args[1]);
- int onMask = 0x00000001; // 00000000 00000000 00000000 00000001
- int offMask = 0x00000001; // 00000000 00000000 00000000 00000001
- int notMask = 0x00000001; // 00000000 00000000 00000000 00000001
- System.out.println(" args = " + Integer.toBinaryString(a));
- System.out.println("bit 8 on = " + Integer.toBinaryString(a | onMask << n));
- System.out.println("bit 8 off = " + Integer.toBinaryString(a & ~(offMask << n)));
- System.out.println("bit 8 not = " + Integer.toBinaryString(a ^ notMask << n));
- }
- }
□ 実行結果
$ java ShiftOperators02 ffff 8 args = 1111111111111111 bit 8 on = 1111111111111111 bit 8 off = 1111111011111111 bit 8 not = 1111111011111111 $ java ShiftOperators02 f000 8 args = 1111000000000000 bit 8 on = 1111000100000000 bit 8 off = 1111000000000000 bit 8 not = 1111000100000000
- public class Signeture01 {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]);
- int s = n >= 0? 1: -1;
- System.out.println("Signeture " + n + " is " + s);
- }
- }
□ 実行結果
$ java Signeture01 123 Signeture 123 is 1 $ java Signeture01 0 Signeture 0 is 1 $ java Signeture01 -987 Signeture -987 is -1
- public class Digits01 {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]);
- System.out.print((n < 10 ? "**" : "*") + n);
- }
- }
□ 実行結果
$ java Digits01 6 **6 $ java Digits01 26 *26Top
□ 実行結果
$ java Signeture02 123 Signeture 123 is 1 $ java Signeture02 0 Signeture 0 is 0 $ java Signeture02 -987 Signeture -987 is -1
(高い) 単項 > 算術 > シフト > 関係 > ビット > 論理 > 代入 (低い)
後置 | [ ] . (パラメーターのリスト) x++ x-- |
単項(*1) | ++x --x +x -x ~ ! |
生成 / キャスト | new (型)x |
算術 | * / % |
算術 | + - |
シフト | << >> >>> |
関係 | < > <= >= instanceof |
関係(等価) | == != |
ビット / 論理 | & |
ビット / 論理 | ^ |
ビット / 論理 | | |
論理 | && |
論理 | || |
3項 | ? : |
代入(*1) | = *= /= %= += -= <<= >>= >>>= &= ^= |= |
順次 | , |