I created my class (exending Activity) with an attribute int a.
The attribute is automatically initialized to 0 in the method onCreate().
Is it normal?
I created my class (exending Activity) with an attribute int a.
The attribute is automatically initialized to 0 in the method onCreate().
Is it normal?
This is normal. You see, "int" is a primitive type. It is not an object, and so, it cannot hold a "null" value. If you want your variable to be null at onCreate(), you must change its type to the Object representation of it. The "Integer" class represents the primitive type "int".
in the method onCreate().
No, it's not initialized in onCreate()
. It's initialized when object of your class is instantiated.
0
Yes it is. Variable of int
is primitive type can only hold numeric values and unless you initialize it other way it will be assigned 0
(contrary to i.e. Integer
which can also be null
).
See docs:
https://docs.oracle.com/javase/tutorial/java/javaOO/index.html https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
From Official Java Tutorial:
Fields that are declared but not initialized will be set to a reasonable default by the compiler.
Check default values for each data type: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html