InfoparkDailyCAREER TOOLS

Java · Lesson 2 of 9

Variables & data types

Primitive types, String, type casting and var.

Java is statically typed: every variable has a type fixed at compile time.

  • Primitives: int (whole numbers), long (big whole numbers), double (decimals), boolean (true/false), char (one character), plus byte, short, float.
  • String — text, an object (capital S), written in double quotes.
  • final makes a variable constant.
  • var lets the compiler infer the type of a local variable (Java 10+).

Converting a larger type to a smaller one needs an explicit cast: (int) 7.9 gives 7. Text to number uses Integer.parseInt("42").

Example program

Run with JDK 17+: java Main.java
public class Main {
    public static void main(String[] args) {
        String name = "Anu";
        int experienceYears = 2;
        double expectedLpa = 6.5;
        boolean isFresher = experienceYears == 0;
        final int MONTHS = 12;
        var city = "Kochi";            // inferred as String

        double monthly = expectedLpa * 100000 / MONTHS;
        System.out.println(name + " | " + city + " | fresher: " + isFresher);
        System.out.println("Monthly: " + (int) monthly);
        System.out.println("Notice days + 15 = " + (Integer.parseInt("30") + 15));
    }
}

Output

Anu | Kochi | fresher: false
Monthly: 54166
Notice days + 15 = 45

Save the code as Main.java and run java Main.java (JDK 11+ runs single files directly). Names and figures are fictional.

Key points

  • Types are fixed at compile time.
  • String is an object; primitives are not.
  • Cast down explicitly; parse text with parseInt / parseDouble.

Quick check

What is (int) 9.99?

Practice task

Store a monthly salary as an int and print the yearly salary as a long, and the average daily pay as a double.

Interview corner

Primitive vs wrapper types?

Primitives (int, double) hold raw values. Wrapper classes (Integer, Double) are objects, can be null and are needed for collections. Java converts between them automatically (autoboxing).

Why is String immutable?

So strings can be shared safely (string pool), used as map keys, and passed between threads without copying.

Further reading

Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.

Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.