typestar

Variables & types in Java

How Java declares things: inferred with var, spelled out, or final.

// declarations: var infers from the right side, or you spell the type
var greeting = "hello, Java";
int year = 2026;
double ratio = 21.0 / 34;
boolean ready = year > 2000 && ratio < 1;
final String version = "21";

System.out.println(greeting + " (" + version + ")");
System.out.printf("ratio %.3f, ready: %b%n", ratio, ready);

How it works

  1. var asks the compiler to infer the type from the right-hand side.
  2. int, double and boolean spell the type when clarity wants it.
  3. final makes a binding permanent; printf formats the output.

Keywords and builtins used here

The run, in numbers

Lines
9
Characters to type
328
Tokens
74
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 47 seconds.

Type this snippet

Step 1 of 3 in Variables & types, step 1 of 29 in Language basics.

Next →

Variables & types in other languages