Skip to main content

Command Palette

Search for a command to run...

Java Variables and Data Types Explained with Examples

Updated
4 min read
M
Product Engineer with 3.4 years of experience in Java, Spring Boot, React, Vue, SQL, AWS, and Warehouse Management Systems. Passionate about backend development and software engineering.

Introduction

Variables and data types are fundamental concepts in Java programming. Every Java application uses variables to store and manipulate data.

In this article, we will learn what variables are, the different types of data types available in Java, and how to use them with examples.


What is a Variable?

A variable is a named memory location used to store data.

Think of a variable as a container that holds a value.

Syntax

dataType variableName = value;

Example

int age = 28;
String name = "Manikandan";

Here:

  • age is a variable

  • 28 is the value stored in age

  • name is a variable

  • Manikandan is the value stored in name


Rules for Naming Variables

A variable name:

✅ Can contain letters, digits, _ and $

✅ Must begin with a letter, _ or $

❌ Cannot start with a number

❌ Cannot use Java keywords

Valid Examples

int age;
String employeeName;
double salary;

Invalid Examples

int 1age;
String class;

Data Types in Java

Java data types are divided into two categories:

1. Primitive Data Types

Primitive data types store simple values.

Data Type Size Example
byte 1 byte 100
short 2 bytes 1000
int 4 bytes 100000
long 8 bytes 100000L
float 4 bytes 10.5f
double 8 bytes 10.55
char 2 bytes 'A'
boolean 1 bit true

Integer Types Example

public class IntegerExample {
    public static void main(String[] args) {

        byte b = 100;
        short s = 1000;
        int i = 100000;
        long l = 1000000L;

        System.out.println(b);
        System.out.println(s);
        System.out.println(i);
        System.out.println(l);
    }
}

Decimal Types Example

public class DecimalExample {
    public static void main(String[] args) {

        float price = 99.99f;
        double salary = 50000.50;

        System.out.println(price);
        System.out.println(salary);
    }
}

Character Data Type

The char data type stores a single character.

char grade = 'A';

System.out.println(grade);

Output:

A

Boolean Data Type

Boolean stores either true or false.

boolean isJavaEasy = true;

System.out.println(isJavaEasy);

Output:

true

Non-Primitive Data Types

Non-primitive data types are created by programmers or provided by Java libraries.

Examples:

  • String

  • Arrays

  • Classes

  • Objects

String Example

String name = "Manikandan";

System.out.println(name);

Output:

Manikandan

Variable Declaration and Initialization

Declaration

int age;

Initialization

age = 28;

Declaration and Initialization Together

int age = 28;

Complete Example

public class VariablesDemo {

    public static void main(String[] args) {

        String name = "Manikandan";
        int age = 28;
        double salary = 45000.50;
        boolean employed = true;

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Salary: " + salary);
        System.out.println("Employed: " + employed);
    }
}

Best Practices

  • Use meaningful variable names.

  • Follow camelCase naming convention.

  • Choose appropriate data types.

  • Avoid single-letter variable names unless necessary.

Good Example

String employeeName;
double annualSalary;

Bad Example

String x;
double y;

Conclusion

Variables and data types are the building blocks of Java programming. Understanding how to declare variables and use different data types is essential before moving on to operators, conditions, loops, and object-oriented programming concepts.

In the next article, we will learn about Operators in Java with Examples.