byte b = 10; ... 10 は byte 型の範囲内 b += 10;
int a = 10, b = 20; "a = " + a ... "a = 10" "a + b = " + a + b ... "a + b = 1020" boolean c = false; "c = " + c ... "c = false"
byte b = (byte)128; ... 128 は byte 型の範囲外 b = (byte)(b + 10); ... b + 10 の結果は int 型 float f = 10.0F; ... 10.0 は double 型
- public class TypeChange11 {
- public static void main(String[] args) {
- byte b = Byte.MAX_VALUE;
- short s = Short.MAX_VALUE;
- int i = Integer.MAX_VALUE;
- long l = Long.MAX_VALUE;
- float f;
- double d = Math.PI;
- b = (byte)s;
- System.out.println("short: " + s + " -> byte: " + b);
- s = (short)i;
- System.out.println("int: " + i + " -> short: " + s);
- i = (int)l;
- System.out.println("long: " + l + " -> int: " + i);
- f = (float)d;
- System.out.println("double: " + d + " -> float: " + f);
- i = Integer.MAX_VALUE;
- f = i;
- System.out.println("int: " + i + " -> float: " + f);
- f = l;
- System.out.println("long: " + l + " -> float: " + f);
- d = l;
- System.out.println("long: " + l + " -> double: " + d);
- }
- }
□ 実行結果
$ java TypeChange11 short: 32767 -> byte: -1 int: 2147483647 -> short: -1 long: 9223372036854775807 -> int: -1 double: 3.141592653589793 -> float: 3.1415927 int: 2147483647 -> float: 2.14748365E9 long: 9223372036854775807 -> float: 9.223372E18 long: 9223372036854775807 -> double: 9.223372036854776E18
- public class CodePrint01 {
- public static void main(String[] args) {
- char c = 'a';
- System.out.println(???? + "'s code is " + ????);
- }
- }
□ 実行結果
a's code is 97
- public class CodePrint02 {
- public static void main(String[] args) {
- char c = ????????????? ;
- System.out.println("'" + c + "': " + ???????????????????? );
- }
- }
□ 実行結果
$ java CodePrint02 a 'a': 61
- public class NegativeNumber {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]);
- int nn = ????;
- System.out.print("Decimal: ");
- System.out.println("-(" + n + ") = " + nn);
- System.out.print("HexDecimal: ");
- System.out.println("-(" + Integer.toHexString(n) + ") = " + Integer.toHexString(nn));
- }
- }
□ 実行結果
$ java NegativeNumber 1234 Decimal: -(1234) = -1234 HexDecimal: -(4d2) = fffffb2e
- public class Encoding01 {
- public static void main(String[] args) {
- int a = args[0].charAt(0);
- int key = 0x3;
- int b = ?????? ;
- System.out.println((char)a + "(" + Integer.toBinaryString(a)
- + ") encode to: "
- + (char)b + "(" + Integer.toBinaryString(b) + ")");
- }
- }
□ 実行結果
$ java Encoding01 a a(1100001) encode to: b(1100010)