Core Java. Lecture #1

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

Ivan Ponomarev, Synthesized.io/MIPT

me
  • MIPT alumni 2003, PhD

  • Staff engineer at Synthesized.io

  • 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

  • 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

tiobe

RedMonk Index

  • GitHub & StackOverflow

redmonk2022

27 years of Java

JDK 1.0 (January 23, 1996)      |  -- 6-monthly release cadence --
JDK 1.1 (February 19, 1997)     |  Java SE 10     (March 20, 2018)
J2SE 1.2 (December 8, 1998)     |  Java SE 11 LTS (September 25, 2018)
J2SE 1.3 (May 8, 2000)          |  Java SE 12     (March 19, 2019)
J2SE 1.4 (February 6, 2002)     |  Java SE 13     (September 17, 2019)
J2SE 5.0 (September 30, 2004)   |  Java SE 14     (March 17, 2020)
Java SE 6 (December 11, 2006)   |  Java SE 15     (September 15, 2020)
-- Sun acquisition by Oracle--  |  Java SE 16     (March 16, 2021)
Java SE 7 (July 28, 2011)       |  Java SE 17 LTS (September 14, 2021)
Java SE 8 (March 18, 2014)      |  Java SE 18     (March 22, 2022)
Java SE 9 (September 21, 2017)  |  Java SE 19     (September 20, 2022)
                                |  Java SE 20     (due to in March 2023)
                                |  Java SE 21 LTS (due to in September 2023)

6-monthly Release Cadence

6 monthly

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

  • Future: Project Loom: lightweight user-mode threads (fibers)

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

Standard Library

lib evolution

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?

javasirs

Language development since Java 8

  • lambdas & functional programming,

  • modules,

  • compact strings,

  • type inference,

  • switch expressions,

  • pattern matching,

  • records…​

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

  • 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

Build systems

mavenlogo
  • XML configuration, Maven project layout, Maven Central

gradlelogo
  • Groovy/Kotlin DSL

Jenkins CI

jenkins logo title

Java community

  • JUGs (Moscow, St. Petersburg, Novosibirsk)

  • Conferences (Joker, JPoint)

  • Come to Moscow JUG (when the pandemic is over)

jugmsk

Oracle certification exams

  • OCA/OCP

Hello, world!

Build and run the jar file