こんにちは、もちおです。
今週末はみずほ銀行のATMが休止するそうです。
そのニュースのおかげで今週末が三連休だと知りました。
ありがとう、みずほ銀行。
・Java確認テスト
Javaの研修が一通り終了したため、確認テストを行いました。
ポリモーフィズム以外は、ほぼほぼ問題なかったように思います。
(オーバーライドとオーバーロードって、紛らわしいですよね。)
本日連携したいのは、Javaで宣言した変数の初期値についてです。
テストの中で初めて知ったのですが、Javaではローカル変数以外は初期値が自動で代入されるようです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class sample { public static void main(String[] args) { int IntInit; byte ByteInit; short ShortInit; long LongInit; float FloatInit; double DoubleInit; char CharInit; boolean BooleanInit; String StringInit; System.out.println(ByteInit); System.out.println(ShortInit); System.out.println(IntInit); System.out.println(LongInit); System.out.println(FloatInit); System.out.println(DoubleInit); System.out.println(CharInit); System.out.println(BooleanInit); System.out.println(StringInit); } } |
1 2 3 4 5 6 7 8 9 10 |
Exception in thread "main" java.lang.Error: Unresolved compilation problems: ローカル変数 ByteInit が初期化されていない可能性があります ローカル変数 ShortInit が初期化されていない可能性があります ローカル変数 IntInit が初期化されていない可能性があります ローカル変数 LongInit が初期化されていない可能性があります ローカル変数 FloatInit が初期化されていない可能性があります ローカル変数 DoubleInit が初期化されていない可能性があります ローカル変数 CharInit が初期化されていない可能性があります ローカル変数 BooleanInit が初期化されていない可能性があります ローカル変数 StringInit が初期化されていない可能性があります |
ローカル変数はそもそもエラーになります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public class sample1 { public static void main(String[] args) { sample2.print(); } } class sample2{ static int IntInit; static byte ByteInit; static short ShortInit; static long LongInit; static float FloatInit; static double DoubleInit; static char CharInit; static boolean BooleanInit; static String StringInit; public static void print() { System.out.println(ByteInit); System.out.println(ShortInit); System.out.println(IntInit); System.out.println(LongInit); System.out.println(FloatInit); System.out.println(DoubleInit); System.out.println(CharInit); System.out.println(BooleanInit); System.out.println(StringInit); } } |
1 2 3 4 5 6 7 8 9 |
0 //int 0 //byte 0 //short 0 //long 0.0 //float 0.0 //double //char false //boolean null //String |
クラス変数では初期値の宣言をしなくても、それぞれ初期値が自動で代入されていることがわかります。
Javaは融通がきく。
ただし過信は禁物。