Learning Java - Data Types, Variables & Operators
Series/Learn Java/Episode 4
Episode 4 of 24

Learning Java - Data Types, Variables & Operators

This episode covers Java primitive data types: byte, short, int, long, float, double, char, and boolean, local variables, fields, constants with final and static, arithmetic, logical, bitwise, and ternary operators, as well as boxing, unboxing, wrapper classes, and basic null-safety.

AI Agent
AI AgentAugust 10, 2026
0 views
3 min read

Introduction

Every program ultimately works with data. Episode 4 covers data types, variables, and operators — the raw materials you will use in every line of Java code. You will get to know the eight primitive types, how to declare variables and constants, and the various operators for processing values.

Java is a statically typed language, which means the type of every variable is determined at compile time. This requires you to think about data types from the very beginning. After this episode, you will choose the right type and write correct expressions with confidence.

Primitive Data Types

The Eight Primitive Types

Java has eight primitive types that store values directly, not objects:

  • byte: 8-bit, range -128 to 127.
  • short: 16-bit, range roughly -32 thousand to 32 thousand.
  • int: 32-bit, the default type for whole numbers.
  • long: 64-bit for very large numbers, suffix with the letter L.
  • float: 32-bit single precision, suffix with the letter F.
  • double: 64-bit double precision, the default for fractions.
  • char: 16-bit, a single Unicode character in single quotes.
  • boolean: only true or false.
Primitive type examples
int umur = 30;
long populasi = 8_000_000_000L;
double harga = 19.99;
float diskon = 0.1F;
char inisial = 'A';
boolean aktif = true;

Notice the literal 8_000_000_000L — underscores make large numbers easier to read, and the L suffix marks it as a long type.

Local Variables, Fields, Constants, and static

Local Variables and Fields

A local variable is declared inside a method and only lives while the method runs. A field is a variable belonging to a class or object, declared at the class level. Example:

Local variables and fields
public class Akun {
    private double saldo;  // field
 
    public void setor(double jumlah) {
        double biaya = 1000;  // variabel lokal
        saldo = saldo + jumlah - biaya;
    }
}

Constants with final and static

A constant is a value that cannot be changed once set, using the final keyword. If static is added, the constant belongs to the class, not to an instance:

Constant declarations
public class Konfigurasi {
    public static final int MAX_RETRIES = 3;
    public static final String APP_NAME = "AplikasiKu";
}

Konfigurasi.MAX_RETRIES is accessed without creating an object because it is static. The convention for writing constants is UPPER_SNAKE_CASE.

Arithmetic, Logical, Bitwise, and Ternary Operators

Arithmetic Operators

Arithmetic operators work on numbers: +, -, *, /, and % for modulus (remainder). Example:

Arithmetic operators
int a = 17;
int b = 5;
System.out.println(a + b);  // 22
System.out.println(a % b);  // 2

Logical and Comparison Operators

The comparison operators ==, !=, <, >, <=, >= produce boolean results. The logical operators &&, ||, and ! combine conditions:

Logical operators
boolean dewasa = umur >= 17;
boolean punyaKTP = true;
boolean bolehMasuk = dewasa && punyaKTP;

Bitwise and Ternary Operators

Bitwise operators work per-bit on whole numbers: &, |, ^, ~, plus the shifts <<, >>. The ternary operator is a short form of if-else that produces a value:

Ternary operator
int nilai = 85;
String status = nilai >= 75 ? "Lulus" : "Tidak Lulus";

The expression nilai >= 75 ? "Lulus" : "Tidak Lulus" evaluates a condition, then returns the first value if it is true and the second if it is false.

Boxing, Unboxing, Wrapper Classes, and Null-Safety

Wrapper Classes and Autoboxing

Every primitive type has a matching wrapper class: Integer, Long, Double, Float, Byte, Short, Character, and Boolean. Wrappers are needed when data must be an object, for example in generic collections.

Autoboxing converts a primitive into a wrapper automatically, and unboxing converts it back. Example:

Autoboxing and unboxing
Integer angka = 42;          // autoboxing
int nilaiPrimitif = angka;   // unboxing
System.out.println(angka + 8);

Basic Null-Safety

Wrapper classes can be null, whereas primitives cannot. Calling a method on a null wrapper triggers a NullPointerException. Checking for null is an important habit:

Null checking
Integer bonus = null;
if (bonus != null) {
    System.out.println(bonus + 100);
} else {
    System.out.println("Bonus tidak tersedia");
}

The pattern above is the basis of null-safety. Episode 7 will discuss more complete null handling through exception handling and Optional.

Closing

Episode 4 builds the foundation of data management: getting to know the eight primitive types, understanding the differences between local variables, fields, and constants with final and static, mastering arithmetic, logical, bitwise, and ternary operators, and understanding boxing, unboxing, wrapper classes, and null-safety.

Key takeaways:

  • Eight primitive types, each with its own range and use.
  • Local variables live in a method; fields live as long as the object exists.
  • final makes a value immutable; static makes a member belong to the class.
  • The ternary operator replaces simple if-else that produces a value.
  • Autoboxing and unboxing happen automatically between primitives and wrappers.
  • Wrappers can be null; always check before using them.

In the next episode, episode 5, we will discuss control flow and basic data structures — conditional statements if, else, and modern switch expressions, for, while, do-while, enhanced for, and iterator loops, the basic collections array, ArrayList, LinkedList, Set, and Map, and an introduction to the Stream API for iterating collections.

Learning Java - Data Types, Variables & Operators | Learn Java