Introduction to Java

Java is a statically typed, object oriented language that compiles to bytecode and runs on a virtual machine, which is what makes it portable.

What Java is

Java is a general purpose programming language that is statically typed, class based and object oriented. Source code is not translated straight into instructions for one particular processor. It is compiled into an intermediate form called bytecode, and a program called the Java Virtual Machine executes that bytecode on whatever machine it happens to be installed on.

That single design decision explains most of what people associate with Java: portability, a managed memory model, and a platform that many languages other than Java now target.

Why the language exists in this shape

  • Portability. One compiled artefact runs anywhere a compatible JVM exists, without recompilation.
  • Safety. There is no direct pointer arithmetic, array bounds are checked, and memory is reclaimed automatically.
  • Readability over cleverness. The language deliberately keeps a small set of ideas and applies them consistently.
  • Stability. Code compiled many years ago still runs, because backward compatibility is treated as a hard requirement.

How a Java program actually runs

Hello.java   -- the source you write
    |  javac (compiler)
    v
Hello.class  -- bytecode, platform independent
    |  java (launcher starts the JVM)
    v
JVM: verify -> load -> interpret, then JIT compile the hot code
    |
    v
native machine instructions

The JVM starts by interpreting bytecode. Methods that run often are detected at runtime and compiled to native code by the just in time compiler, which is why a long running Java process usually gets faster after it warms up.

JDK, JRE and JVM

TermWhat it isYou need it to
JVMThe engine that loads, verifies and executes bytecode.Run bytecode.
JREThe JVM plus the standard class library.Run a Java application.
JDKThe JRE plus development tools such as javac, jar and javadoc.Write and compile Java.
Since Java 11 the separate JRE download was discontinued. You install a JDK and, if a trimmed runtime is needed for shipping, you build one with jlink.

Editions

  • Java SE - the standard edition. The language, the JVM and the core libraries. This is what these notes cover.
  • Jakarta EE - a set of server side specifications built on top of Java SE. Mentioned only so the name is familiar.
  • Java ME - a much reduced edition for constrained devices, rarely encountered today.

Versions and the release cadence

Since Java 9 a feature release arrives every six months. Some of those are designated long term support releases, which is what production systems normally target.

ReleaseStatusNotable for
Java 8LTS, legacyLambdas, streams, java.time
Java 11LTS, legacyModern HTTP client, single file source launch
Java 17LTSSealed classes, records finalised
Java 21LTSVirtual threads, pattern matching for switch
Java 25LTSCompact source files and instance main methods

These notes teach current Java. Wherever a feature depends on a version, the version is stated beside it.

Characteristics worth knowing by name

  • Statically typed - the type of every expression is known at compile time, so many errors are caught before the program runs.
  • Garbage collected - objects that are no longer reachable are reclaimed automatically.
  • Multi threaded - threads and a defined memory model are part of the platform, not an add on.
  • Not purely object oriented - the eight primitive types are not objects. That is a deliberate performance trade off.

Common misconceptions

  • Java is interpreted, therefore slow. It is interpreted at first and compiled at runtime afterwards. Steady state performance is generally close to natively compiled languages.
  • Java and JavaScript are related. They are separate languages with separate designs. The similarity is only in the name.
  • The JVM only runs Java. Any language that emits valid bytecode can run on it.

Practice

  1. Explain in two sentences why the same .class file runs on two different operating systems.
  2. A colleague installs a runtime only and cannot compile. What is missing, and why?
  3. Why does a server application often report better throughput after several minutes of load?

Conclusion

Java compiles to bytecode and a virtual machine runs it. Hold on to that one sentence, because portability, the memory model, class loading and the just in time compiler all follow from it.

Useful resources

Hand picked references for this topic
Written by Lorens Mishra

Default administrator account created by the installer.

Continue reading

All Java notes →

Discussion

0 comments
Sign in to join the discussion.

No comments yet. Be the first to say something.