Java Tutorial

Java Data Types

int, double, char, boolean, and String each store a different kind of value. Picking the right one is the first correctness decision you make in any program.

The primitive types

Java has eight built-in primitive types. In everyday code you reach for four of them:int, double, boolean, and char. The rest matter when size or precision does.

TypeHoldsExample
intWhole numbers (±2.1 billion)int n = 7;
longBig whole numberslong big = 9000000000L;
doubleDecimal numbersdouble x = 3.5;
booleantrue or falseboolean ok = true;
charOne character, single quoteschar g = 'B';

Example

public class Main {
  public static void main(String[] args) {
    int n = 7;
    double x = 3.5;
    char grade = 'B';
    boolean ok = true;
    System.out.println(n + " " + x + " " + grade + " " + ok);
  }
}

A long literal needs an L suffix (9000000000L); a plain9000000000 is read as an int and overflows before it is even assigned.

String is a class, not a primitive

String holds text in double quotes. It is a full object with methods like length()and toUpperCase(). Join strings with +.

Example

public class Main {
  public static void main(String[] args) {
    String name = "Ada";
    System.out.println(name.length());        // 3
    System.out.println(name.toUpperCase());   // ADA
  }
}

The number-one Java beginner trap: compare strings with .equals(), not==. == asks "are these the same object in memory?", which is often false even when the text matches. a.equals(b) asks "is the text the same?" — that's almost always what you want.

Example

public class Main {
  public static void main(String[] args) {
    String a = "hi";
    String b = new String("hi");
    System.out.println(a == b);        // false  (different objects)
    System.out.println(a.equals(b));   // true   (same text)
  }
}

Integer division and casting

Dividing two ints throws the fraction away — 17 / 5 is 3, not3.4. Make one side a double to keep the decimals. Going the other way,(int) drops the fraction on purpose.

Example

public class Main {
  public static void main(String[] args) {
    System.out.println(17 / 5);      // 3    (int division truncates)
    System.out.println(17 / 5.0);    // 3.4  (one double keeps decimals)
    System.out.println((int) 9.8);   // 9    (cast drops the fraction)
  }
}

This bites in averages: (a + b) / 2 with two ints truncates. Write (a + b) / 2.0when you want the real average.

Wrapper types and overflow

Each primitive has a matching object type — Integer for int, Double fordouble — used when you need objects (for example inside an ArrayList). They also carry helpers like Integer.parseInt("42") to turn text into a number.

An int tops out near 2.1 billion. Add past that and it silently wraps to a negative number (overflow) — no error. Reach for long when a value could get large, such as a running total or a factorial.

Try It Yourself

Exercise: Predict the output, then explain the surprise.

System.out.println(5 / 2);
System.out.println(5 / 2.0);
Show solution

The first prints 2 — both operands are int, so the division truncates. The second prints 2.5 — the 2.0 makes it floating-point division. Same numbers, different types, different answer.

Key Takeaways

  • Use int, double, boolean, char for everyday values; long for big numbers.
  • String is a class — compare text with .equals(), never ==.
  • Int-by-int division truncates; make one side a double to keep the fraction.
  • int overflows silently past ~2.1 billion — use long when values can grow.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Maths

Integer vs double division

17 / 5 is 3 in integer arithmetic. 17 / 5.0 is 3.4. The type of the operands decides the kind of division.

Example

public class Main {
  public static void main(String[] args) {
    System.out.println(17 / 5);
    System.out.println(17 / 5.0);
  }
}

FAQ: Java Data Types

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java data types examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java data types in this Java Java lesson (Java Data Types).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.