← Back to all posts

03 Java Fundamentals

Variables and Data Types in Java

By Dummy for Dummies


Variables

Monkey loves bananas. Monkey collects bananas. But Monkey can only carry so many at once. To hold more, Monkey needs pockets or bags. Find a bag, and every banana goes inside it.

But bananas aren't the only thing Monkey likes. Monkey also likes mangoes and apples. So Monkey finds two more bags, one per fruit, each with its own section to keep things sorted.

The Analogy
Monkey = You (the programmer)
Fruits = Information (numbers, words, etc.)
Bags = Variables (where you store the info)

Why bother with bags at all? Small numbers like 2 + 2 are easy to type. But sometimes you're dealing with something like this:

Number
1234567890123456789

Imagine writing that everywhere in your code again and again. Painful, right? Instead, you just give it a name like number or simply n. Now, whenever you use n in your program, Java will treat it as 1234567890123456789. Easy, clean, and way more powerful when data grows big.


Types of Variables

Monkey was smart. Monkey put each fruit in a separate bag. You are smarter, you put each type of information in different variables as well. The different types or classes of variables are called "Data Types".

Consider you have 3 types of data/information:

Integer → 123
Decimal → 12.002
Word → "Coding"

Each type of data has its own class/type in Java:

123Integer (whole number)
12.002Double or Float (decimal numbers)
"Coding"String (words, text)

Common Data Types in Java

Data TypeDescriptionExample
intStores whole numbers123, -99
doubleStores decimal numbers (precise)3.14159
floatStores decimals (less precise, saves memory)2.5f
charStores a single character'A', 'x'
StringStores text (capital S)"Hello 123"
booleanStores true or falsetrue, false
longStores very big whole numbers123456789L
shortStores smaller whole numbers123
byteEven smaller whole numbers (efficiency)12

Points to Remember


Creating Variable Inside Code

When creating a variable inside a code you need to first write its datatype, then a name for it, then assign it to a value. Let's store the word Skibidi in a variable.

  1. It is a word so Datatype will be String
  2. Name can be anything but must start with a small letter
  3. Use equality = to assign the value
  4. Then write the value "Skibidi"
  5. After end of each line of code in java write semi colon ;
Java
public class Main {
    public static void main(String[] args) {
        String name = "Skibidi";
        System.out.println(name);  // This will print Skibidi
    }
}
Console Output
Skibidi

Creating All in One Code

Java
public class VariablesExample {
    public static void main(String[] args) {
        int bananas = 123;
        double price = 12.002;
        String hobby = "Coding";
        char grade = 'A';
        boolean isHappy = true;

        System.out.println(bananas);  // This will print 123
        System.out.println(price);    // This will print 12.002
        System.out.println(hobby);    // This will print Coding
        System.out.println(grade);    // This will print A
        System.out.println(isHappy);  // This will print true
    }
}
Console Output
123 12.002 Coding A true

Extra Tips

Declaration vs Initialization
If you just create a variable and do not assign any value, it is called "Declaration"
int num;

When you assign it a value later, it is called "Initialization"
num = 199;


Closing

That's it for Variables and Data types! In the next blog, Monkey won't just store fruits, Monkey will start playing with them (yes, I'm talking about operations and expressions).

← Previous 02 - Your First Java Program - Hello World Next → 04 - Operators in Java