| データ型 | サイズ | 表現できる値 | |
|---|---|---|---|
| 整数 | byte | 8ビット | -128 〜 127 |
| short | 16ビット | -32,768 〜 32,767 | |
| int | 32ビット | -2,147,483,648 〜 2,147,483,647 | |
| long | 64ビット | -9,223,372,036,854,775,808 〜 9,223,372,036,854,775,807 | |
| 浮動小数 | float | 32ビット | 32ビット単精度浮動小数点数 |
| double | 64ビット | 64ビット倍精度浮動小数点数 | |
| 文字 | char | 16ビット | \u0000 〜 \uffff(Unicode) |
| 真偽値 | boolean | - | true又はfalse |
| Sample2_3_1.java |
|---|
public class Sample2_3_1 {
public static void main(String[] args) {
new DataType().view();
}
}
class DataType {
void view() {
byte b = 100;
short s = 150;
int i = 200;
long l = 2147483648L; // long型を使う場合、intのサイズを超える場合は後尾にLを付ける
float f = 0.35F; // float型の変数に値を入れる場合は、常に後尾にFを付ける
double d = 10.05; // double型の場合は、何も付けなくていい。
char c = 'A';
boolean bl = 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 = " + bl);
}
}
|
byte = 100 short = 150 int = 200 long = 2147483648 float = 0.35 double = 10.05 char = A boolean = true |
| Sample2_3_2.java |
|---|
public class Sample2_3_2 {
public static void main(String[] args) {
new SampleString().view();
}
}
class SampleString {
void view() {
String str = "サンプル文字列";
System.out.println("文字列を表示 = " + str);
System.out.println("文字列の長さ = " + str.length());
}
}
|
文字列を表示 = サンプル文字列 文字列の長さ = 7 |
| 2項算術演算子 | 単項算術演算子 | |||||
|---|---|---|---|---|---|---|
| + | 2項+演算子 | 加算 | + | 単項+演算子 | プラス | |
| - | 2項-演算子 | 減算 | - | 単項-演算子 | マイナス | |
| * | 2項*演算子 | 乗算 | ||||
| / | 2項/演算子 | 除算 | ||||
| % | 2項%演算子 | 剰余算 | ||||
| Sample2_3_3.java |
|---|
public class Sample2_3_3 {
public static void main(String[] args) {
new Arithmetic().view();
}
}
class Arithmetic {
void view() {
int arit1, arit2, arit3, arit4, arit5, arit6, arit7, arit8;
arit1 = 5 + 2; // 2項+演算子
arit2 = 10 - 8; // 2項-演算子
arit3 = 5 * 3; // 2項*演算子
arit4 = 21 / 3; // 2項/演算子
arit5 = 20 % 3; // 2項%演算子
arit6 = 20 / 3; // 小数は切り捨てられる(int型の範囲で記憶する)
arit7 = +10; // 単項+演算子
arit8 = -10; // 単項-演算子
System.out.println("5 + 2 = " + arit1);
System.out.println("10 - 8 = " + arit2);
System.out.println("5 * 3 = " + arit3);
System.out.println("21 / 3 = " + arit4);
System.out.println("20 % 3 = " + arit5);
System.out.println("20 / 3 = " + arit6);
System.out.println("+10 = " + arit7);
System.out.println("-10 = " + arit8);
}
}
|
5 + 2 = 7 10 - 8 = 2 5 * 3 = 15 21 / 3 = 7 20 % 3 = 2 20 / 3 = 6 +10 = 10 -10 = -10 |