O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Introduction to JVM Languages

Book Description

Explore the Java Virtual Machine with modern programming languages

About This Book

  • This guide provides in-depth coverage of the Java Virtual Machine and its features
  • Filled with practical examples, this book will help you understand the core concepts of Java, Scala, Clojure, Kotlin, and Groovy
  • Work with various programming paradigms and gain knowledge about imperative, object-oriented and functional programming

Who This Book Is For

This book is meant for programmers who are interested in the Java Virtual Machine (JVM) and want to learn more about the most popular programming languages that can be used for JVM development. Basic practical knowledge of a modern programming language that supports object-oriented programming (JavaScript, Python, C#, VB.NET, and C++) is assumed.

What You Will Learn

  • Gain practical information about the Java Virtual Machine
  • Understand the popular JVM languages and the Java Class Library
  • Get to know about various programming paradigms such as imperative, object-oriented, and functional
  • Work with common JVM tools such as Eclipse IDE, Gradle, and Maven
  • Explore frameworks such as SparkJava, Vert.x, Akka and JavaFX
  • Boost your knowledge about dialects of other well-known programming languages that run on the JVM, including JavaScript, Python, and Ruby

In Detail

Anyone who knows software development knows about the Java Virtual Machine. The Java Virtual Machine is responsible for interpreting Java byte code and translating it into actions. In the beginning, Java was the only programming language used for the JVM. But increasing complexity of the language and the remarkable performance of the JVM created an opening for a new generation of programming languages. If you want to build a strong foundation with the Java Virtual Machine and get started with popular modern programming languages, then this book is for you.

The book will begin with a general introduction of the JVM and its features, which are common to the JVM languages, helping you get abreast with its concepts. It will then dive into explaining languages such as Java, Scala, Clojure, Kotlin, and Groovy and will show how to work with each language, their features, use cases, and pros and cons. By writing example projects in those languages and focusing on each language's strong points, it will help you find the programming language that is most appropriate for your particular needs.

By the end of the book, you will have written multiple programs that run on the Java Virtual Machine and know about the differences between the various languages.

Style and approach

This practical, example-filled guide will help you get started with the JVM and some of its most popular languages.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Java Virtual Machine
    1. JVM implementations
    2. Why develop on JVM?
      1. JVM adapts to market changes
      2. Java Class Library
      3. Ecosystem
        1. Scenario 1 – Using a JVM application server
        2. Scenario 2 – Using a general high-level web application framework
        3. Scenario 3 – Using a microservice framework
    3. Popular use cases
      1. Web applications
      2. Big data
      3. IoT
    4. JVM concepts
      1. Virtual machine
      2. The JIT compiler
      3. Primitive datatypes
      4. Classes
      5. Reference types
        1. References and null references
      6. Garbage collector
        1. Example
      7. Backward compatibility
      8. Build tools
    5. Java editions
      1. Java SE
      2. Java EE
        1. Example – Java Persistence API as implemented by two application servers
      3. Java ME
    6. Other languages on JVM
      1. Why choose a language other than Java?
        1. Java is a very verbose language
        2. Java is not ideal for everything or everyone
        3. Missing classes in Java Class Library
      2. Mixing JVM languages in a project
        1. Increasing build process complexity
        2. Language runtime libraries
      3. Writing unit tests in a different language
    7. Summary
  3. Developing on the Java Virtual Machine
    1. JDK
      1. Installing JDK
        1. Downloading JDK
        2. Installing JDK on Windows
        3. Installing JDK on macOS
        4. Installing JDK on Linux
        5. Downloading API documentation
      2. Exploring JDK
        1. The directory structure
        2. JDK commands
        3. GUI monitoring tools
          1. Java VisualVM
          2. Oracle Mission Control
          3. JConsole
      3. JRE
    2. Class organization with packages
      1. What are packages?
      2. Choosing a package name
      3. Package name examples
      4. Fully qualified class name
    3. Java Class Library
      1. Java Class Library organization
      2. Package overview
      3. Fundamental classes of the java.lang package
        1. The Object class (java.lang.Object)
          1. Important object methods
        2. The String class (java.lang.String)
        3. Primitive wrapper classes (Integer, Long, Short, Char, Float, Double in java.lang)
          1. Autoboxing examples
        4. Exceptions and errors (java.lang.Exception and java.lang.Error)
      4. The Collections API - java.util.ArrayList and java.util.HashMap
        1. ArrayList (java.util.ArrayList)
          1. Commonly used methods of the ArrayList class
          2. ArrayList usage example
        2. HashMap (java.util.HashMap)
          1. Commonly used methods of the HashMap class
          2. HashMap usage example
        3. Preparing your classes for the Collections API
          1. About hashCode()
          2. About equals ()
          3. The hashing mechanism
    4. Running JVM applications on the command line
      1. At least one class must have a static main() method
      2. Required directory structure for class files
      3. Setting ClassPath for a JVM instance
      4. Placing class files inside a JAR archive
        1. Runnable JAR file
      5. Running a program with the java command
        1. Run a project consisting of separate class files
        2. Running a project that is placed inside a runnable JAR file
        3. Other useful parameters of the java command
          1. -D to pass properties and values
          2. -ea to enable assertions
      6. A hands-on example project to run on JVM
        1. A ClassPath example
    5. Eclipse IDE
      1. Downloading Eclipse IDE
      2. Installing Eclipse IDE
    6. Summary
  4. Java
    1. OOP in Java
      1. Defining classes
      2. Class access modifiers
      3. Final class modifier - locking a class
      4. Defining packages
      5. Importing classes
      6. Adding class members - variables and methods
        1. Instance variables
        2. Methods
      7. Modifiers
        1. Protecting class members with access modifiers
          1. Access modifier example
        2. Static modifier - instance variables and class variables
        3. Final modifier - locking a class member
        4. Overloading methods
      8. Constructors and finalizers
        1. Constructors
        2. Finalizers
        3. Extending a class
          1. Overriding methods
          2. Calling constructors of a parent class
        4. Abstract classes
        5. Interfaces
      9. Upcasting and downcasting
    2. Writing Java code
      1. Operators
      2. Conditional checks
        1. The if...else statement
        2. The switch...case statement
      3. POJO
      4. Arrays
      5. Generics and Collections
      6. Loops
        1. The for loop
          1. The normal for loop
          2. The enhanced for loop
          3. The while loop
          4. The do...while loop
      7. Exceptions
        1. Runtime exceptions
      8. Threads
      9. Lambdas
    3. Style guide
    4. Quiz
    5. Summary
  5. Java Programming
    1. Configuring Eclipse IDE
    2. Creating a web service in Java
      1. Creating a new Gradle project in Eclipse IDE
        1. Exploring the generated project
      2. Modifying the Gradle build file
      3. Building the project
      4. Coding the backend class
        1. Backend class business rules
        2. Creating a dummy implementation of the method
        3. Creating the test case class and writing its first unit test
        4. Implementing an input validation check
        5. Writing the second unit test
        6. Implementing the business logic
        7. Creating an executable application task
        8. Creating a web service
        9. Running the web service
        10. Creating Javadoc documentation
    3. Summary
  6. Scala
    1. Installing Scala
    2. Scala's Read-Eval-Print-Loop shell
    3. Functional versus imperative programming
    4. Scala language syntax and rules
      1. Statically typed language
      2. Mutable and immutable variables
      3. Common Scala types
        1. Any class
          1. AnyRef class - reference classes
          2. AnyVal class - value classes
        2. Strings
    5. OOP in Scala
      1. Defining packages and subpackages
      2. Importing members
      3. Defining classes
      4. Instance variables and methods
        1. Instance variables
        2. Instance methods
        3. Access modifiers for class instance members
      5. Constructors
      6. Extending a class
        1. Overriding methods
      7. Overloading methods
      8. Abstract classes
      9. Traits
      10. Singleton objects
      11. Operator overloading
      12. Case classes
    6. Scala's standard library
      1. Generics
      2. Collections
        1. Immutable list
        2. Mutable list
        3. Immutable map
        4. Mutable map
      3. XML processing
    7. Functional programming in Scala
      1. Iterating through collections using functions
      2. The map, filter, and reduce design pattern
        1. Map - transform data
        2. Filter - filter items from a collection or array
        3. Reduce - for performing calculations
      3. Currying
    8. Quiz
    9. Summary
  7. Scala Programming
    1. Scala IDE for the Eclipse plugin
      1. Installing Scala IDE for Eclipse
      2. Switching to the Scala IDE perspective
    2. SBT
      1. Installing SBT
      2. Creating an SBT-based Eclipse IDE project
        1. Creating a new SBT project
        2. Loading the SBTEclipse plugin
        3. Generating a new Eclipse IDE project with SBTEclipse
        4. Importing the generated project in Eclipse IDE
      3. The Scala compiler (scalac)
        1. Creating a singleton object with the main() method
        2. Creating a singleton object that extends the App trait
    3. Creating an Akka project
      1. Adding an Akka dependency to the SBT build file
      2. Updating the Scala IDE project
      3. Akka concepts
        1. Actors
        2. Actor references (ActorRef)
        3. Messages
        4. Dispatchers
      4. Creating our first Akka actor - QuotesHandlerActor
      5. Creating messages
      6. Writing a ScalaTest-based unit test
      7. Implementing a message handler
      8. Creating QuotePrinterActor
      9. The main application
    4. Summary
  8. Clojure
    1. Installing Clojure
      1. Creating a start script
        1. Creating a start script on Windows
        2. Creating a start script on macOS and Linux
    2. Clojure's interactive shell (REPL)
    3. Clojure language
      1. Syntax
      2. Expressions
      3. Defining variables
      4. Defining functions
      5. Data structures
        1. Numeric types
        2. Strings and characters
        3. Collections
          1. Lists
          2. Vectors
          3. Sets
          4. Hash-maps
        4. Iteration over arrays and loops
        5. Conditions
    4. Working with Java classes
      1. Creating simple Java classes with deftype and defrecord
    5. Managing states with agents
      1. Agent example
    6. Style guide
    7. Quiz
    8. Summary
  9. Clojure Programming
    1. The Counterclockwise plugin for Eclipse IDE
      1. Installing the Counterclockwise plugin
      2. Switching to the Java perspective
    2. Leiningen's build tool
      1. Installing Leiningen
    3. Creating executable programs in Clojure
      1. Compiling to class files without Leiningen
      2. Compiling projects with Leiningen
    4. Creating a new Counterclockwise project
      1. Clojure REPL in Eclipse IDE
      2. Updating the project's Clojure version
      3. Adding a dependency
    5. Exploring monads by applying test-driven development
    6. The Luminus web framework
      1. Creating a Luminus project
      2. Importing the project in Counterclockwise
      3. Exploring the Luminus project
      4. Adding a page to the web application
    7. Summary
  10. Kotlin
    1. Installing Kotlin
      1. Launch scripts
    2. Kotlin's REPL interactive shell
    3. Kotlin language fundamentals
      1. Defining local variables
      2. Defining a function
      3. Kotlin's types
        1. Kotlin basic types
        2. Strings
        3. Null safety handling
          1. Option 1 - Adding a conditional check
          2. Option 2 - Using the safe call operator ?.
          3. Option 3 - Using the Elvis operator ?:
          4. Option 4 - Using the !! operator
        4. Conversions
        5. Collections and generics
      4. Loops
    4. OOP in Kotlin
      1. Defining packages
      2. Importing members
      3. Defining classes and constructors
      4. Adding members to classes
        1. Adding functions
          1. The main entry function
        2. Adding properties
      5. Inheritance
      6. Interfaces
      7. Visibility modifiers
      8. Singleton and companion objects
      9. Data classes
      10. Lambdas and inline functions
    5. Procedural programming in Kotlin
    6. Style guide
    7. Quiz
    8. Summary
  11. Kotlin Programming
    1. Kotlin for the Eclipse IDE plugin
      1. Installing the Kotlin plugin for the Eclipse IDE
      2. Switching to Kotlin perspective
    2. Apache Maven
      1. Installing Apache Maven
      2. Downloading a preconfigured Kotlin starter kit
      3. Importing the project in the Eclipse IDE
      4. Exploring the pom.xml build file
      5. Updating the build file in Eclipse
    3. Creating a JavaFX desktop GUI application
      1. Preparing the project
      2. Creating a runnable application
      3. Writing an extension function
      4. Layout panes
      5. Implementing a BorderPane-based layout
      6. Implementing animation
      7. Debugging the program
    4. Summary
  12. Groovy
    1. Installing Groovy
      1. GroovyConsole and GroovyShell
        1. GroovyConsole
        2. GroovyShell
    2. Groovy Language
      1. Object-oriented programming in Groovy
        1. Groovy is fully object oriented
        2. Access modifiers
        3. Adding properties to a class
        4. Optional types
        5. Automatically creating a fully featured POJO
        6. Creating immutable classes
    3. Groovy Development Kit (GDK)
      1. Groovy Strings (GStrings)
      2. Collections
        1. Lists
        2. Maps
    4. Dynamic and static programming
      1. Meta programming
      2. Static programming in Groovy
    5. Quiz
    6. Summary
  13. Groovy Programming
    1. Installing the Groovy Eclipse plugin
      1. Switching to Java perspective
    2. Apache Ivy and IvyDE
      1. Installing Apache IvyDE plugin for Eclipse IDE
    3. Creating and configuring the project
      1. Creating a new Groovy Eclipse project
      2. Creating an ivy.xml file for Ivy
    4. Java Database Connectivity (JDBC)
      1. H2 database
      2. Creating an in-memory database
    5. Generating XML using MarkupBuilder
      1. Generating XML based on SQL
    6. The Vert.x microservice platform
      1. Adding Vert.x dependency to Ivy
      2. Creating the web service
    7. Summary
  14. Other JVM languages
    1. Oracle Nashorn
      1. Embedding Nashorn in JVM-based projects
      2. Running Nashorn
    2. Jython (Python)
      1. Differences between CPython and Jython
      2. Running Jython
    3. JRuby (Ruby)
      1. Ruby on Rails and JRuby
      2. Running JRuby
    4. Frege (Haskell)
      1. Calling Java code from Frege
      2. Running Frege
    5. Ceylon
      1. Ceylon's module system
      2. Running Ceylon
    6. Summary
  15. Quiz Answers
    1. Chapter 3: Java
    2. Chapter 5: Scala
    3. Chapter 7: Clojure
    4. Chapter 9: Kotlin
    5. Chapter 11: Groovy