Back to Java Roadmap

Language Basics - Variables, Types, Operators

Learn Java variable declaration, primitive vs wrapper types, safe casting, and arithmetic/logical operators with practical examples.

#java #language-basics #types #operators

Why this step matters

Most Java bugs in beginner code come from weak type understanding and operator misuse. If you master variables, types, and operators early, your code becomes safer, clearer, and easier to debug.

Variables in Java

A variable is a named container for a value. In Java, every variable has a type known at compile time.

Declaration and initialization

int age = 28;
String name = "Briac";
boolean active = true;

You can declare first, assign later:

double price;
price = 19.99;

final variables (constants)

Use final when a value should not change:

final double VAT_RATE = 0.20;

A final variable must be assigned once and cannot be reassigned.

Primitive types vs wrapper classes

Java has 8 primitive types. Wrappers are object representations of primitives and are useful in collections, generics, and APIs expecting objects.

PrimitiveWrapperTypical use
byteBytevery small integers
shortShortsmall integers
intIntegerdefault integer choice
longLonglarge integer values
floatFloatdecimal values (less precision)
doubleDoubledefault decimal choice
charCharactersingle Unicode character
booleanBooleantrue/false flags

Key differences

  • Primitive: faster, no null, stored as raw value
  • Wrapper: object, can be null, useful with generic types (List<Integer>)

Autoboxing and unboxing

Java can convert automatically:

Integer boxed = 10;   // autoboxing (int -> Integer)
int unboxed = boxed;  // unboxing (Integer -> int)

Be careful with null wrappers:

Integer count = null;
// int n = count; // throws NullPointerException at runtime

Type casting

Type casting means converting one type to another.

Widening cast (implicit, safe)

From smaller to larger compatible type:

int i = 42;
long l = i;      // implicit
double d = l;    // implicit

Narrowing cast (explicit, risk of loss)

From larger to smaller type requires explicit cast:

double d = 9.75;
int i = (int) d; // 9 (decimal part lost)

Overflow example:

int value = 130;
byte b = (byte) value; // -126 (overflow)

Best practice

  • Prefer widening conversions when possible
  • Use narrowing casts only when intentional
  • Validate bounds before narrowing

Arithmetic operators

Main arithmetic operators:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % remainder
int a = 10;
int b = 3;
System.out.println(a / b); // 3 (integer division)
System.out.println(a % b); // 1

Use double when you need decimal division:

double result = 10.0 / 3;
System.out.println(result); // 3.333...

Increment and decrement

int x = 5;
x++; // x = 6
x--; // x = 5

Relational and logical operators

Relational operators compare values:

  • ==, !=, >, <, >=, <=

Logical operators combine boolean expressions:

  • && AND
  • || OR
  • ! NOT
int age = 20;
boolean hasTicket = true;

boolean canEnter = age >= 18 && hasTicket;

Short-circuit behavior

&& and || stop evaluation early when result is already known. This is useful for null-safe checks:

String email = null;
if (email != null && email.contains("@")) {
    System.out.println("Valid email format");
}

Common mistakes to avoid

  • Using == for String content comparison (use .equals())
  • Ignoring integer division behavior
  • Casting without checking value range
  • Unboxing nullable wrappers

Mini practice examples

Example 1: safe average

double sum = 17;
int count = 4;
double avg = sum / count; // 4.25

Example 2: eligibility rule

int age = 17;
boolean parentalConsent = true;
boolean allowed = age >= 18 || parentalConsent;

Takeaway

At this stage, focus on three rules:

  1. Choose the right type first
  2. Cast explicitly and carefully
  3. Keep boolean conditions readable

These basics are the foundation for control flow, methods, and object-oriented code.