Introduction to JVM Languages
()
About this ebook
- 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
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.
Related to Introduction to JVM Languages
Related ebooks
Learning RxJava Rating: 4 out of 5 stars4/5Java 9 Concurrency Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsJava 9 Programming By Example Rating: 4 out of 5 stars4/5Scala for Java Developers Rating: 5 out of 5 stars5/5Distributed Computing in Java 9 Rating: 0 out of 5 stars0 ratingsLearning Reactive Programming with Java 8 Rating: 0 out of 5 stars0 ratingsJava Coding Problems: Improve your Java Programming skills by solving real-world coding challenges Rating: 0 out of 5 stars0 ratingsLearn Java 12 Programming: A step-by-step guide to learning essential concepts in Java SE 10, 11, and 12 Rating: 0 out of 5 stars0 ratingsClojure for Java Developers Rating: 0 out of 5 stars0 ratingsMastering Clojure Rating: 0 out of 5 stars0 ratingsClojure Data Structures and Algorithms Cookbook Rating: 0 out of 5 stars0 ratingsMastering Apache Maven 3 Rating: 0 out of 5 stars0 ratingsRabbitMQ Essentials Rating: 0 out of 5 stars0 ratingsASP.NET Core 3 and React: Hands-On full stack web development using ASP.NET Core, React, and TypeScript 3 Rating: 0 out of 5 stars0 ratingsInstant Java Password and Authentication Security Rating: 0 out of 5 stars0 ratingsMastering Java EE Development with WildFly Rating: 0 out of 5 stars0 ratingsJasmine JavaScript Testing - Second Edition Rating: 0 out of 5 stars0 ratingsMaven Essentials Rating: 0 out of 5 stars0 ratingsMastering Hibernate Rating: 0 out of 5 stars0 ratingsJUnit in Action Rating: 0 out of 5 stars0 ratingsMastering Concurrency Programming with Java 8 Rating: 0 out of 5 stars0 ratingsClojure Programming Cookbook Rating: 0 out of 5 stars0 ratingsThe Java Workshop: Learn object-oriented programming and kickstart your career in software development Rating: 0 out of 5 stars0 ratingsLearning Elixir Rating: 0 out of 5 stars0 ratingsThe Joy of Clojure Rating: 4 out of 5 stars4/5C# Programming Cookbook Rating: 0 out of 5 stars0 ratingsFunctional Programming in Kotlin Rating: 0 out of 5 stars0 ratingsJava Testing with Spock Rating: 0 out of 5 stars0 ratingsGetting Started with LLVM Core Libraries Rating: 0 out of 5 stars0 ratingsRx.NET in Action Rating: 0 out of 5 stars0 ratings
Programming For You
Game Development with Unreal Engine 5: Learn the Basics of Game Development in Unreal Engine 5 (English Edition) Rating: 0 out of 5 stars0 ratingsPython: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5The Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsPython Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratingsLearn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratingsCoding All-in-One For Dummies Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Beginning Programming with Python For Dummies Rating: 3 out of 5 stars3/5Web Designer's Idea Book, Volume 4: Inspiration from the Best Web Design Trends, Themes and Styles Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5
Reviews for Introduction to JVM Languages
0 ratings0 reviews
Book preview
Introduction to JVM Languages - Vincent van der Leun
Introduction to JVM Languages
Java, Scala, Clojure, Kotlin, and Groovy
Vincent van der Leun
BIRMINGHAM - MUMBAI
Introduction to JVM Languages
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: June 2017
Production reference: 1230617
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78712-794-4
www.packtpub.com
Credits
About the Author
Vincent van der Leun is a software engineer living in the city of Utrecht in the Netherlands. Programming since the age of 8, he has worked with many different languages and platforms over the years. Rediscovering Java a few years ago, he loved it so much that he became an Oracle Certified Professional, Java 7 Programmer, and started the JVM Fanboy blog. Currently he works for CloudSuite, a company specializing in modern e-commerce solutions. At CloudSuite he works on various backend systems and web services, writes JavaScript code for frontend applications, supports consultants by providing complex SQL queries, and consumes coffee while having design-related discussions with fellow developers. When not trying out new web frameworks or technologies in his spare time, he is collecting cult movies and obscure action flicks on DVD/Bluray, reading classic science fiction novels, or attending concerts of non-mainstream singers and songwriters.
I am grateful to everyone at Packt, for their hard work in making this book a reality. Thanks to the editors, Nitin, Vikas and Subhalaxmi and to the reviewer, Ramasubramanian. I'd like to thank my parents, Anton and Irene, and my brothers, Alexander and Ruben (and Wendy). Thank you for your awesome support! Thanks to my family and friends. The following persons deserve a special mention: Erik, Guy, Mallory, Job, Jenna, and Nina for their much appreciated support and encouragement during the writing of this book; Natalie and Marco for the good old times. Last but not least, kudos to all my colleagues at CloudSuite; Special thanks to Corne, Rob, Emeli, and Berthold.
I dedicate this book to the amazingly funny, kind and always clever Melissa and Esmee Hulstein.
About the Reviewer
Ramasubramanian Sankar is a passionate, polyglot, fullstack developer, and a seasoned technical architect working in the Information Technology industry for over 13 years now. Specializing on JVM-based backend services and distributed systems, he has experience with a diverse set of platforms and languages. After having worked with service companies, banks, antivirus product companies, and product-based start-ups, he is currently working as a consultant for Ajira technologies in Chennai with a group of like-minded technologists. He is passionate about architecting and designing simple, high-performant, and clean solutions for complex business problems. He writes/rants about his learnings on Twitter (@ramsankar83) and on his technology blog (http://technicalitee.blogspot.in/). His current interest is in learning functional programming and putting it to practical use. He loves LISP and is actively looking forward to use it in production some day.
I would like to thank my wife, Gomathi, and kids for giving me the extra time to code outside work and review this book. I am grateful to my parents, Sankar and Geetha, for providing me with an education and buying my first computer. I would also like to thank my employer, Ajira technologies, for giving me the freedom and encouragement to try out cutting-edge tech in customer projects and, thereby, learn a lot.
www.PacktPub.com
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/178712794X.
If you'd like to join our team of regular reviewers, you can e-mail us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Table of Contents
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
Java Virtual Machine
JVM implementations
Why develop on JVM?
JVM adapts to market changes
Java Class Library
Ecosystem
Scenario 1 – Using a JVM application server
Scenario 2 – Using a general high-level web application framework
Scenario 3 – Using a microservice framework
Popular use cases
Web applications
Big data
IoT
JVM concepts
Virtual machine
The JIT compiler
Primitive datatypes
Classes
Reference types
References and null references
Garbage collector
Example
Backward compatibility
Build tools
Java editions
Java SE
Java EE
Example – Java Persistence API as implemented by two application servers
Java ME
Other languages on JVM
Why choose a language other than Java?
Java is a very verbose language
Java is not ideal for everything or everyone
Missing classes in Java Class Library
Mixing JVM languages in a project
Increasing build process complexity
Language runtime libraries
Writing unit tests in a different language
Summary
Developing on the Java Virtual Machine
JDK
Installing JDK
Downloading JDK
Installing JDK on Windows
Installing JDK on macOS
Installing JDK on Linux
Downloading API documentation
Exploring JDK
The directory structure
JDK commands
GUI monitoring tools
Java VisualVM
Oracle Mission Control
JConsole
JRE
Class organization with packages
What are packages?
Choosing a package name
Package name examples
Fully qualified class name
Java Class Library
Java Class Library organization
Package overview
Fundamental classes of the java.lang package
The Object class (java.lang.Object)
Important object methods
The String class (java.lang.String)
Primitive wrapper classes (Integer, Long, Short, Char, Float, Double in java.lang)
Autoboxing examples
Exceptions and errors (java.lang.Exception and java.lang.Error)
The Collections API - java.util.ArrayList and java.util.HashMap
ArrayList (java.util.ArrayList)
Commonly used methods of the ArrayList class
ArrayList usage example
HashMap (java.util.HashMap)
Commonly used methods of the HashMap class
HashMap usage example
Preparing your classes for the Collections API
About hashCode()
About equals ()
The hashing mechanism
Running JVM applications on the command line
At least one class must have a static main() method
Required directory structure for class files
Setting ClassPath for a JVM instance
Placing class files inside a JAR archive
Runnable JAR file
Running a program with the java command
Run a project consisting of separate class files
Running a project that is placed inside a runnable JAR file
Other useful parameters of the java command
-D to pass properties and values
-ea to enable assertions
A hands-on example project to run on JVM
A ClassPath example
Eclipse IDE
Downloading Eclipse IDE
Installing Eclipse IDE
Summary
Java
OOP in Java
Defining classes
Class access modifiers
Final class modifier - locking a class
Defining packages
Importing classes
Adding class members - variables and methods
Instance variables
Methods
Modifiers
Protecting class members with access modifiers
Access modifier example
Static modifier - instance variables and class variables
Final modifier - locking a class member
Overloading methods
Constructors and finalizers
Constructors
Finalizers
Extending a class
Overriding methods
Calling constructors of a parent class
Abstract classes
Interfaces
Upcasting and downcasting
Writing Java code
Operators
Conditional checks
The if...else statement
The switch...case statement
POJO
Arrays
Generics and Collections
Loops
The for loop
The normal for loop
The enhanced for loop
The while loop
The do...while loop
Exceptions
Runtime exceptions
Threads
Lambdas
Style guide
Quiz
Summary
Java Programming
Configuring Eclipse IDE
Creating a web service in Java
Creating a new Gradle project in Eclipse IDE
Exploring the generated project
Modifying the Gradle build file
Building the project
Coding the backend class
Backend class business rules
Creating a dummy implementation of the method
Creating the test case class and writing its first unit test
Implementing an input validation check
Writing the second unit test
Implementing the business logic
Creating an executable application task
Creating a web service
Running the web service
Creating Javadoc documentation
Summary
Scala
Installing Scala
Scala's Read-Eval-Print-Loop shell
Functional versus imperative programming
Scala language syntax and rules
Statically typed language
Mutable and immutable variables
Common Scala types
Any class
AnyRef class - reference classes
AnyVal class - value classes
Strings
OOP in Scala
Defining packages and subpackages
Importing members
Defining classes
Instance variables and methods
Instance variables
Instance methods
Access modifiers for class instance members
Constructors
Extending a class
Overriding methods
Overloading methods
Abstract classes
Traits
Singleton objects
Operator overloading
Case classes
Scala's standard library
Generics
Collections
Immutable list
Mutable list
Immutable map
Mutable map
XML processing
Functional programming in Scala
Iterating through collections using functions
The map, filter, and reduce design pattern
Map - transform data
Filter - filter items from a collection or array
Reduce - for performing calculations
Currying
Quiz
Summary
Scala Programming
Scala IDE for the Eclipse plugin
Installing Scala IDE for Eclipse
Switching to the Scala IDE perspective
SBT
Installing SBT
Creating an SBT-based Eclipse IDE project
Creating a new SBT project
Loading the SBTEclipse plugin
Generating a new Eclipse IDE project with SBTEclipse
Importing the generated project in Eclipse IDE
The Scala compiler (scalac)
Creating a singleton object with the main() method
Creating a singleton object that extends the App trait
Creating an Akka project
Adding an Akka dependency to the SBT build file
Updating the Scala IDE project
Akka concepts
Actors
Actor references (ActorRef)
Messages
Dispatchers
Creating our first Akka actor - QuotesHandlerActor
Creating messages
Writing a ScalaTest-based unit test
Implementing a message handler
Creating QuotePrinterActor
The main application
Summary
Clojure
Installing Clojure
Creating a start script
Creating a start script on Windows
Creating a start script on macOS and Linux
Clojure's interactive shell (REPL)
Clojure language
Syntax
Expressions
Defining variables
Defining functions
Data structures
Numeric types
Strings and characters
Collections
Lists
Vectors
Sets
Hash-maps
Iteration over arrays and loops
Conditions
Working with Java classes
Creating simple Java classes with deftype and defrecord
Managing states with agents
Agent example
Style guide
Quiz
Summary
Clojure Programming
The Counterclockwise plugin for Eclipse IDE
Installing the Counterclockwise plugin
Switching to the Java perspective
Leiningen's build tool
Installing Leiningen
Creating executable programs in Clojure
Compiling to class files without Leiningen
Compiling projects with Leiningen
Creating a new Counterclockwise project
Clojure REPL in Eclipse IDE
Updating the project's Clojure version
Adding a dependency
Exploring monads by applying test-driven development
The Luminus web framework
Creating a Luminus project
Importing the project in Counterclockwise
Exploring the Luminus project
Adding a page to the web application
Summary
Kotlin
Installing Kotlin
Launch scripts
Kotlin's REPL interactive shell
Kotlin language fundamentals
Defining local variables
Defining a function
Kotlin's types
Kotlin basic types
Strings
Null safety handling
Option 1 - Adding a conditional check
Option 2 - Using the safe call operator ?.
Option 3 - Using the Elvis operator ?:
Option 4 - Using the !! operator
Conversions
Collections and generics
Loops
OOP in Kotlin
Defining packages
Importing members
Defining classes and constructors
Adding members to classes
Adding functions
The main entry function
Adding properties
Inheritance
Interfaces
Visibility modifiers
Singleton and companion objects
Data classes
Lambdas and inline functions
Procedural programming in Kotlin
Style guide
Quiz
Summary
Kotlin Programming
Kotlin for the Eclipse IDE plugin
Installing the Kotlin plugin for the Eclipse IDE
Switching to Kotlin perspective
Apache Maven
Installing Apache Maven
Downloading a preconfigured Kotlin starter kit
Importing the project in the Eclipse IDE
Exploring the pom.xml build file
Updating the build file in Eclipse
Creating a JavaFX desktop GUI application
Preparing the project
Creating a runnable application
Writing an extension function
Layout panes
Implementing a BorderPane-based layout
Implementing animation
Debugging the program
Summary
Groovy
Installing Groovy
GroovyConsole and GroovyShell
GroovyConsole
GroovyShell
Groovy Language
Object-oriented programming in Groovy
Groovy is fully object oriented
Access modifiers
Adding properties to a class
Optional types
Automatically creating a fully featured POJO
Creating immutable classes
Groovy Development Kit (GDK)
Groovy Strings (GStrings)
Collections
Lists
Maps
Dynamic and static programming
Meta programming
Static programming in Groovy
Quiz
Summary
Groovy Programming
Installing the Groovy Eclipse plugin
Switching to Java perspective
Apache Ivy and IvyDE
Installing Apache IvyDE plugin for Eclipse IDE
Creating and configuring the project
Creating a new Groovy Eclipse project
Creating an ivy.xml file for Ivy
Java Database Connectivity (JDBC)
H2 database
Creating an in-memory database
Generating XML using MarkupBuilder
Generating XML based on SQL
The Vert.x microservice platform
Adding Vert.x dependency to Ivy
Creating the web service
Summary
Other JVM languages
Oracle Nashorn
Embedding Nashorn in JVM-based projects
Running Nashorn
Jython (Python)
Differences between CPython and Jython
Running Jython
JRuby (Ruby)
Ruby on Rails and JRuby
Running JRuby
Frege (Haskell)
Calling Java code from Frege
Running Frege
Ceylon
Ceylon's module system
Running Ceylon
Summary
Quiz Answers
Chapter 3: Java
Chapter 5: Scala
Chapter 7: Clojure
Chapter 9: Kotlin
Chapter 11: Groovy
Preface
The Java Virtual Machine is a mature and very versatile platform for running software that takes full advantage of modern hardware features. While it is true that Java-based applications once could be considered slow, bloated, and extremely memory-hungry, things have improved greatly over the years. It's no coincidence that many mainstream cloud-based services and websites, which often have to serve tens of thousands users simultaneously, are powered by a JVM-based backend.
While Java is, without a doubt, the most popular language used to create applications that run on the JVM, other languages are getting more and more popular every year. This book covers five different JVM-based languages: Java, Scala, Clojure, Kotlin, and Groovy. Some of those languages are statically typed while others are dynamically typed. Likewise, this book covers both object-oriented programming languages and functional programming languages. The JVM is versatile enough to make this all possible.
By covering all these languages in a single book, you can easily compare each language with the others and, hopefully, pick your favorite language by building the sample projects.
What this book covers
Chapter 1, Java Virtual Machine, provides a high-level overview of the Java platform and the Java Virtual Machine (JVM). It describes popular use cases for applications running on the JVM, namely web applications, big data analysis, and Internet of Things (IoT). Also covered are important JVM concepts, including its just-in-time compiler, type system, and garbage collector.
Chapter 2, Developing on the Java Virtual Machine, explains the JVM in more technical detail. Covered are both the installation procedure and organization of the Java Development Kit (JDK) on major operating systems (Windows, macOS, and Linux). Also explained is the organization of the Java Class Library and instructions on how to run JVM-based applications by setting up the ClassPath.
Chapter 3, Java, covers the fundamentals of the Java language. It covers creating classes and instantiating objects based on these classes, adding methods and properties to classes, and Java's access modifiers and other modifiers. Some of the other concepts that are discussed include abstract classes, interfaces, arrays, and collections and exceptions. More advanced features such as threading and lambdas are covered as well.
Chapter 4, Java Programming, contains a step-by-step guide to creating a simple web service in the Java language. Tools that are used along the way include the Eclipse IDE, the Gradle build tool, and programming libraries such as SparkJava (a micro web service framework) and the JUnit unit testing framework.
Chapter 5, Scala, talks about the hybrid functional programming and object-oriented programming language Scala. It describes the installation procedure and the usage of the interactive shell bundled with the language. By using the interactive shell, Scala code can be entered and executed dynamically, without explicitly compiling code. Both object-oriented and functional programming in Scala are discussed.
Chapter 6, Scala Programming, contains a step-by-step guide to create a simple console-based application powered by the popular Akka toolkit. Akka is a toolkit specializing in writing scalable applications that take full advantage of modern multicore processors. Many Akka concepts, such as its actor-based system, are discussed thoroughly. To build the project, the Scala Build Tool (SBT) is used, while the ScalaTest library is used for writing unit tests.
Chapter 7, Clojure, explains the fundamentals of Clojure, a dynamic functional programming language inspired by Lisp, which is not object-oriented. Like Scala, Clojure comes with an interactive shell that can be used to enter the various examples that are provided. Agents, a technique to handle state in multithreading applications, are discussed as well.
Chapter 8, Clojure Programming, provides step-by-step guides for two smaller projects. One project is based on monads, a technique that is commonly used in functional programming languages, especially in Lisp. The second project is a web application that is powered by Luminus, a popular micro web framework for Clojure. The Leiningen build tool is used to build both the projects.
Chapter 9, Kotlin, discusses JetBrain's statically typed programming language, Kotlin. Kotlin's type system, which promises null safety, is explained. Other features that are discussed include data classes, lambdas, and inline functions. Procedural programming in Kotlin is covered as well.
Chapter 10, Kotlin Programming, contains a step-by-step guide to create a GUI-based desktop application using the JavaFX toolkit. Apache Maven is used to build the project. The Eclipse IDE's debugger is used to find and fix bugs.
Chapter 11, Groovy, covers the dynamic programming language Groovy, one of the first alternative languages that appeared on the JVM. While Groovy is primarily a dynamic language, it allows compiling statically typed code as well. Both use cases are explained and described in this chapter. Also explored is the Groovy Development Kit, an extensive library of built-in classes, which is distributed as part of the Groovy language distribution.
Chapter 12, Groovy Programming, provides a step-by-step guide to create a web service in Groovy that pulls data from an embedded database management system using the Java Database Connectivity (JDBC) standard and generates XML using classes from the Groovy Development Kit. The Vert.x framework is used to power the web service.
Appendix A, Other JVM Languages, covers five other JVM-based languages, often dialects of mainstream languages: Oracle Nashorn (JavaScript), Jython (Python), JRuby (Ruby), Frege (Haskell), and Ceylon, a statically typed language by Red Hat.
Appendix B, Quiz Answers, gives the solutions to quizes provided at the end of all the chapters.
What you need for this book
To get the most out of this book, a modern laptop or desktop computer is required, running an up-to-date version of either Windows, macOS, or Linux (preferably Ubuntu). About 4 GB of RAM memory is recommended at the minimum; more RAM is always welcome.
It is assumed that the reader has a reasonable knowledge of the operating system of their choice and is comfortable with installing programs and adding directories to a path.
Who this book is for
This book is meant for programmers who are interested in the Java Virtual Machine (JVM) and who want to learn more about the most popular programming languages that can be used for JVM development. A basic practical knowledge of a modern programming language that supports object-oriented programming (JavaScript, Python, C#, VB.NET, and C++) is assumed.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: We then call the setName method on this object instance.
A block of code is set as follows:
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
Any command-line input or output is written as follows:
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: The System Properties window appears. Click the Environment Variables... button.
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail feedback@packtpub.com, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Introduction-to-JVM-Languages. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Downloading the color images of this book
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from http://www.packtpub.com/sites/default/files/downloads/IntroductionToJVMLanguages_ColorImages.pdf.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at copyright@packtpub.com with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at questions@packtpub.com, and we will do our best to address the problem.
Java Virtual Machine
Java Virtual Machine (JVM) is a modern platform on which you can develop and deploy software. As the name implies, it was originally created to power applications written in the Java language. However, it didn't take language designers long to realize that they could not only run their languages on JVM, but also take advantage of its features and extensive class library.
Sun Microsystems released Java and the first JVM implementation in 1995. With its focus on Internet applications, Java quickly became popular. It was also designed from the ground up to run anywhere. Its initial goal was to run on set-top boxes, but when Sun Microsystems found out the market was not ready at that time yet, they decided to bring the platform to desktop computers as well. To make all those use cases possible, Sun invented their own binary executable format and called it Java bytecode. To run programs compiled to Java bytecode, a JVM implementation must be installed on the system.
This book will help you get started with five most popular languages that target JVM. By learning the language fundamentals and writing code yourself, you will be able to find the language that best suits you, your team, and your projects.
Before we dive into the Java Development Kit (JDK) and Java Class Library in the next chapter, we will look at some practical points first. With so many competing programming languages and platforms available today, it makes sense to first take a detailed look at what JVM has to offer to developers. Therefore, we will cover the following topics:
Reasons for developing on JVM
Popular use cases of JVM
Introducing JVM concepts
Java editions
Other languages on JVM
JVM implementations
It's important to note that this book focuses on JVM implementations compatible with Oracle's Java SE (Standard Edition) 8 (and higher) platform only. This version can be installed on desktop computers, servers, and many single-board computers (including all the models of the popular credit-card-sized Raspberry Pi). We will use Oracle's implementation in this book, but both the open source OpenJDK and IBM's own J9 Java SE implementations of the same version should work equally well.
The Java platform as published by Google on Android phones and tablets is not covered in this book at all. One of the reasons is that the Java version used on Android is based on an older version of Java. While progress has been made to make Android's version of the platform more up to date, it still doesn't have all the features of Oracle's Java SE 8, and it requires different compilers and tools. Another reason is that Google omitted a lot of the Java SE APIs and replaced them with their own unique, incompatible APIs. Some of the languages covered in this book can be used with Android, however. Kotlin, in particular, is a very popular choice for modern Android development. This use case will not be explored in this book, though.
Why develop on JVM?
With so many programming languages and platform options available today, why would you consider developing and deploying your next project on JVM? After all, Java, the language that JVM was originally built for, has been declared obsolete (and, ridiculously, even dead) by fans of different languages more times over the years than anyone cares to remember.
Yet, while many other programming languages have come in and gone out of the spotlight, Java has always managed to return to impressive spots, either near or lately even on top of the list of the most used languages in the world.
Let's look at some of the most important reasons why the JVM platform is so strong:
It keeps up with the modern times by adapting to market changes
The Java Class Library, the built-in library of classes, is very strong
It has an unmatched ecosystem
JVM adapts to market changes
When Java first appeared in the mid-1990s, computers had CPUs with only a single core and didn't have gigabytes