2. Java Echosystem
• Java as a programming language
• Java as runtime platform/virtual machine
• Java libraries (collections, database drivers..)
3. Java the programming language
• Object Oriented
• Multi-threaded, Concurrent
• Strongly typed
• Garbage collection
• No multiple inheritance
• 2nd most popular language after C at 16%
4. 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
5. Code Organization
• Package Declaration
• Imports
• Class or Interface Declaration
• Members
– Class variables
– Methods
6. Imports
• To use classes from a binary distribution or
source
• Can be wildcarded (discouraged)
• Auto cleanup – Eclipse Ctl-Shift-O
• Unused Imports
7. 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.
8. 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
9. 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”);
10. Control Structures
• if-else
• for loops
• ternary operator (: ?)
• while loops
• switch/case
• try/catch/finally
• break/continue for loop control
11. 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)
12. 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>();
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
• 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!
17. 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
18. 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.
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
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