JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics...

Post on 28-Jun-2020

12 views 0 download

Transcript of JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics...

JVM Survival Guide Hadi Hariri

This talk

❖ For What

❖ For Who

20 years of Java

❖ The Sun and The Oracle

❖ Java The Language

❖ Java The Virtual Machine

❖ Java The Ecosystem

The Community

❖ Community Driven

❖ Not Vendor-Driven

❖ Large and Open Ecosystem

It’s an Open Platform

❖ Languages

❖ Java, Clojure, Scala, Kotlin, Frege, Groovy, JRuby, Ceylon

❖ Frameworks

❖ JavaEE, Spring, Spark, Play Framework

What to Install

Java SE

Java FX

Java EE

Java ME

JDK

JRE

Versioning

❖ Language - 1.X where X is the version

❖ Java XY - X where X is the version

❖ Usually aligned major versioning in Language and VM

Installing Java

❖ Oracle Web Site (includes Ask Toolbar)

❖ Package Managers

❖ Brew, Chocolatey

JAVA_HOME

❖ Defines where Java Virtual Machine is located

❖ On Linux/OSX if undefined

export JAVA_HOME=$(/usr/libexec/java_home)

A First Java App

Create a Java AppUse javac to compile itUse java to run itExample - firstApp

Decompiling

Decompiling exampleshow byte code and also decompiling

Multiple Classes and Files

usingClass example

Java ARchives

❖ Archive classes files into single jar file

❖ Provide metadata with METAINF file

❖ jar command line to create jar files

❖ war is the web equivalent of jar

Creating Jar Files

createJar.sh samples

Conventions

❖ In Java

❖ Virtual by default

❖ Open by default

❖ In other languages

❖ It depends

Conventions

❖ Packages and Modules

❖ Accessibility and Visibility Modifiers

❖ In Java, single class per file is enforced

Conventions

❖ Java and Java-inspired languages

lowerCamelCaseNotation

Namespaces

❖ Or death by a hundred folders

ext.vendor.module.subsystem

❖ Individual imports has been widely adopted

Namespaces

namespaces demo

The Class Path

❖ Defines where Java Classes are located

❖ Set via command line java -cp

❖ Most common reason for NoClassDefFoundError

Class Loaders

❖ Responsible for loading classes

❖ Each class loader has a parent

❖ Delegates what it can to its parent

Class Loaders

Parent

Bootstrap Class Loader

Load java.lang and other key classes

Parent

ExtensionClass Loader

Load java.ext.dirs

Parent

SystemClass Loader

Load classpath

Other Common Errors

❖ NoSuchMethodError - wrong version of class loaded

❖ ClassCastException - casting to wrong type

❖ IllegalAccessError - wrong access modifier

Using ClassPath

usingClass Path example Compile and remove to show exception

Loading Classes

loadingClasses Example

Build Systems

❖ Ant

❖ IntelliJ IDEA

❖ Eclipse

Build and Dependency Management

❖ Maven

❖ Gradle

Maven

❖ XML-Based

❖ Extensible

❖ Supports templates

❖ Maven Central Repository for Packages (and custom)

Maven

mavenSamplemavenDependencymavenZipmavenTeamCity

Gradle

❖ Not XML a.k.a. a better DSL

❖ Based on Groovy

❖ Extensible

❖ Somewhat template support (gradle --init)

❖ Maven Central Repository for Packages (and custom)

Gradle

gradleSamplesampleApp - app plugin

Working with Resources

❖ Resource folder to ship contents along with your app

❖ Ability to define in Maven/Gradle

Resources

resourcesSampleresourcesSampleMaven

Garbage Collection

❖ Generational (Young, Old, Permanent)

❖ The “IDisposable” is Closeable

❖ The “using” is “try-with-resources”

Asynchronous Programming

❖ CompletableFuture

❖ RxJava and adapters

Generics

❖ Most languages support Generics

❖ Reified Generics on JVM

Genericsexample of genericsexample of call site and covarianceexample of reflection

Testing Frameworks

❖ JUnit - The Standard

❖ TestNg

❖ Spek

Testing

junitjunit from Java and Kotlinspek

JavaEE

❖ The Dreaded Java 2 EE Glorious Enterprise

❖ Application Servers

Web Frameworks

❖ Servlets and Application Containers

❖ Jetty, TomCat

Spring

Spring

❖ Application Framework

❖ Web, Security, Templating, SQL, AWS, Social

❖ start.spring.io as generator

Spring

springRestSamplestart.spring.io

Vert.x

Vert.x

vertx example

Other Interesting Libraries

❖ Mockito for mocking

❖ GSon and Jackson for JSON Serialisation

❖ JodaTime for Calendar and Time Management

❖ Netty a low-level network client and server library

Summary

❖ Java is a very vibrant ecosystem

❖ Many languages, many frameworks

❖ There is no single vendor driven strategy as such

Thank you@hhariri - mail@hadihariri.com