The easiest way to start programming in Java
Java 25 makes beginner programs much easier to write than older versions of Java.
Two features are especially useful when you are learning:
- Compact source files let a small Java program omit the class declaration and the traditional
public static void main(String[] args)boilerplate. - The new
java.lang.IOclass provides simplereadln(),print()andprintln()methods for console input and output.
That means a beginner can focus immediately on variables, decisions, loops and user input instead of first learning about classes, static methods, arrays, Scanner and input streams.
Your first Java 25 Hello World program
A complete Java 25 Hello World program can now be this small:
void main() {
IO.println("Hello, World!");
}That's a complete launchable Java program. There is no explicit class declaration, no public, no static and no String[] args.
Save it in a file such as HelloWorld.java and run it with Java 25:
java HelloWorld.javaJava's source-file launcher compiles the file in memory and runs its main() method.
Read input with Java 25 IO.readln()
Older beginner tutorials often use Scanner to read keyboard input:
Scanner input = new Scanner(System.in);
String name = input.nextLine();That works, but it introduces several concepts before a beginner needs them.
Java 25's IO.readln() makes line-oriented input much simpler:
void main() {
String name =
IO.readln("What is your name? ");
IO.println("Hello, " + name + "!");
}The prompt is displayed, the user types a line of text, and readln() returns that text as a String.
Build a Java number guessing game
Now let's build something more interesting than Hello World.
Our number guessing game will teach four fundamental programming concepts:
- Variables
- User input
- Conditional logic
- Loops
Step 1: Create the variables
The game needs a secret number and a value for the player's guess:
void main() {
var magicNumber = 7;
var guess = 0;
}The var keyword asks the Java compiler to infer each local variable's type. Because both values are initialized with whole numbers, Java infers both variables to be int.
Step 2: Ask the player for a guess
IO.readln() always returns text. Because the game needs an integer, convert the returned String with Integer.parseInt():
String input =
IO.readln("Guess the number: ");
int guess =
Integer.parseInt(input);The same operation can be written more compactly:
int guess = Integer.parseInt(
IO.readln("Guess the number: ")
);Step 3: Add if-else logic
The program should tell the player whether the guess is too low, too high or correct:
if (guess < magicNumber) {
IO.println(guess + " is too low!");
} else if (guess > magicNumber) {
IO.println(guess + " is too high!");
} else {
IO.println(guess + " is correct!");
}The three branches are mutually exclusive. Exactly one will run for each guess.
Step 4: Add a while loop
The game should continue until the user finds the correct number.
while (guess != magicNumber) {
// Ask for another guess.
}The != operator means "not equal to." The loop continues while the guess and the secret number are different.
The complete Java 25 guessing game
Put the variables, input, conditional logic and loop together and the complete program remains surprisingly small:
void main() {
var magicNumber = 7;
var guess = 0;
IO.println("Guess the magic number!");
while (guess != magicNumber) {
guess = Integer.parseInt(
IO.readln("Enter your guess: ")
);
if (guess < magicNumber) {
IO.println(guess + " is too low!");
} else if (guess > magicNumber) {
IO.println(guess + " is too high!");
} else {
IO.println(guess + " is correct!");
}
}
}If the player enters 5, 9 and 7, the interaction looks like this:
Guess the magic number!
Enter your guess: 5
5 is too low!
Enter your guess: 9
9 is too high!
Enter your guess: 7
7 is correct!Add input validation
There is still one weakness in the program. If the user types hello instead of a number, Integer.parseInt() throws a NumberFormatException.
A slightly more advanced version can catch that error:
void main() {
var magicNumber = 7;
var guess = 0;
while (guess != magicNumber) {
String input =
IO.readln("Enter your guess: ");
try {
guess = Integer.parseInt(input);
} catch (NumberFormatException e) {
IO.println(
"Please enter a whole number."
);
continue;
}
if (guess < magicNumber) {
IO.println("Too low!");
} else if (guess > magicNumber) {
IO.println("Too high!");
} else {
IO.println("Correct!");
}
}
}This introduces two more Java concepts, exceptions and continue, but the core input code remains simple.
Java 25 IO vs. Scanner
| Task | Java 25 IO | Scanner |
|---|---|---|
| Read a line | IO.readln() |
scanner.nextLine() |
| Display a prompt and read | IO.readln("Guess: ") |
Separate print and scan calls |
| Print a line | IO.println(value) |
System.out.println(value) |
| Parse an integer | Integer.parseInt(IO.readln()) |
scanner.nextInt() |
| Beginner boilerplate | Very little | More concepts required |
Scanner remains useful when an application needs tokenization and direct parsing of several primitive types. But for a beginner program that simply reads one line at a time, Java 25's IO API is much easier to teach.
What happened to public static void main?
Java 25 did not remove traditional Java classes or the classic main method. This is still perfectly valid:
public class NumberGuessingGame {
public static void main(String[] args) {
IO.println("Hello from traditional Java!");
}
}The compact form simply lets beginners postpone those concepts until they become useful.
As programs grow, developers can naturally move from:
void main() {
IO.println("Hello!");
}to classes, packages, constructors, methods, interfaces, collections and the rest of the Java platform.
What should you learn next?
Once the guessing game works, try changing it rather than immediately moving to another tutorial.
- Generate the magic number with
Random. - Limit the player to five guesses.
- Keep score.
- Ask whether the player wants to play again.
- Move repeated logic into methods.
- Eventually convert the compact program into a normal Java class.
With fewer than 25 lines of meaningful code, you've already learned variables, local type inference, console input and output, numeric conversion, conditionals and loops.
That is exactly what Java 25's compact source files and java.lang.IO are designed to do: let new programmers learn programming concepts first and introduce Java's larger application structure when they are ready for it.