Nearby lessons

12 of 125

Java - Data Types (8 Primitives)

📌 What You Will Learn
  • The small building blocks of Java: tokens
  • All 8 primitive data types with their ranges
  • Type casting — implicit and explicit
  • Control statements: if, switch, loops
  • Arrays — single, double and jagged
  • Variable length arguments (var-args)

Data Types (8 Primitives) is a core concept of the Java language. This lesson explains Literals with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Literals

Literals are constant values written directly in the program.

Literal typeExample
Integer10, -5, 100L (L for long)
Floating point10.5, 3.14f (f for float)
Character'A', '5', '\n'
String"Hello", "Java"
Booleantrue, false
Nullnull
Binary/Octal/Hex0b1010 (binary), 010 (octal), 0x1F (hex)
Example01
JCode Cell
1int a = 100L; // ERROR! L makes it long, cannot store in int
2long b = 100L; // OK
3float f = 3.14f; // f is needed for float
4float g = 3.14; // ERROR! 3.14 is double by default
5char c = 'A'; // single quotes for char
6String s = "A"; // double quotes for String
📝 Key Takeaways
  • Tokens are the smallest pieces of a program: identifiers, literals, keywords, operators, separators.
  • Java has 8 primitive types: byte, short, int, long, float, double, char, boolean.
  • Widening casting is automatic and safe; narrowing casting needs brackets and may lose data.
  • if / switch decide which path runs; for / while / do-while repeat code; break and continue control loops.
  • Arrays hold many same-type values; index starts at 0; size is fixed; for-each loop reads them simply.
  • Var-args (int... x) lets a method take any number of arguments.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1