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?

Default values of Java types
TypeDefault valueSimple explanation
byte, short, int, long0The initial integer value is zero.
float, double0.0The initial floating-point value is zero.
booleanfalseThe condition is initially considered false.
char'\u0000'The null character, which is usually invisible on screen.
Reference types, such as StringnullThe 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);
Console output

0
0.0
false
0
null

  1. prekiuKiekis is int, so its initial value is 0.
  2. bendraKaina is double, so its initial value is 0.0.
  3. apmoketa is boolean, so its initial value is false.
  4. char we cannot see the null character, so we convert it to int and print its code 0.
  5. pirkejoVardas does not yet refer to a String object, so its value is null.

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]);
Console output

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.

More about Java arrays

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
}
Result

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ų.");
}
Console output

Ieva yra 12 metų.

More about local variables

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

  • null means that the reference points to no object.
  • "" is an existing, empty String object.
  • Through null you cannot call a method – doing so can cause NullPointerException.
  • 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.