Variables and types

Primitive types, String, var, casting, and converting text to numbers.

Runs in Zerethon IDE · Java 25 Official Java documentation ↗
Main.java
public class Main {
    public static void main(String[] args) {
        int count = 3;
        double price = 9.5;
        long big = 3_000_000_000L;
        boolean ready = true;
        char grade = 'A';
        String name = "Ada";
        var total = count * price; // var: the type (double) is inferred
        final int MAX = 10;        // cannot be reassigned

        System.out.println(count + " " + price + " " + big + " " + ready + " " + grade + " " + name);
        System.out.println(total + " " + MAX);
        System.out.println(7 / 2 + " " + 7 / 2.0 + " " + 7 % 2 + " " + (int) 3.99);
        System.out.println(Integer.parseInt("42") + 1 + " " + Double.parseDouble("2.5") * 2 + " " + String.valueOf(7) + 7);
    }
}

Output

3 9.5 3000000000 true A Ada
28.5 10
3 3.5 1 3
43 5.0 77
  • int / int is whole-number division (7 / 2 is 3); make one side a double to get 3.5.
  • int holds about ±2.1 billion; use long (with an L suffix on literals) for more.
  • Compare strings with a.equals(b), never a == b (that compares object identity).