KEMBAR78
Java and the JVM | PPTX
Java and the JVM



                Manish Pandit
              IGN Engineering
Java Echosystem
• Java as a programming language
• Java as runtime platform/virtual machine
• Java libraries (collections, database drivers..)
Java the programming language
•   Object Oriented
•   Multi-threaded, Concurrent
•   Strongly typed
•   Garbage collection
•   No multiple inheritance
•   2nd most popular language after C at 16%
IDEs
• Please, no vim, emacs, pico etc. I completely
  get it that you’re rock awesome when you
  code on the command line with these tools.
• The IDEs were built to make you productive.
• I use Eclipse, the team uses IntelliJ IDEA. Both
  are good.
• Use anything as long as it’s a real IDE
Code Organization
•   Package Declaration
•   Imports
•   Class or Interface Declaration
•   Members
    – Class variables
    – Methods
Imports
• To use classes from a binary distribution or
  source
• Can be wildcarded (discouraged)
• Auto cleanup – Eclipse Ctl-Shift-O
• Unused Imports
Packages
• To namespace the Class or Interface
• Act as modules containing groups of Classes or
  Interfaces
• Convention
  – dot-separated
  – com., net., sf., org.
• Can contain subpackages
• Packaging can impact visibility if default scope
  is used.
Classes
• Have to have the same file name as the public
  class
• May or may not be a part of a package
• Can be abstract or concrete and/or final
• Acts as a template to create Objects
Classes
• Have a constructor
  – Can be private (for singletons)
  – Default no-args constructor
  – Objects created by using new

     <T> varName = new <? extends T>(args);
     String myName = new String(“Manish”);
Control Structures
•   if-else
•   for loops
•   ternary operator (: ?)
•    while loops
•   switch/case
•   try/catch/finally
•   break/continue for loop control
Using this
• this provides a reference to the current
  instance
• Static members cannot use this, as they do
  not have instances (think Class, not Object)
Typed Collections and Classes
• Introduced in Java 1.5
• Add strong typing via declaration, so the
  compile time checks can be performed
• Syntax:
           Collection myCollection = new ArrayList<String>();
Annotations
• Declarative programming
  – @SuppressWarnings
  – @Override
  – @Deprecated
• Custom annotations
  – An annotation is an @interface
Threading
• Two ways
  – Implement Runnable Interface
  – Extend Thread class
• In both cases, you put the implementation in a
  method called run()
• A thread is started by instantiating the Thread
  and calling start() on it. Never call run()
  directly.
Concurrent Code
• synchornized method
  – Makes a method thread safe
  – You cannot synchronize a constructor
• synchronized block
  – You can acquire a lock on an object, and write
    your code as synchronized(lock) {…}
  – All synchronized methods of a class use the same
    lock if you use synchronize(this) so be careful!
Exceptions
• Checked
• Runtime
Dependency Injection
• Is used to specify dependencies at runtime,
  which get injected (instantiated, associated)
  on class initialization.
• Popular Frameworks
  – Spring DI
  – Google guice
• Declared via configuration, or annotations
Maven
• Maven (and Ant, and Gradle..) are build tools
  used to manage (large) java projects
• Helps manage dependencies declaratively
• Rich set of plugins to run tests, generate
  javadocs, build sites and artifacts
• Everything comes together in pom.xml file.
JVM
•   A very efficient, tuned virtual machine
•   Runs bytecode
•   Runtime garbage collection
•   Supports monitoring via JMX
•   Concurrent
•   Target VM for Groovy, Scala and Clojure
JVM Memory Model
• Heap
  – Where the instance, static variables and Objects
    go
  – Shared across all threads in the VM
  – Automatically garbage collected
• Stack
  – Where the methods and local variables go
  – Every thread gets its own stack
JVM Performance Management
•   Heap and stack sizes
•   GC algorithm tuning
•   Heap monitoring (jprofiler)
•   Deadlocks
•   JMX (jconsole)
Java Libraries
• Utilities
   – Apache Commons
   – JodaTime
   – Google collections
• Web Frameworks
   – JSF
   – Struts
• Application Frameworks
   – Spring
   – Play! Framework
Common java packages
Package           Description
Java.lang.*       Has the core classes like threading, runtime, Data types..
Java.net.*        Has networking classes and adapters (think URL)
Java.util.*       Hash Collections, Calendar, Time/Locale classes
Java.io.*         Has Stream and Buffer handling classes for I/O
Javax.swing.*     Thick Client (UI) classes
Further reading
• Javadocs
• Java Language Specification
• Wikipedia

Java and the JVM

  • 1.
    Java and theJVM Manish Pandit IGN Engineering
  • 2.
    Java Echosystem • Javaas a programming language • Java as runtime platform/virtual machine • Java libraries (collections, database drivers..)
  • 3.
    Java the programminglanguage • Object Oriented • Multi-threaded, Concurrent • Strongly typed • Garbage collection • No multiple inheritance • 2nd most popular language after C at 16%
  • 4.
    IDEs • Please, novim, emacs, pico etc. I completely get it that you’re rock awesome when you code on the command line with these tools. • The IDEs were built to make you productive. • I use Eclipse, the team uses IntelliJ IDEA. Both are good. • Use anything as long as it’s a real IDE
  • 5.
    Code Organization • Package Declaration • Imports • Class or Interface Declaration • Members – Class variables – Methods
  • 6.
    Imports • To useclasses from a binary distribution or source • Can be wildcarded (discouraged) • Auto cleanup – Eclipse Ctl-Shift-O • Unused Imports
  • 7.
    Packages • To namespacethe Class or Interface • Act as modules containing groups of Classes or Interfaces • Convention – dot-separated – com., net., sf., org. • Can contain subpackages • Packaging can impact visibility if default scope is used.
  • 8.
    Classes • Have tohave the same file name as the public class • May or may not be a part of a package • Can be abstract or concrete and/or final • Acts as a template to create Objects
  • 9.
    Classes • Have aconstructor – Can be private (for singletons) – Default no-args constructor – Objects created by using new <T> varName = new <? extends T>(args); String myName = new String(“Manish”);
  • 10.
    Control Structures • if-else • for loops • ternary operator (: ?) • while loops • switch/case • try/catch/finally • break/continue for loop control
  • 11.
    Using this • thisprovides a reference to the current instance • Static members cannot use this, as they do not have instances (think Class, not Object)
  • 12.
    Typed Collections andClasses • Introduced in Java 1.5 • Add strong typing via declaration, so the compile time checks can be performed • Syntax: Collection myCollection = new ArrayList<String>();
  • 13.
    Annotations • Declarative programming – @SuppressWarnings – @Override – @Deprecated • Custom annotations – An annotation is an @interface
  • 14.
    Threading • Two ways – Implement Runnable Interface – Extend Thread class • In both cases, you put the implementation in a method called run() • A thread is started by instantiating the Thread and calling start() on it. Never call run() directly.
  • 15.
    Concurrent Code • synchornizedmethod – Makes a method thread safe – You cannot synchronize a constructor • synchronized block – You can acquire a lock on an object, and write your code as synchronized(lock) {…} – All synchronized methods of a class use the same lock if you use synchronize(this) so be careful!
  • 16.
  • 17.
    Dependency Injection • Isused to specify dependencies at runtime, which get injected (instantiated, associated) on class initialization. • Popular Frameworks – Spring DI – Google guice • Declared via configuration, or annotations
  • 18.
    Maven • Maven (andAnt, and Gradle..) are build tools used to manage (large) java projects • Helps manage dependencies declaratively • Rich set of plugins to run tests, generate javadocs, build sites and artifacts • Everything comes together in pom.xml file.
  • 19.
    JVM • A very efficient, tuned virtual machine • Runs bytecode • Runtime garbage collection • Supports monitoring via JMX • Concurrent • Target VM for Groovy, Scala and Clojure
  • 20.
    JVM Memory Model •Heap – Where the instance, static variables and Objects go – Shared across all threads in the VM – Automatically garbage collected • Stack – Where the methods and local variables go – Every thread gets its own stack
  • 21.
    JVM Performance Management • Heap and stack sizes • GC algorithm tuning • Heap monitoring (jprofiler) • Deadlocks • JMX (jconsole)
  • 22.
    Java Libraries • Utilities – Apache Commons – JodaTime – Google collections • Web Frameworks – JSF – Struts • Application Frameworks – Spring – Play! Framework
  • 23.
    Common java packages Package Description Java.lang.* Has the core classes like threading, runtime, Data types.. Java.net.* Has networking classes and adapters (think URL) Java.util.* Hash Collections, Calendar, Time/Locale classes Java.io.* Has Stream and Buffer handling classes for I/O Javax.swing.* Thick Client (UI) classes
  • 24.
    Further reading • Javadocs •Java Language Specification • Wikipedia