分類 | 型 | ビット数 (バイト数) | 範囲 |
---|---|---|---|
整数型 | byte | 8 (1) | -128 ~ 127 |
short | 16 (2) | -32768 ~ 32767 | |
int | 32 (4) | -2147483648(-2G, -2*109) ~ 2147483647(2G, 2*109) | |
long | 64 (8) | -9223372036854775808(-9*1018) ~ 9223372036854775807(9*1018) | |
実数型 | float | 32 (4) | 1.4E-45 ~ 3.4028235E+38 |
double | 64 (8) | 4.9E-324 ~ 1.7976931348623157E308 | |
文字型 | char | 16 (2) | 0 ~ 65535 |
boolean 型 | boolean | * (*) | true / false |
|
図 変数 |
- public class PrimitiveData01 {
- public static void main(String[] args) {
- System.out.println("byte : " + Byte.MIN_VALUE + " ~ " + Byte.MAX_VALUE);
- System.out.println("short : " + Short.MIN_VALUE + " ~ " + Short.MAX_VALUE);
- System.out.println("int : " + Integer.MIN_VALUE + " ~ " + Integer.MAX_VALUE);
- System.out.println("long : " + Long.MIN_VALUE + " ~ " + Long.MAX_VALUE);
- System.out.println("float : " + Float.MIN_VALUE + " ~ " + Float.MAX_VALUE);
- System.out.println("double : " + Double.MIN_VALUE + " ~ " + Double.MAX_VALUE);
- System.out.println("char : " + (int)Character.MIN_VALUE + " ~ " + (int)Character.MAX_VALUE);
- }
- }
□ 実行結果
$java PrimitiveData01 byte : -128 ~ 127 short : -32768 ~ 32767 int : -2147483648 ~ 2147483647 long : -9223372036854775808 ~ 9223372036854775807 float : 1.4E-45 ~ 3.4028235E38 double : 4.9E-324 ~ 1.7976931348623157E308 char : 0 ~ 65535
- public class Scope01 {
- public static void main(String[] args) {
- for (int i = 0; i < 10; i++) {
- System.out.print(i + ", ");
- }
- System.out.println(i);
- }
- }
$ javac Scope01.java Scope01.java:6: シンボルを解釈処理できません。 シンボル: 変数 i 位置 : Scope01 の クラス System.out.println(i); ^ エラー 1 個
- public class Scope02 {
- public static void main(String[] args) {
- int i;
- for (i = 0; i < 10; i++) {
- System.out.print(i + ", ");
- }
- System.out.println(i);
- }
- }
□ 実行結果
$ java Scope02 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
\b | バックスペース |
\t | タブ |
\n | 改行 |
\f | 用紙送り |
\r | 復帰改行 |
\" | ダブル・クオート |
\' | シングル・クオート |
\\ | バックスラッシュ |
\xxx | 000-377までの8進数で表現されるUnicode文字 |
\uxxxx | 0000-ffffまでの16進数で表現されるUnicode文字 |
- public class Variable50 {
- public static void main(String[] args) {
- byte b = 10;
- short s = -200;
- int i = 3000;
- long l = -40000;
- float f = 5.6f;
- double d = -78.901e23;
- char c = 'a';
- boolean bo = true;
- System.out.println("byte : " + b);
- System.out.println("short : " + s);
- System.out.println("int : " + i);
- System.out.println("long : " + l);
- System.out.println("float : " + f);
- System.out.println("double : " + d);
- System.out.println("char : " + c);
- System.out.println("boolean : " + bo);
- }
- }
□ 実行結果
$java Variable50 byte : 10 short : -200 int : 3000 long : -40000 float : 5.6 double : -7.8901E24 char : a boolean : true
- public class Variable51 {
- public static void main(String[] args) {
- char c = 'a'; // char 型
- String s = "a"; // String 型
- System.out.println("char : " + c); // char 型
- System.out.println("String : " + s); // String 型
- }
- }
□ 実行結果
$java Variable51 char : a String : a
- public class Concat01 {
- public static void main(String[] args) {
- int i = 10;
- int j = 20;
- System.out.println("i + j = " + i + j);
- System.out.println(i + j + " = i + j");
- }
- }
□ 実行結果
$java Concat01 i + j = 1020 30 = i + j