Book Image

Learn Java 17 Programming - Second Edition

By : Nick Samoylov
4 (1)
Book Image

Learn Java 17 Programming - Second Edition

4 (1)
By: Nick Samoylov

Overview of this book

Java is one of the most preferred languages among developers. It is used in everything right from smartphones and game consoles to even supercomputers, and its new features simply add to the richness of the language. This book on Java programming begins by helping you learn how to install the Java Development Kit. You’ll then focus on understanding object-oriented programming (OOP), with exclusive insights into concepts such as abstraction, encapsulation, inheritance, and polymorphism, which will help you when programming for real-world apps. Next, you’ll cover fundamental programming structures of Java such as data structures and algorithms that will serve as the building blocks for your apps with the help of sample programs and practice examples. You’ll also delve into core programming topics that will assist you with error handling, debugging, and testing your apps. As you progress, you’ll move on to advanced topics such as Java libraries, database management, and network programming and also build a sample project to help you understand the applications of these concepts. By the end of this Java book, you’ll not only have become well-versed with Java 17 but also gained a perspective into the future of this language and have the skills to code efficiently with best practices.
Table of Contents (23 chapters)
1
Part 1: Overview of Java Programming
5
Part 2: Building Blocks of Java
15
Part 3: Advanced Java

How to install and run Java

When somebody says “Java”, they may mean quite different things. They could be referring to any of the following:

  • Java programming language: A high-level programming language that allows an intent (a program) to be expressed in a human-readable format that can be translated into binary code that is executable by a computer
  • Java compiler: A program that can read a text written in the Java programming language and translate it into bytecode that can be interpreted by the Java Virtual Machine (JVM) into binary code that is executable by a computer
  • JVM: A program that reads bytecode of the compiled Java program and interprets it into binary code that is executable by a computer
  • Java Development Kit (JDK): A collection of programs (tools and utilities), including the Java compiler, the JVM, and supporting libraries, which allow the compilation and execution of a program written in the Java language

The following section walks you through the installation of the JDK of Java 17 and the basic related terms and commands.

What is the JDK and why do we need it?

As we have mentioned already, the JDK includes a Java compiler and the JVM. The task of the compiler is to read a .java file that contains the text of a program written in Java (called source code) and transform (compile) it into bytecode stored in a .class file. The JVM can then read the .class file, interpret the bytecode into binary code, and send it to the operating system for execution. Both the compiler and the JVM have to be invoked explicitly from the command line.

The hierarchy of languages used by Java programs goes like this:

  • You write Java code (.java file).
  • The compiler converts your Java code into bytecode (.class file).
  • The JVM converts the bytecode into machine-level assembly instructions (run on hardware).

Have a look at the following example:

int a = b + c;

When you write the preceding code, the compiler adds the following bytecode to the .class file:

ILOAD b
ILOAD c
IADD
ISTORE a

Write once, run anywhere is the most famous programming marketing jingle driving worldwide adoption. Oracle claims more than 10 million developers use Java, which runs on 13 billion devices. You write Java and compile it into bytecode in .class files. There is a different JVM for Windows, Mac, Unix, Linux, and more, but the same .class file works on all of them.

To support the .java file compilation and its bytecode execution, the JDK installation also includes standard Java libraries called the Java Class Library (JCL). If the program uses a third-party library, it has to be present during compilation and execution. It has to be referred from the same command line that invokes the compiler, and later when the bytecode is executed by the JVM. JCL, on the other hand, does not need to be referred to explicitly. It is assumed that the standard Java libraries reside in the default location of the JDK installation so that the compiler and the JVM know where to find them.

If you do not need to compile a Java program and would like to run only the already compiled .class files, you can download and install the Java Runtime Environment (JRE). For example, it consists of a subset of the JDK and does not include a compiler.

Sometimes, the JDK is referred to as a software development kit (SDK), which is a general name for a collection of software tools and supporting libraries that allow the creation of an executable version of source code written using a certain programming language. So, the JDK is an SDK for Java. This means it is possible to call the JDK an SDK.

You may also hear the terms Java platform and Java edition applied to the JDK. A typical platform is an operating system that allows a software program to be developed and executed. Since the JDK provides its own operating environment, it is called a platform too. An edition is a variation of a Java platform (JDK) assembled for a specific purpose. There are four Java platform editions, as listed here:

  • Java Platform SE (Java SE): This includes the JVM, JCL, and other tools and utilities.
  • Java Platform Enterprise Edition (Java EE): This includes Java SE, servers (computer programs that provide services to the applications), JCL, other libraries, code samples, tutorials, and other documentation for developing and deploying large-scale, multi-tiered, and secure network applications.
  • Java Platform Micro Edition (Java ME): This is a subset of Java SE with some specialized libraries for developing and deploying Java applications for embedded and mobile devices, such as phones, personal digital assistants, TV set-top boxes, printers, and sensors. A variation of Java ME (with its own JVM implementation) is called the Android SDK, which was developed by Google for Android programming.
  • Java Card: This is the smallest of the Java editions and is intended for developing and deploying Java applications onto small embedded devices such as smart cards. It has two editions: Java Card Classic Edition, for smart cards, (based on International Organization for Standardization (ISO) 7816 and ISO 14443 communication), and Java Card Connected Edition, which supports a web application model and Transmission Control Protocol/Internet Protocol (TCP/IP) as a basic protocol and runs on high-end secure microcontrollers.
  • So, to install Java means to install the JDK, which also means to install the Java platform on one of the listed editions. In this book, we are going to talk about and use only Java SE (which includes the JVM, JCL, and other tools and utilities necessary to compile your Java program into bytecode, interpret it into binary code, and automatically send it to your operating system for execution).

Installing Java SE

All the recently released JDKs are listed on the official Oracle page at https://www.oracle.com/java/technologies/downloads/#java17 (we will call this the installation home page for further references in later chapters).

Here are the steps that need to be followed to install Java SE:

  1. Select the Java SE tab with your operating system.
  2. Click on the link to the installer that fits your operating system and the format (extension) you are familiar with.
  3. If in doubt, click the Installation Instructions link below and read the installation instructions for your operating system.
  4. Follow the steps that correspond to your operating system.
  5. The JDK is installed successfully when the java -version command on your computer displays the correct Java version, as demonstrated in the following example screenshot:

Commands, tools, and utilities

If you follow the installation instructions, you may have noticed a link (Installed Directory Structure of the JDK) given under Table of Contents. This brings you to a page that describes the location of the installed JDK on your computer and the content of each directory of the JDK root directory. The bin directory contains all executables that constitute Java commands, tools, and utilities. If the bin directory is not added to the PATH environment variable automatically, consider doing so manually so that you can launch a Java executable from any directory.

In the previous section, we have already demonstrated the java -version Java command. A list of the other Java executables available (commands, tools, and utilities) can be found in the Java SE documentation (https://www.oracle.com/technetwork/java/javase/documentation/index.html) by clicking the Java Platform Standard Edition Technical Documentation site link, and then the Tools Reference link on the next page. You can learn more about each executable tool by clicking its link.

You can also run each of the listed executables on your computer using one of the following options:

-?, -h, --help, or -help

These will display a brief description of the executable and all its options.

The most important Java commands are listed here:

  • javac: This reads a .java file, compiles it, and creates one or more corresponding .class files, depending on how many Java classes are defined in the .java file.
  • java: This executes a .class file.

These are the commands that make programming possible. Every Java programmer must have a good understanding of their structure and capabilities, but if you are new to Java programming and use an IDE (see the How to install and run an IDE section), you do not need to master these commands immediately. A good IDE hides them from you by compiling a .java file automatically every time you make a change to it. It also provides a graphical element that runs the program every time you click it.

Another very useful Java tool is jcmd. This facilitates communication with, and diagnosis of, any currently running Java processes (JVM) and has many options. But in its simplest form, without any option, it lists all currently running Java processes and their process IDs (PIDs). You can use it to see whether you have runaway Java processes. If you have, you can then kill such a process using the PID provided.