Advanced "Java 101"

Developer Advocate

Java Team at Oracle

Java in Education

Who learned Java in
school, university, etc.?

🖐🏾

Java in Education

Java became a cornerstone in CS education because:

  • the runtime simplified coding (e.g. GC)

  • explicit types and concepts

  • well-suited for OOP

  • widely used

Java as a Hobby

Kids/students/grown-ups learn Java for their hobbies:

  • home automation

  • static website generation

  • game modding

games

Java as a Second Language

Developers move from other languages to Java:

  • frontend developers

  • ML/AI developers

  • developers from other ecosystems

Java for Beginners

All these learners benefit from:

  • a simple, straightforward language

  • a smooth standard library

  • a powerful runtime

  • a well-trodden path

Java in Business

Java is successful in business, science, etc. because:

  • multi-paradigm language

  • refined programming model

  • comprehensive standard library

  • powerful runtime

  • backwards-compatible evolution

  • detailed, varied tools

  • strong, diverse ecosystem

Tension

There’s a tension here:

  • education benefits from a simple, straightforward Java

  • professional use benefits from a detailed, diverse Java

To stay relevant in education, Java must have an on-ramp.

On-Ramp

An on-ramp to Java:

  • small starting set of language constructs,
    APIs, and tools

  • features that allow quick achievements

  • gradual expansion to the full set

Today

  • teaching the language

  • teaching the tools

  • teaching the concepts

  • teaching Java

Notes

  • I’m no teacher/professor

  • this is inspiration, not instruction

  • slides at slides.nipafx.dev/teaching-java
    (hit "?" to get navigation help)

  • ask questions at any time

Teaching the Language

Teaching the Language
Teaching the Tools
Teaching the Concepts
Teaching Java

Language Constructs

Minimal Java code:

import java.util.List;
public class Prog {
	public static void main(String[] args) {
		System.out.println(List.of("Hello", "World!"));
	}
}
  • imports & visibility

  • classes & methods

  • static vs instance

  • returns & parameters

  • statements & arguments

Simplification

Beginners should be able to:

  • start with a minimal set of language constructs

  • achieve first results quickly

  • learn as they go without "delearning"

Simplification

Remove requirement of:

  • String[] args parameter

  • main being static

  • main being public

  • the class itself

  • System.out (use IO instead)

  • imports for java.base

Simpler Code

Previews in Java 23 (JEP 477):

// all the code in Prog.java
void main() {
	println(List.of("Hello", "World!"));
}
  • imports & visibility

  • classes & methods

  • static vs instance

  • returns & parameters

  • statements & arguments

Progression

Natural progression:

  • start with main()

  • add String[] args for arguments

  • add methods to organize code

  • add fields to share state

  • explore JDK APIs for more functionality

Ramp up to full language.

Teaching the Tools

Teaching the Language
Teaching the Tools
Teaching the Concepts
Teaching Java

Starting (with) Java

To write and run a simple Java program, you need:

  • a JDK

  • an editor (IDE?)

  • javac (build tool? IDE?)

  • java (IDE?)

Simplification

Beginners should be able to run
simple Java programs with
no/fewer/simpler tools!

The Java Playground

Oracle’s Java Platform Group developed the Java Playground:

  • all you need:

    • tools: browser

    • concepts: statements & arguments

  • but:

    • limited capabilities
      (e.g. no file I/O or network)

    • no persistence

jshell

Java 9 added jshell:

  • all you need:

    • tools: JDK, jshell

    • concepts: statements & arguments

  • but:

    • no syntax highlighting

    • no (smooth) persistence

    • no progression

IDE vs Editor

An IDE:

  • makes a few things easier

  • is itself a complex tool

  • is almost certainly unknown to beginners

Maybe not the best tool to introduce early.

IDE vs Editor

VS Code:

  • is better known

  • has decent Java capabilities

vs code java

Single-file Execution

Java 11 added single-file execution (JEP 330):

java Prog.java
  • tools: JDK, java

  • but: no progression

Much better for beginners, but just a section of an on-ramp.

Running Multiple Files

If you have a folder:

MyFirstJava
 ├─ Prog.java
 ├─ Helper.java
 └─ Lib
     └─ library.jar

Run with:

java -cp 'Lib/*' Prog.java

Added in Java 22 (JEP 458).

Dependencies

But where do you get dependencies from?

This is an open problem.

Progression

Natural progression:

  • start experiments in playground

  • to persist work:

    • download JDK

    • move to text editor

    • start with single source file

  • split into multiple files when code becomes larger

  • use visibility & packages to add structure

Ramp up until delivery of artifacts is needed.

Teaching the Concepts

Teaching the Language
Teaching the Tools
Teaching the Concepts
Teaching Java

Programming Concepts

Java is a great OOP language.

OOP is a great paradigm for large systems.

But is OOP the best paradigm to learn first?

Object-Oriented Programming

OOP hinges on:

  • abstraction of the domain through classes

  • combination of state and behavior

  • encapsulation and visibility

  • inheritance (at least of types)

Neither easy nor necessary for simple programs.

Simple Programs

Programming can be immediately useful:

  • automation

  • data analysis

  • visualization

  • interactive media

Let learners write simple, useful programs!

⇝ Such programs rarely need OOP.

Terminal Interaction

Simple programs usually interact via terminal.

Use IO instead of System.{in|out}:

  • void print(Object) & void println(Object) to write

  • String readln(String) to read input

(Auto-imported in a simple source file.)

Focusing on Data

Simple programs often focus on data:

  • reading data from disk or remote

  • parsing various data formats

  • representation of data as types

  • mass-processing data

  • visualizing data

  • writing data to disk or a remote

Reading Local Data

NIO.2 makes reading text files straightforward:

var transactions = Path.of("transactions.csv");
for (var line : Files.readAllLines(transactions)) {
	// ...
}

Reading Remote Data

HTTP/2 client makes reading remote data
(relatively) straightforward:

var client = HttpClient.newHttpClient();
var request = HttpRequest
		.newBuilder(URI.create("http://example.org/"))
		.build();
var body = client
		.send(request, BodyHandlers.ofString())
		.body();

Parsing Data

Java has out-of-the-box support for:

  • XML

😬

Clearly room for improvement!

Representing Data

Records make simple types simple:

record Transaction(
	LocalDate date, long cents,
	TransactionType type) { }

enum TransactionType { PRIVATE, BUSINESS }

Processing Data

for-loops, if statements, etc. are simplest.

But once functions are introduced, streams become available.

Visualizing Data

JavaFX is powerful but not simple to get started with.

Java lacks beginner-level visualization tools.

Writing Data

NIO.2 makes writing data very simple:

var analysis = "...";
var file = Path.of("analysis.txt");
Files.writeString(file, analysis);

Progression

Natural progression:

  • start with simple terminal interaction

  • use NIO.2 to read and write local data

  • use HTTP/2 client to access remote data

  • use records to model data

  • use common statements to analyze data

  • potentially upgrade to streams

Go into more OOP as problems get more complex.

Teaching Java

Teaching the Language
Teaching the Tools
Teaching the Concepts
Teaching Java

Starting Point

Take first steps in the playground.

To get started "for real":

  • tools: JDK, text editor

  • code: simple main and IO

  • execution with java Prog.java

Progression

  • need arguments? ⇝ add String[] args

  • need to organize code? ⇝ add methods

  • need more functionality? ⇝ explore JDK APIs:
    e.g. NIO.2 or HTTP/2 client

  • need to capture data? ⇝ define records

  • need shared state? ⇝ add fields

  • need even more functionality? ⇝ explore simple libraries:
    e.g. JavaFX for visualization

  • need more structure? ⇝ split into multiple files

  • even more ⇝ use visibility & packages

Progression

Java can do better is some areas:

  • managing dependencies without build tool

  • steering away from outdated APIs

  • support more data formats out of the box

  • simplify (data) visualization

Progression

At some point:

  • a more powerful editor would be helpful ⇝ IDE

  • testing or delivering becomes necessary ⇝ build tool

Welcome to full Java!

So long…​

37% off with
code fccparlog

bit.ly/the-jms

More

Slides at slides.nipafx.dev
⇜ Get my book!

Follow Nicolai

nipafx.dev
/nipafx

Follow Java

inside.java // dev.java
/java    //    /openjdk

Image Credits