HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaVariablesData TypesOperatorsStringsArraysLists (ArrayList)Conditions (if / switch)Loops (for / while)MethodsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
Typed variablefinal (constant)var (type inference)
🔢 Numbers & Math
✍️ String
📋 Array & List
🔁 Condition & Loop
🧩 Method & Class
Java›REFERENCE›Variables
📦

Variables

Typed variables and constants.

CommandDescription
Typed variableVariable with a specified type.
final (constant)Constant value.
var (type inference)Infers the type automatically (Java 10+).

Typed variable

Try it yourself →

Variable with a specified type.

Syntax

type name = value;

Description

Java is statically typed: the type of every variable is written. int, double, boolean, String are the most common.

Example

public class Main {
    public static void main(String[] args) {
        int age = 36;
        String name = "Alex";
        System.out.println(name + " " + age);
    }
}

Expected output

Alex 36

When to use?

Storing all kinds of data.

Common mistakes

  • ⚠️ Assigning the wrong type causes a compilation error: int x = "5"; → ERROR.

final (constant)

Try it yourself →

Constant value.

Syntax

final type NAME = value;

Description

A value defined with final cannot be changed again (constant).

Example

public class Main {
    public static void main(String[] args) {
        final double PI = 3.14;
        System.out.println(PI);
    }
}

Expected output

3.14

When to use?

Constants.

var (type inference)

Try it yourself →

Infers the type automatically (Java 10+).

Syntax

var name = value;

Description

Type is understood from the value; shortens the syntax for local variables.

Example

public class Main {
    public static void main(String[] args) {
        var number = 42;
        System.out.println(number);
    }
}

Expected output

42

When to use?

Short local variable definition.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code