Java Programming Tutorials

Java programming tutorials with many code examples!

Java primitive types default values

Problem:

What are default values of Java primitive types?

Solution:

If you don’t initialize fields their default values are as follows.

package com.farenda.solved;

public class JavaSolved {
    private static boolean booleanField;
    private static char charField;
    private static byte byteField;
    private static short shortField;
    private static int intField;
    private static long longField;
    private static float floatField;
    private static double doubleField;
    private static String objectField;

    public static void main(String[] args) {
        System.out.println("Default values of Java primitive types:");
        System.out.println("boolean: " + booleanField);
        // Need to cast to number to not print char with value 0
        System.out.println("char   : " + (int) charField);
        System.out.println("byte   : " + byteField);
        System.out.println("short  : " + shortField);
        System.out.println("int    : " + intField);
        System.out.println("long   : " + longField);
        System.out.println("float  : " + floatField);
        System.out.println("double : " + doubleField);
        System.out.println("object : " + objectField);
    }
}

Result:

Default values of Java primitive types:
boolean: false
char   : 0
byte   : 0
short  : 0
int    : 0
long   : 0
float  : 0.0
double : 0.0
object : null
Share with the World!