Learn Java Variables and Data Types (Day 2): Java for Beginners

Learn Java Variables and Data Types (Day 2) Java for Beginners

Day 2: Java Variables and Data Types – Learn Java for Beginners

Welcome back to Day 2 of our 100 Days of Java course. Yesterday, we installed Java, set up Eclipse, and ran our first program. Today, we’ll dive into the foundation of every Java program, variables and data types.

By the end of this lesson, you’ll understand how Java stores and manages data, and you’ll be writing your own variable-based programs confidently.

What Are Variables in Java?

Think of a variable as a labeled container where you can store data.
You can put something in it, use it, or even replace it later.

For example:

int age = 25;
String name = "John";
double height = 5.9;

Here’s what’s happening:

  • int age = 25; → stores an integer value
  • String name = "John"; → stores text (sequence of characters)
  • double height = 5.9; → stores a decimal number

In Java, you must declare the type of data before using a variable, that’s why it’s called a strongly typed language.

Java Data Types Explained

Java has two main categories of data types:

1. Primitive Data Types

These are the basic building blocks:

TypeSizeExampleDescription
byte1 bytebyte age = 20;Small integers
short2 bytesshort salary = 32000;Medium integers
int4 bytesint marks = 95;Common integer type
long8 byteslong population = 8000000L;Large integers
float4 bytesfloat weight = 65.5f;Decimal numbers (less precise)
double8 bytesdouble price = 199.99;Decimal numbers (more precise)
char2 byteschar grade = 'A';Single characters
boolean1 bitboolean isJavaFun = true;True or false values

2. Non-Primitive Data Types

These include Strings, Arrays, Classes, and Objects.

Example:

String language = "Java";

Strings are actually objects, not primitive types, but they behave like basic data in most cases.


Example: Using Variables in Java

Let’s write a simple program to use all these types together.

public class VariablesDemo {
    public static void main(String[] args) {
        int age = 25;
        String name = "John";
        double height = 5.9;
        char grade = 'A';
        boolean isJavaFun = true;

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Height: " + height);
        System.out.println("Grade: " + grade);
        System.out.println("Java is fun: " + isJavaFun);
    }
}

Output

Name: John
Age: 25
Height: 5.9
Grade: A
Java is fun: true

This is how Java variables and data types help you store and organize data in your programs.

Pro Tip

If you’re unsure which type to use:

  • Use int for whole numbers.
  • Use double for decimals.
  • Use String for text.
  • Use boolean for true/false values.

Java automatically handles memory efficiently, but choosing the right type keeps your programs fast and clean.

What’s Next?

You’ve mastered Java variables and data types. Next, in Day 3, we’ll explore Java operators, the tools you’ll use to perform calculations and logic.

You can also check out:

About Us

If the information you are looking for is not available here, please contact us. Additionally, follow us on our social media platforms for updates and more information.

Leave a Comment

Your email address will not be published. Required fields are marked *