はじめに
この実験では、Java における 8 つのプリミティブデータ型について学びます。それらのサイズ、範囲、および初期値について学びます。また、オートボクシングについても学びます。
新しい Java ファイルを作成する
次のコマンドを使用して、新しい Java ファイルを作成し、それを PrimitivesLab.java と名付けます。
touch PrimitivesLab.java
int 型と byte 型のデータ型を宣言する
i という名前の int 型のデータ型と、b という名前の byte 型のデータ型を宣言します。それぞれに値 15 と 10 を割り当てます。
public class PrimitivesLab {
public static void main(String[] args){
int i = 15;
byte b = 10;
//Print the values
System.out.println("Value of i: " + i);
System.out.println("Value of b: " + b);
}
}
short 型と long 型のデータ型を宣言する
s という名前の short 型のデータ型を宣言し、それに値 1000 を割り当てます。l という名前の long 型のデータ型を宣言し、それに値 9999999L を割り当てます。
public class PrimitivesLab {
public static void main(String[] args){
int i = 15;
byte b = 10;
short s = 1000;
long l = 9999999L;
//Print the values
System.out.println("Value of i: " + i);
System.out.println("Value of b: " + b);
System.out.println("Value of s: " + s);
System.out.println("Value of l: " + l);
}
}
float 型と double 型のデータ型を宣言する
f という名前の float 型のデータ型を宣言し、それに値 3.14f を割り当てます。d という名前の double 型のデータ型を宣言し、それに値 2.71828 を割り当てます。
public class PrimitivesLab {
public static void main(String[] args){
int i = 15;
byte b = 10;
short s = 1000;
long l = 9999999L;
float f = 3.14f;
double d = 2.71828;
//Print the values
System.out.println("Value of i: " + i);
System.out.println("Value of b: " + b);
System.out.println("Value of s: " + s);
System.out.println("Value of l: " + l);
System.out.println("Value of f: " + f);
System.out.println("Value of d: " + d);
}
}
boolean 型と char 型のデータ型を宣言する
bool という名前の boolean 型のデータ型を宣言し、それに値 true を割り当てます。c という名前の char 型のデータ型を宣言し、それに値 'A' を割り当てます。
public class PrimitivesLab {
public static void main(String[] args){
int i = 15;
byte b = 10;
short s = 1000;
long l = 9999999L;
float f = 3.14f;
double d = 2.71828;
boolean bool = true;
char c = 'A';
//Print the values
System.out.println("Value of i: " + i);
System.out.println("Value of b: " + b);
System.out.println("Value of s: " + s);
System.out.println("Value of l: " + l);
System.out.println("Value of f: " + f);
System.out.println("Value of d: " + d);
System.out.println("Value of bool: " + bool);
System.out.println("Value of c: " + c);
}
}
オートボクシングを示す
x という名前の Integer 型のデータ型を宣言し、それに値 25 を割り当てることでオートボクシングを実証します。
public class PrimitivesLab {
public static void main(String[] args){
int i = 15;
byte b = 10;
short s = 1000;
long l = 9999999L;
float f = 3.14f;
double d = 2.71828;
boolean bool = true;
char c = 'A';
Integer x = 25;
//Print the values
System.out.println("Value of i: " + i);
System.out.println("Value of b: " + b);
System.out.println("Value of s: " + s);
System.out.println("Value of l: " + l);
System.out.println("Value of f: " + f);
System.out.println("Value of d: " + d);
System.out.println("Value of bool: " + bool);
System.out.println("Value of c: " + c);
System.out.println("Value of x: " + x);
}
}
整数のアンダーフローを示す
n という名前の整数型のデータ型を宣言し、それに値 2147483647 を割り当てます。n を 1 だけ増やし、出力を確認します。
public class PrimitivesLab {
public static void main(String[] args){
int i = 15;
byte b = 10;
short s = 1000;
long l = 9999999L;
float f = 3.14f;
double d = 2.71828;
boolean bool = true;
char c = 'A';
Integer x = 25;
int n = 2147483647;
n = n + 1;
System.out.println("Value is: " + n);
}
}
整数のオーバーフローを示す
m という名前の整数型のデータ型を宣言し、それに値 -2147483648 を割り当てます。m を 1 だけ減らし、出力を確認します。
public class PrimitivesLab {
public static void main(String[] args){
int i = 15;
byte b = 10;
short s = 1000;
long l = 9999999L;
float f = 3.14f;
double d = 2.71828;
boolean bool = true;
char c = 'A';
Integer x = 25;
int n = 2147483647;
n = n + 1;
System.out.println("Value is: " + n);
int m = -2147483648;
m = m - 1;
System.out.println("Value is: " + m);
}
}
プログラムをコンパイルして実行する
端末で以下のコマンドを使用してプログラムをコンパイルします。
javac PrimitivesLab.java
以下のコマンドを使用してプログラムを実行します。
java PrimitivesLab
まとめ
この実験では、Java における 8 つのプリミティブデータ型について学びました。Java プログラムを作成し、各プリミティブデータ型の変数を宣言しました。オートボクシングと、Java がプリミティブデータ型を対応するラッパークラスに自動的に変換する方法について学びました。また、整数型データ型におけるオーバーフローとアンダーフローについても学びました。最後に、プログラムをコンパイルして実行して出力を確認しました。これらのデータ型を使用して、プログラミングでデータを効率的に格納および操作することができます。



