Data Types in Java

Java is a statically typed language. Every variable must have a declared data type, which defines the kind of values it can store and the operations allowed on it.


  • Java has two broad categories: Primitive and Non-Primitive (Reference) data types.

  • Class fields (instance/static) get default values automatically, but local variables must be initialized before use.

  • Primitive values are stored directly; non-primitives store references to objects.

Data Types in Java - prowessapps.com

Primitive Data Types

Java provides eight primitive data types. They are predefined by the language and represented by reserved keywords.

Data Type Size Default Value (for fields)
boolean1 bitfalse
char2 bytes'\u0000'
byte1 byte0
short2 bytes0
int4 bytes0
long8 bytes0L
float4 bytes0.0f
double8 bytes0.0d
Example: Default Values
DefaultValuesTest.java Copy Code
public class DefaultValuesTest {
    // Instance (non-static) primitives
    byte b; short sh; int a; long l;
    float f; double d; char ch; boolean bool;

    // Static field
    static int x;

    // Reference type
    String s;

    void showValues() {
        System.out.println("Default Value of byte b : " + b);
        System.out.println("Default Value of short sh : " + sh);
        System.out.println("Default Value of int a : " + a);
        System.out.println("Default Value of long l : " + l);
        System.out.println("Default Value of float f : " + f);
        System.out.println("Default Value of double d : " + d);
        System.out.println("Default Value of char ch : " + ch);
        System.out.println("Default Value of boolean bool : " + bool);
        System.out.println("Default Value of static int x : " + x);
        System.out.println("Default Value of reference String s : " + s);
    }

    public static void main(String[] args) {
        DefaultValuesTest ob = new DefaultValuesTest();
        ob.showValues();
    }
}
    

Output:

Default Value of byte b : 0
Default Value of short sh : 0
Default Value of int a : 0
Default Value of long l : 0
Default Value of float f : 0.0
Default Value of double d : 0.0
Default Value of char ch : 
Default Value of boolean bool : false
Default Value of static int x : 0
Default Value of reference String s : null
Notes
  • Default values apply to fields (instance/static) only; local variables must be initialized.
  • boolean has only two values: true and false (no 0/1 shortcuts).
  • The default for a reference (e.g., String) is null.

Next: Java Identifiers



πŸš€ Quick Knowledge Check

Topic: Data-types | Language: Java

Q1. What will be the output of the following code?
int x = 130;
byte y = (byte) x;
System.out.println(y);
Q2. Which data type is used to store a single character in Java?
Q3. Which of the following is NOT a primitive data type in Java?
Q4. What will be the output of this code?
double a = 10/4;
System.out.println(a);
Q5. What is the default value of a boolean variable in Java?