Java and the JVM

24
Java and the JVM Manish Pandit IGN Engineering

description

My presentation to the code foo 2012 interns. http://code.ign.com/foo

Transcript of Java and the JVM

Page 1: Java and the JVM

Java and the JVM

Manish PanditIGN Engineering

Page 2: Java and the JVM

Java Echosystem

• Java as a programming language• Java as runtime platform/virtual machine• Java libraries (collections, database drivers..)

Page 3: Java and the JVM

Java the programming language

• Object Oriented• Multi-threaded, Concurrent• Strongly typed• Garbage collection• No multiple inheritance• 2nd most popular language after C at 16%

Page 4: Java and the JVM

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

Page 5: Java and the JVM

Code Organization

• Package Declaration• Imports• Class or Interface Declaration• Members– Class variables– Methods

Page 6: Java and the JVM

Imports

• To use classes from a binary distribution or source

• Can be wildcarded (discouraged)• Auto cleanup – Eclipse Ctl-Shift-O• Unused Imports

Page 7: Java and the JVM

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.

Page 8: Java and the JVM

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

Page 9: Java and the JVM

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”);

Page 10: Java and the JVM

Control Structures

• if-else• for loops• ternary operator (: ?)• while loops• switch/case• try/catch/finally• break/continue for loop control

Page 11: Java and the JVM

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)

Page 12: Java and the JVM

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>();

Page 13: Java and the JVM

Annotations

• Declarative programming– @SuppressWarnings– @Override– @Deprecated

• Custom annotations– An annotation is an @interface

Page 14: Java and the JVM

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.

Page 15: Java and the JVM

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!

Page 16: Java and the JVM

Exceptions

• Checked• Runtime

Page 17: Java and the JVM

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

Page 18: Java and the JVM

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.

Page 19: Java and the JVM

JVM

• A very efficient, tuned virtual machine• Runs bytecode• Runtime garbage collection• Supports monitoring via JMX• Concurrent• Target VM for Groovy, Scala and Clojure

Page 20: Java and the JVM

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

Page 21: Java and the JVM

JVM Performance Management

• Heap and stack sizes• GC algorithm tuning• Heap monitoring (jprofiler)• Deadlocks• JMX (jconsole)

Page 22: Java and the JVM

Java Libraries

• Utilities– Apache Commons– JodaTime– Google collections

• Web Frameworks– JSF– Struts

• Application Frameworks– Spring– Play! Framework

Page 23: Java and the JVM

Common java packagesPackage 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

Page 24: Java and the JVM

Further reading

• Javadocs• Java Language Specification• Wikipedia