Java Variables — Free Java Tutorial
Learn Java Variables in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
TL;DR: Learn Java Variables in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated
Java Variables in Java
Java is a statically-typed language — every variable must have a declared type. Java has two categories of data types: primitive types and reference types.
Primitive types store actual values directly in memory. There are 8 primitive types: byte, short, int, long, float, double, boolean, and char.
Reference types store a reference (memory address) to an object. Strings, arrays, and all class instances are reference types.
The most commonly used primitives are: `int` (whole numbers), `double` (decimal numbers), `boolean` (true/false), and `char` (single character).
Java also provides wrapper classes (Integer, Double, Boolean, Character) for every primitive — these are objects and provide useful utility methods.
Type casting allows you to convert between types. Widening (int → double) happens automatically; narrowing (double → int) requires explicit cast with `(type)`.
Java Variables — Syntax
// Primitive types int myInt = 10; double myDouble = 9.99; boolean myBool = true; char myChar = 'A'; long myLong = 15000000000L; float myFloat = 5.75f; // Reference type String myString = "Hello"; // Type casting double d = 9.7; int i = (int) d; // narrowing: i = 9 (truncated)
Learn Java Variables step by step with Syllab's free interactive Java tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full Java course →