JAVA BASICS IN DETAIL
Java default values
When an object or array is created, Java automatically fills its fields or elements with initial values. These are called default values. This rule does not apply to local variables.
Which value does Java assign?
| Type | Default value | Simple explanation |
|---|---|---|
byte, short, int, long | 0 | The initial integer value is zero. |
float, double | 0.0 | The initial floating-point value is zero. |
boolean | false | The condition is initially considered false. |
char | '\u0000' | The null character, which is usually invisible on screen. |
Reference types, such as String | null | The reference does not yet point to an object. |
Default values of object fields
If we do not assign values to class fields ourselves, Java supplies them when creating the object:
class PirkiniuKrepselis {
int prekiuKiekis;
double bendraKaina;
boolean apmoketa;
char kategorija;
String pirkejoVardas;
}
PirkiniuKrepselis krepselis = new PirkiniuKrepselis();
System.out.println(krepselis.prekiuKiekis);
System.out.println(krepselis.bendraKaina);
System.out.println(krepselis.apmoketa);
System.out.println((int) krepselis.kategorija);
System.out.println(krepselis.pirkejoVardas);0
0.0
false
0
null
prekiuKiekisisint, so its initial value is0.bendraKainaisdouble, so its initial value is0.0.apmoketaisboolean, so its initial value isfalse.charwe cannot see the null character, so we convert it tointand print its code0.pirkejoVardasdoes not yet refer to aStringobject, so its value isnull.
Array elements are filled too
int[] taskai = new int[3];
boolean[] atlikta = new boolean[2];
String[] vardai = new String[2];
System.out.println(taskai[0]);
System.out.println(atlikta[0]);
System.out.println(vardai[0]);0
false
null
new creates all the array slots at once. Even before we write anything, each slot already holds the default value for its type.
Local variables do not receive default values
We must initialize a variable declared inside a method ourselves before using it:
public static void main(String[] args) {
int amzius;
System.out.println(amzius); // Error: amzius has not been initialised
}The code does not compile: the compiler reports that the variable amzius might not have been initialized.
Java prevents us from accidentally using an unknown value. A variable needs a value before we try to read it.
public static void main(String[] args) {
int amzius = 12;
String vardas = "Ieva";
System.out.println(vardas + " yra " + amzius + " metų.");
}Ieva yra 12 metų.
It is often better to specify a value explicitly
class Lempa {
boolean ijungta = true;
int ryskumas = 50;
String kambarys = "Virtuvė";
} Although Java could provide false, 0 or null, a meaningful initial value expresses the programmer's intention more clearly. In this example, we can immediately see the lamp's initial state.
null is not empty text
nullmeans that the reference points to no object.""is an existing, emptyStringobject.- Through
nullyou cannot call a method – doing so can causeNullPointerException. - A default value is not necessarily meaningful for the program, so initialize important fields deliberately.
What should you remember?
- Object fields and array elements receive default values automatically.
- Numbers start at zero,
boolean–false. - The initial value for reference types is
null. - You must initialize local variables yourself before using them.
- An explicit initial value often makes code easier to understand.