public class Main {
public static void main(String[] args) {
int a = 38;
int b = 5;
// 割り算の結果を余りつきで表示する
System.out.println(a + " / " + b + " = " + a / b + " 余り " + a % b);
}
}
実行結果
public class Main {
public static void main(String[] args) {
byte a = 83;
byte b = -35;
// a, bを2進数の形の文字列に変換する
String aBinary = String.format("%8s", Integer.toBinaryString(a & 0xFF)).replace(' ', '0');
String bBinary = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
// aを2進数, 10進数の形で並べて表示する
System.out.println("a = \t\t" + aBinary + " (" + a + ")");
// aの補数を計算する
byte c = (byte) ~a;
// aを1左シフトさせる
byte d = (byte) (a << 1);
// aを1右シフトさせる
byte e = (byte) (a >> 1);
// aとbのビット論理積を計算する
byte f = (byte) (a & b);
// aとbのビット論理和を計算する
byte g = (byte) (a | b);
// aとbのビット排他的論理和を計算する
byte h = (byte) (a ^ b);
// cを2進数の形の文字列に変換する
String cBinary = String.format("%8s", Integer.toBinaryString(c & 0xFF)).replace(' ', '0');
String dBinary = String.format("%8s", Integer.toBinaryString(d & 0xFF)).replace(' ', '0');
String eBinary = String.format("%8s", Integer.toBinaryString(e & 0xFF)).replace(' ', '0');
String fBinary = String.format("%8s", Integer.toBinaryString(f & 0xFF)).replace(' ', '0');
String gBinary = String.format("%8s", Integer.toBinaryString(g & 0xFF)).replace(' ', '0');
String hBinary = String.format("%8s", Integer.toBinaryString(h & 0xFF)).replace(' ', '0');
// 計算結果を2進数, 10進数の形で並べて表示する
System.out.println("~a = \t\t" + cBinary + " (" + c + ")");
System.out.println("a << 1 = \t" + dBinary + " (" + d + ")");
System.out.println("a >> 1 = \t" + eBinary + " (" + e + ")");
// a, bを2進数, 10進数の形で並べて表示する
System.out.println("a = \t\t" + aBinary + " (" + a + ")");
System.out.println("b = \t\t" + bBinary + " (" + b + ")");
System.out.println("a & b = \t" + fBinary + " (" + f + ")");
System.out.println("a | b = \t" + gBinary + " (" + g + ")");
System.out.println("a ^ b = \t" + hBinary + " (" + h + ")");
}
}
実行結果
public class Main {
public static void main(String[] args) {
int a = 10; // 元の値
int b; // 表示用の変数
// 前置インクリメント
b = ++a;
System.out.println("前置インクリメントの結果 : a = " + a + ", b = " + b);
// aを元に戻す
a = 10;
// 後置インクリメント
b = a++;
System.out.println("後置インクリメントの結果 : a = " + a + ", b = " + b);
// aを元に戻す
a = 10;
// 前置デクリメント
b = --a;
System.out.println("前置デクリメントの結果 : a = " + a + ", b = " + b);
// aを元に戻す
a = 10;
// 後置デクリメント
b = a--;
System.out.println("後置デクリメントの結果 : a = " + a + ", b = " + b);
}
}
実行結果
public class Main {
public static void main(String[] args) {
int a = 10;
// 加算代入の例を表示する
a += 5;
System.out.println("a += 5を行った結果, a = " + a);
// 減算代入の例を表示する
a -= 3;
System.out.println("a -= 3を行った結果, a = " + a);
// 乗算代入の例を表示する
a *= 4;
System.out.println("a *= 4を行った結果, a = " + a);
// 除算代入の例を表示する
a /= 2;
System.out.println("a /= 2を行った結果, a = " + a);
// 剰余代入の例を表示する
a %= 7;
System.out.println("a %= 7を行った結果, a = " + a);
byte b = 10;
byte c = 6;
// b, cを2進数の形の文字列に変換する
String bStr = String.format("%8s", Integer.toBinaryString(b)).replace(" ", "0");
String cStr = String.format("%8s", Integer.toBinaryString(c)).replace(" ", "0");
System.out.println("b = " + bStr);
System.out.println("c = " + cStr);
// 論理積代入の例を表示する
b &= c;
System.out.println("b &= cを行った結果, b = " + String.format("%8s", Integer.toBinaryString(b)).replace(" ", "0"));
// bを元に戻す
b = 10;
// 論理和代入の例を表示する
b |= c;
System.out.println("b |= cを行った結果, b = " + String.format("%8s", Integer.toBinaryString(b)).replace(" ", "0"));
// bを元に戻す
b = 10;
// 排他的論理和代入の例を表示する
b ^= c;
System.out.println("b ^= cを行った結果, b = " + String.format("%8s", Integer.toBinaryString(b)).replace(" ", "0"));
// bを元に戻す
b = 10;
// 左シフト代入の例を表示する
b <<= 2;
System.out.println("b <<= 2を行った結果, b = " + String.format("%8s", Integer.toBinaryString(b)).replace(" ", "0"));
// bを元に戻す
b = 10;
// 右シフト代入の例を表示する
b >>= 2;
System.out.println("b >>= 2を行った結果, b = " + String.format("%8s", Integer.toBinaryString(b)).replace(" ", "0"));
}
}
実行例
public class Main {
public static void main(String[] args) {
// 乗算が加算より優先されることを示す
int a = 4;
int b = 2;
int c = 3;
// a + b * c の計算結果を段階的に表示する
System.out.println(" " + a + "+" + b + "*" + c);
System.out.println("=" + a + "+" + b * c);
System.out.println("=" + (a + b * c));
System.out.println();
// (a+b)*c の計算結果を段階的に表示する
System.out.println(" (" + a + "+" + b + ")" + "*" + c);
System.out.println("=" + (a + b) + "*" + c);
System.out.println("=" + (a + b) * c);
System.out.println();
// 論理積が論理和より優先されることを示す
boolean d = true;
boolean e = false;
boolean f = false;
// d || e && f の計算結果を段階的に表示する
System.out.println(" " + d + "||" + e + "&&" + f);
System.out.println("=" + d + "||" + (e && f));
System.out.println("=" + (d || e && f));
System.out.println();
// (d || e) && f の計算結果を段階的に表示する
System.out.println(" (" + d + "||" + e + ")" + "&&" + f);
System.out.println("=" + (d || e) + "&&" + f);
System.out.println("=" + ((d || e) && f));
}
}
実行例
public class Main {
public static void main(String[] args) {
double a = 3.14159265358979323846264338327950288419716939937510582097;
// aをfloat型にキャスト
float b = (float) a;
// bをint型にキャスト
int c = (int) b;
// cをdouble型にキャスト
double d = (double) c;
System.out.println("double型の変数aの値: " + a);
System.out.println("float型の変数bの値: " + b);
System.out.println("int型の変数cの値: " + c);
System.out.println("double型の変数dの値: " + d);
int e = 10;
int f = 3;
// 整数どうしの割り算の式を表示する
System.out.println(e + "/" + f + "=" + e / f);
// 変数eをdouble型にキャストして割り算の式を表示する
System.out.println("(double)" + e + "/" + f + "=" + (double) e / f);
}
}
実行例