Core Java. Lecture #1

Overview of the history, features, and facilities of Java ecosystem

Ivan Ponomarev, Synthesized.io/MIPT

me
  • MIPT alumni 2003, PhD

  • ERP systems & Java background

  • Speaker at conferences

  • Heisenbug Program Committee Member

How to get in touch with me?

Course structure

  • Lectures

  • Labs

  • Homework

  • Labs and homework marks will define your final mark

Useful books

horstmann
horstmann2
urma
  • Cay S. Horstmann. Core Java vol 1 & 2

  • Raoul-Gabriel Urma et al. Modern Java in Action

Useful books*

bloch
goetz
  • Joshua Bloch Effective Java

  • Brian Goetz et al. Java Concurrency in Practice

TIOBE Index (https://www.tiobe.com/tiobe-index/)

  • number of search engine results for queries containing the name of the language

  • covers searches in Google, Google Blogs, MSN, Yahoo!, Baidu, Wikipedia and YouTube

  • reflects the number of skilled engineers, courses and jobs worldwide

RedMonk

  • GitHub & StackOverflow

Three Decades of Java: Before Java 9

JDK 1.0 (January 23, 1996)
JDK 1.1 (February 19, 1997)
J2SE 1.2 (December 8, 1998)
J2SE 1.3 (May 8, 2000)
J2SE 1.4 (February 6, 2002)
J2SE 5.0 (September 30, 2004)   <--- Scala: early 2004
Java SE 6 (December 11, 2006)
-- Sun acquisition by Oracle--  <--- Groovy 1.0: January 2, 2007
Java SE 7 (July 28, 2011)
Java SE 8 (March 18, 2014)      <--- Kotlin 1.0: February 15, 2016
Java SE 9 (September 21, 2017)

(source: Wikipedia)

Three Decades of Java: After Java 9

6-monthly release cadence

10     (March 20, 2018)
11 LTS (September 25, 2018)
12     (March 19, 2019)
13     (September 17, 2019)
14     (March 17, 2020)
15     (September 15, 2020)
16     (March 16, 2021)
17 LTS (September 14, 2021)
18     (March 22, 2022)
19     (September 20, 2022)
20     (March 21, 2023)
21 LTS (September 19, 2023)
22     (March 19, 2024)
23     (due to in September 2024)
24     (due to in March 2025)
25 LTS (due to in September 2025)

(source: Wikipedia)

Java origins

gosling lotr
  • Language for set-top boxes

  • Inspired by C++

  • WORA

  • There were other things, but WORA was most important

Q&A with James Gosling, June 2020:
https://www.youtube.com/watch?v=XThhlihqTXI

Question

How would you implement WORA?

  • Endianness…​

  • Register size…​

Continued

  • Applets

  • Built-in security

  • But security turned out to be a tricky thing

  • Applets died (JavaScript won)

Nowadays

  • Desktop (?)

  • Embedded Systems

  • Server-side (backend)

  • Mobile Devices (Android) — a separate story (see Google vs. Oracle case)

Security and safety in design-time and in run-time

rtvscompiletime

Virtual machine

WORA + two dependant tasks:

  • Safety

  • No fatal faults on server

Byte code generation and execution in a safe environment

Question

int i = 2;
i = ++i + ++i + ++i;

Java — 12,
C — undefined behaviour
(https://stackoverflow.com/questions/3879176/i-i-i-in-java-vs-c)

Runtime errors are not catastrophic

  • NullPointerException

  • ArrayIndexOutOfBoundsException

Java Virtual Machines

Reference implementation: Oracle HotSpot

~ 20 implementations

Virtual machine and platform services

  • Byte code, interpretation and JIT-compilation.

  • Garbage Collection

  • MultiThreading & Memory Model

  • Loading and running code in runtime

  • Reflection API

  • Standard library

Byte code verification

  • There are no operand stack overflows or underflows.

  • All local variable uses and stores are valid.

  • The arguments to all the Java Virtual Machine instructions are of valid types.

Interpretation and JIT compilation

java bytecode exec

On-the-fly optimization

codecache

Garbage Collection

  • The developer does not need to worry about freeing up memory

  • Garbage collection when there are no references to the object

  • Advantages:

    • Less code

    • There are no invalid links (to destroyed objects)

    • Fewer memory leaks

  • Disadvantages:

    • We do not control the operating time of the GC

    • STW-pauses (sometimes at the inappropriate time)

How can GC be implemented?

  • Reference counting — cycles?

  • Mark & sweep

  • Generational hypothesis

Generational Hypothesis

generations

GC Types

  • Serial (for single-threaded, client environments)

  • Parallel (default)

  • CMS (Concurrent mark and Sweep — shorter pauses, share processor resources, deprecated in Java 9)

  • G1 (Garbage First — big heaps)

  • Shenandoah (submillisecond pauses)

  • Epsilon (no GC)

Latency vs. Throughput tradeoff

seesaw

Multithreading

  • The ability to run parallel threads of execution is built into the language from the first version (Thread, Runnable etc)

  • JDK 1.5: significant improvements (executor, semaphore, mutex, barrier, latches, concurrent collections, blocking queues)

  • JDK 1.7: ForkJoinPool (work-stealing technique)

  • JDK 1.8: CompletableFuture

  • Java SE 21: Project Loom: virtual threads

Memory model

  • Memory Model: Part of the VM specification: a model of how the memory works

  • Answers the question: "what values can we possibly seen when we read a variable?"

When do we need a memory model?

class DumbWayToFallAsleep implements Runnable {
  private boolean asleep;

  public void setAsleep(boolean asleep){
    this.asleep = asleep;
  }

  @Override
  public void run() {
    while (!asleep){
      //countSomeSheep
      //WILL WE FALL ASLEEP?
    }
  }
}

When do we need a memory model?

class NotSoDumbWayToFallAsleep implements Runnable {
  private volatile boolean asleep;

  public void setAsleep(boolean asleep){
    this.asleep = asleep;
  }

  @Override
  public void run() {
    while (!asleep){
      //countSomeSheep
      //...
    }
  }
}

Loading and executing code in the runtime

  • Compiled class bytecode can be delivered to any system in the runtime (most often by saving a file to the file system).

  • Recompilation/linking is not required.

Built-in Serialization

  • An idea that seemed very good at first!

Reflection API

  • Explore information about classes, fields, methods, etc. at run time

  • Read/modify fields (including private!) and dynamically call methods (including private!)

  • Proxy and AOP

Continuously Improving Standard Library

  • System-independent part

    • Data structures

    • Mathematics

  • System-dependent part

    • Date and time

    • File system access

    • Network access

    • User Interface

Backward compatibility

  • Any .jar file compiled many years ago can be run on any modern JVM

  • A gift or a curse?

Criticism of the Java platform and language

  • Poor performance

  • Memory consumption

  • Slow startup

  • Verbose (too much "boilerplate code")

  • The language is conservative and is developing slowly

Criticism of the Java platform and language

  • Poor performance — no

  • Memory consumption — no

  • Slow startup — partially es

  • Verbose (too much "boilerplate code") — that’s true, but Lombok and modern Java versions fix this

  • The language is conservative and is developing slowly — nowadays, no

Language development since version 9 (6-months release cadence)

  • type inference,

  • switch expressions,

  • sealed classes,

  • pattern matching,

  • records,

  • unnamed main classes,

  • string templates (to be dropped?)…​

Current projects

  • Project Amber: productivity-oriented features

  • Project Loom: fibers & continuators

  • Project Valhalla: value types & generic specialization

  • Graal, Panama, Kulla, Sumatra…​

Battle for new features

  • Alexey Shipilev, 2015: The Lord of the Strings: Two Scours

  • Tagir Valeev, 2018: Pattern matching and his imaginary friends

  • Sergey Kuksenko, 2020: Valhalla is coming

  • Sergey Kuksenko, 2020: Java threads are losing weight, performance review of Project Loom

pirates

Variety of JVM languages

Essential elements of the ecosystem (beyond the standard library)

  • Libraries and frameworks (including testing)

  • Integrated Development Environments (IDEs)

  • Build systems

  • CI

JUnit

unit testing and test automation

junit5

Spring Framework

DI container and application framework

springframework

Integrated Development Environments

IntelliJ IDEA Logo

IDEA

(proprietary, JetBrains)

Apache NetBeans Logo

NetBeans

(ASF / Oracle)

Eclipse Luna Logo

Eclipse Foundation

vscode

VS Code

(Microsoft)

Build systems

mavenlogo

XML configuration
Maven project layout
Maven Central
plugins
declarative

gradlelogo

Groovy DSL / Kotlin DSL
plugins
scripts
mixed declarative/imperative

Java community

  • JUGs (in big cities)

  • Conferences (Joker/JPoint/SnowOne, DevoXX etc.)

duke

Oracle certification exams

OCA/OCP (search for "Oracle Java Certification")

oca

Hello, world!

Build and run the jar file