Skip to content

Instantly share code, notes, and snippets.

@1206yaya
Last active August 16, 2019 10:56
Show Gist options
  • Save 1206yaya/3bfff42f72111ab44ecc to your computer and use it in GitHub Desktop.
Save 1206yaya/3bfff42f72111ab44ecc to your computer and use it in GitHub Desktop.
変数初期化について #java
package lesson.Silver.textBook;
import java.util.ArrayList;
/*
ラッパークラスの初期化はnullなのでそのまま演算などをするとNullPointerExceptionが発生。static メンバー変数は そのクラスがロードされた時点で default値で初期化されるので、 オブジェクトを代入しなくても コンパイルエラーにはならない
*/
class Test {
static byte _byte;
static short _short;
static char _char;
static int _int;
static long _long;
static float _float;
static double _double;
static boolean _boolean;
static Byte _Byte;
static Short _Short;
static Character _Character;
static Integer _Integer;
static Long _Long;
static Float _Float;
static Double _Double;
static Boolean _Boolean;
public static void main(String[] args) {
int local_member;
System.out.println(local_member); // 初期化されていないのでコンパイルエラー
System.out.println(_int + _int); // 0
System.out.println(_byte); // 0
System.out.println(_short); // 0
System.out.println(_char); //
System.out.println(_int); // 0
System.out.println(_long); // 0
System.out.println(_float); // 0.0
System.out.println(_double); // 0.0
System.out.println(_boolean); // false
System.out.println(_Integer + 1); // NullPointerException
System.out.println(_Byte); // null
System.out.println(_Short); // null
System.out.println(_Character); // null
System.out.println(_Integer); // null
System.out.println(_Long); // null
System.out.println(_Float); // null
System.out.println(_Double); // null
System.out.println(_Boolean); // null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment