Movatterモバイル変換


[0]ホーム

URL:


Packt
Search iconClose icon
Search icon CANCEL
Subscription
0
Cart icon
Your Cart(0 item)
Close icon
You have no products in your basket yet
Save more on your purchases!discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Profile icon
Account
Close icon

Change country

Modal Close icon
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timerSALE ENDS IN
0Days
:
00Hours
:
00Minutes
:
00Seconds
Home> Programming> Object Oriented Programming> Learn Java 17 Programming
Learn Java 17 Programming
Learn Java 17 Programming

Learn Java 17 Programming: Learn the fundamentals of Java Programming with this updated guide with the latest features , Second Edition

Arrow left icon
Profile Icon Nick Samoylov
Arrow right icon
$31.99$35.99
Full star iconFull star iconFull star iconHalf star iconEmpty star icon3.3(12 Ratings)
eBookJul 2022748 pages2nd Edition
eBook
$31.99 $35.99
Paperback
$44.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$31.99 $35.99
Paperback
$44.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature iconInstant access to your Digital eBook purchase
Product feature icon Download this book inEPUB andPDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature iconDRM FREE - Read whenever, wherever and however you want
Product feature iconAI Assistant (beta) to help accelerate your learning
OR

Contact Details

Modal Close icon
Payment Processing...
tickCompleted

Billing Address

Table of content iconView table of contentsPreview book icon Preview Book

Learn Java 17 Programming

Chapter 1: Getting Started with Java 17

This chapter is about how to start learning Java 17 and Java in general. We will begin with the basics, first explaining what Java is and its main terms, followed by how to install the necessary tools to write and run (execute) a program. In this respect, Java 17 is not much different from the previous Java versions, so this chapter’s content applies to the older versions too.

We will describe and demonstrate all the necessary steps for building and configuring a Java programming environment. This is the bare minimum that should have on your computer to start programming. We also describe the basic Java language constructs and illustrate them with examples that can be executed immediately.

The best way to learn a programming language—or any language, for that matter—is to use it, and this chapter guides readers on how they can do this with Java. We will cover the following topics in this chapter:

  • How to install and run Java
  • How to install and run anintegrated development environment (IDE)
  • Java primitive types and operators
  • String types and literals
  • Identifiers (IDs) and variables
  • Java statements

Technical requirements

To be able to execute the code examples provided in this chapter, you will need the following:

  • A computer with a Microsoft Windows, Apple macOS, or Linux operating system
  • Java SE version 17 or later
  • An IDE or your preferred code editor

The instructions for how to set up a JavaStandard Edition (SE) and IntelliJ IDEA editor will be provided later in this chapter. The files with the code examples for this chapter are available on GitHub in thehttps://github.com/PacktPublishing/Learn-Java-17-Programming.git repository, in theexamples/src/main/java/com/packt/learnjava/ch01_start folder.

How to install and run Java

Whensomebody 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 programthat can read a text written in the Java programming language and translate it into bytecode that can be interpreted by theJava Virtual Machine (JVM) intobinary 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): Acollection 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 basicrelated terms and commands.

What is the JDK and why do we need it?

As we havementioned 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 Javalibraries called theJava 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 theJava Runtime Environment (JRE). Forexample, it consists of a subset of the JDK and does not include a compiler.

Sometimes, the JDK is referred to as asoftware 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 mayalso hear the termsJava platform andJava 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 includesthe 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 somespecialized 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: Thisis 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 onInternational Organization for Standardization (ISO)7816 and ISO14443 communication), and Java Card Connected Edition, which supports a web application model andTransmission Control Protocol/Internet Protocol (TCP/IP) as a basic protocoland 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 useonlyJava 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 athttps://www.oracle.com/java/technologies/downloads/#java17 (we will call this theinstallation 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 theInstallation 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 thejava -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 underTable 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. Thebin directory contains all executables that constitute Java commands, tools, and utilities. If thebin directory is not added to thePATH 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 thejava -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 theJava Platform Standard Edition Technical Documentation site link, and then theTools 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 theHow 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 fileautomatically 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 theirprocess IDs (PIDs). Youcan use it to see whether you have runaway Java processes. If you have, you can then kill such a process using the PID provided.

How to install and run an IDE

What used to be just a specialized editor that allowed checking the syntax of a written program the same way a Word editor checks the syntax of an English sentence gradually evolved into an IDE. This bears its main function in the name. It integrates all the tools necessary for writing, compiling, and then executing a programunder onegraphical user interface (GUI). Using the power of Java compiler, the IDE identifies syntax errors immediately and then helps to improve code quality by providing context-dependent help and suggestions.

Selecting an IDE

There areseveral IDEs available for a Java programmer, such as NetBeans, Eclipse, IntelliJ IDEA, BlueJ, DrJava, JDeveloper, JCreator, jEdit, JSource, and jCRASP, to name a few. You can read a review of the top Java IDEs and details about each by following this link:https://www.softwaretestinghelp.com/best-java-ide-and-online-compilers. The most popular ones are NetBeans, Eclipse, and IntelliJ IDEA.

NetBeans development started in 1996 as a Java IDE student project at Charles University in Prague. In 1999, the project and the company created around the project were acquired by Sun Microsystems. After Oracle acquired Sun Microsystems, NetBeans became open source, and many Java developers have since contributed to the project. It was bundled with JDK 8 and became an official IDE for Java development. In 2016, Oracle donated it to the Apache Software Foundation.

There is a NetBeans IDE for Windows, Linux, Mac, and Oracle Solaris. It supports multiple programming languages and can be extended with plugins. As of the time of writing, NetBeans is bundled only with JDK 8, but NetBeans 8.2 can work with JDK 9 too and uses features introduced with JDK 9 such as Jigsaw, for example. Onnetbeans.apache.org, you can read more about the NetBeans IDE and download the latest version, which is 12.5 as of the time of this writing.

Eclipse is the most widely used Java IDE. The list of plugins that add new features to the IDE is constantly growing, so it is not possible to enumerate all the IDE’s capabilities. The Eclipse IDE project has been developedsince 2001 asopen source software (OSS). A non-profit, member-supported corporation Eclipse Foundation was created in 2004 to provide the infrastructure (version control systems (VCSs), code review systems, build servers, download sites, and so on) and a structured process. None of the 30-something employees of the Eclipse Foundation is working on any of the 150 Eclipse-supported projects.

The sheer number and variety of Eclipse IDE plugins create a certain challenge for a beginner because you have to find your way around different implementations of the same—or similar—features that can, on occasion, be incompatible and may require deep investigation, as well as a clear understanding of all the dependencies. Nevertheless, the Eclipse IDE is very popular and has solid community support. You can read about the Eclipse IDE and download the latest release fromwww.eclipse.org/ide.

IntelliJ IDEA has two versions: a paid one and a free community edition. The paid version is consistently ranked as the best Java IDE, but the community edition is listed among the three leading Java IDEs too. The JetBrains software company that develops the IDE has offices in Prague, Saint Petersburg, Moscow, Munich, Boston, and Novosibirsk. The IDE is known for its deep intelligence that is “giving relevant suggestions in every context: instant and clever code completion, on-the-fly code analysis, and reliable refactoring tools”, as stated by the authors while describing the product on their website (www.jetbrains.com/idea). In theInstalling and configuring IntelliJ IDEA section, we will walk you through the installation and configuration of IntelliJ IDEA’s community edition.

Installing and configuring IntelliJ IDEA

Theseare thesteps you need to follow in order to download and install IntelliJ IDEA:

  1. Download an installer of the IntelliJ community edition fromwww.jetbrains.com/idea/download.
  2. Launch the installer and accept all the default values.
  3. Select.java on theInstallation Options screen. We assume you have installed the JDK already, so you do not check theDownload and install JRE option.
  4. The last installation screen has aRun IntelliJ IDEA checkbox that you can check to start the IDE automatically. Alternatively, you can leave the checkbox unchecked and launch the IDE manually once the installation is complete.
  5. When the IDE starts for the first time, it provides you with anImport IntelliJ IDEA settings option. Check theDo not import settings checkbox if you have not used IntelliJ IDEA before.
  6. The next couple of screens ask whether you accept theJetBrains Privacy Policy and whether you would like to pay for the license or prefer to continue to use the free community edition or free trial (this depends on the particular download you get).
  7. Answer the questions whichever way you prefer, and if you accept the privacy policy, theCustomize IntelliJ IDEA screen will ask you to choose a theme:white (IntelliJ) ordark (Darcula).
  8. Accept the default settings.
  9. If you decideto change the set values, you can do so later byselecting from the topmost menu,File |Settings, on Windows, orPreferences on Linux and macOS.

Creating a project

Before youstart writing your program, you need to create a project. There are several ways to create a project in IntelliJ IDEA, which is the same for any IDE, as follows:

  1. New Project: This creates a new project from scratch.
  2. Open: This facilitates reading of the existing project from the filesystem.
  3. Get from VCS: This facilitates reading of the existing project from the VCS.

In this book, we will walk you through the first option only—using the sequence of guided steps provided by the IDE. Options2 and3 include many settings that are automatically set by importing an existing project that has those settings. Once you have learned how to create a new project from scratch, the other ways to bring up a project in the IDE will be very easy for you.

Start by clicking theNew Project link and proceed further as follows:

  1. SelectMaven in the left panel and a value forProject SDK (Java Version 17, if you have installed JDK 17 already), and clickNext.
  2. Maven is a project configuration tool whose primary function is to manage project dependencies. We will talk about it shortly. For now, we will use its other responsibility: to define and hold the project code identity using threeArtifact Coordinates properties (see next).
  3. Type the project name—for example,myproject.
  4. Select the desired project location in theLocation field setting (this is where your new code will reside).
  5. ClickArtifact Coordinates, and the following properties will appear:
    • GroupId: This is the base package name that identifies a group of projects within an organization or an open source community. In our case, l et's typecom.mywork.
    • ArtifactId: To identify a particular project within the group. Leave it asmyproject.
    • Version: To identify the version of the project. Leave it as1.0-SNAPSHOT.

The main goal is to make the identity of a project unique among all projects in the world. To help avoid aGroupId clash, the convention requires that you start building it from the organization domain name in reverse. For example, if a company has acompany.com domain name, theGroupId properties of its projects should start withcom.company. That is why for this demonstration we usecom.mywork, and for the code in this book, we use thecom.packt.learnjavaGroupID value.

  1. ClickFinish.
  2. You will see the following project structure and generatedpom.xml file:

Now, if somebody would like to use the code of your project in their application, they would refer toit by the three values shown, and Maven (if they use it) will bring it in (if you upload your project to the publicly shared Maven repository, of course). Read more about Maven athttps://maven.apache.org/guides. Another function of theGroupId value is to define the root directory of the folders tree that holds your project code. Thejava folder undermain will hold the application code, while thejava folder undertest will hold the test code.

Let’s create our first program using the following steps:

  1. Right-click onjava, selectNew, and then clickPackage, as illustrated in the following screenshot:
  1. In theNew Package window provided, typecom.mywork.myproject and pressEnter.

You shouldsee in the left panel the following set of new folders:

  1. Right-click oncom.mywork.myproject, selectNew, and then clickJava Class, as illustrated in the following screenshot:
  1. In theinput window provided, typeHelloWorld, as follows:
  1. PressEnter and you will see your first Java class,HelloWorld, created in thecom.mywork.myproject package, as illustrated in the following screenshot:

The package reflects the Java class location in the filesystem. We will talk about this more inChapter 2,Java Object-Oriented Programming (OOP). Now, in order to run a program, we create amain() method. If present, this method can be executed to serve as an entry point into the application. It has a certain format, as shown here:

This has to have the following attributes:

  • public: Freely accessible from outside the package
  • static: Should be able to be called without creating an object of the class it belongs to

It should also have the following:

  • Returnvoid (nothing)

Accept aString array as an input, orvarargs, as we have done. We will talk aboutvarargs inChapter 2,Java Object-Oriented Programming (OOP). For now, suffice to say thatString[] args andString... args essentially define the same input format.

We explain how to run themain class using a command line in theExecuting examples from the command line section. You can read more about Java command-line arguments in the official Oracle documentation athttps://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html. It is also possible to run the examples from IntelliJ IDEA.

Notice the two green triangles to the left in the screenshot shown next. By clicking any of them, you can execute themain() method. For example, let’s displayHello, world!.

In order to dothis, type the following line inside themain() method:

System.out.println("Hello, world!");

The following screenshot shows how the program should look afterward:

Then, click one of the green triangles, and you should get the following output in the Terminal area:

From now on, every time we are going to discuss code examples, we will run them the same way, by using themain() method. While doing this, we will not capture a screenshot but put the result in comments, because such a style is easier to follow. For example, the following code snippet displays how the previous code demonstration would look in this style:

System.out.println("Hello, world!"); //prints: Hello, world!

It is possible to add a comment (any text) to the right of the code line separated by a double slash//. The compiler does not read this text and just keeps it as it is. The presence of a comment does not affect performance and is used to explain the programmer’s intent to humans.

Importing a project

We are goingto demonstrate project importing using the source code for this book. We assume that you have Maven installed (https://maven.apache.org/install.html) on your computer and that you have Git (https://gist.github.com/derhuerst/1b15ff4652a867391f03) installed too, and can use it. We also assume that you have installed JDK 17, as was described in theInstalling Java SE section.

To import the project with the code examples for this book, follow these steps:

  1. Go to the source repository (https://github.com/PacktPublishing/Learn-Java-17-Programming) and click theCode drop-down menu, as shown in the following screenshot:
  1. Copy theprovidedUniform Resource Locator (URL) (click thecopy symbol to the right of the URL), as illustrated in the following screenshot:
  1. Select a directory on your computer where you would like the source code to be placed and then run thegit clone https://github.com/PacktPublishing/Learn-Java-17-Programming.git Git command and observe similar output to that shown in the following screenshot:
  1. A newLearn-Java-17-Programming folder is created.

Alternatively, instead of cloning, you can download the source as a.zip file using theDownload ZIP link shown in the screenshot just before. Unarchive the downloaded source in a directory on your computer where you would like the source code to be placed, and then rename the newly created folder by removing the-master suffix from its name, making sure that the folder’s name isLearn-Java-17-Programming.

  1. The newLearn-Java-17-Programming folder contains the Maven project with all the source code from this book. If you prefer, you can rename this folder however you like. In our case, we renamed itLearnJava for brevity.
  2. Now, run IntelliJ IDEA and clickOpen. Navigate to the location of the project and select the just-created folder (LearnJava, in our case), then click theOpen button.
  3. If the following popup shows in the bottom-right corner, clickLoad:
  1. Also, clickTrust project..., as shown in the following screenshot:
  1. Then, click theTrust Project button on the following popup:
  1. Now, go toProject Structure (cogwheel symbol in the upper-right corner) and make sure that Java 17 is selected as an SDK, as shown in the following screenshot:
  1. ClickApply and make sure that the defaultProject SDK is set to Javaversion 17 andProject language level is set to17, as in the following screenshot:
  1. ClickApply and then (optionally) remove theLearnJava module by selecting it and clicking"-", as follows:
  1. Confirm theLearnJava module removal on the popup by clickingYes, as follows:
  1. Here's how the final list of modules should look:

ClickOK in thebottom-right corner and get back to your project. Click examples in the left pane and continue going down the source tree until you see the following list of classes:

Click on the green arrow in the right pane and execute themain() method of any class you want. For example, let’s execute themain() method of thePrimitiveTypes class. The resultyou will be able to see in theRun window should be similar to this:

Executing examples from the command line

To execute theexamples from the command line, go to theexamples folder, where thepom.xml file is located, and run themvn clean package command. If the command is executed successfully, you can run anymain() method in any of the programs in theexamples folder from the command line. For example, to execute themain() method in theControlFlow.java file, run the following command as one line:

java -cp target/examples-1.0-SNAPSHOT.jar   com.packt.learnjava.ch01_start.ControlFlow

You will see the following results:

This way, you can run any class that has themain() method in it. The content of themain() methodwill be executed.

Java primitive types and operators

With all the main programming tools in place, we can start talking about Java as a language. The language syntax is defined by theJava Language Specification, which you can find athttps://docs.oracle.com/javase/specs. Don’t hesitate to refer to it every time you need some clarification—it is not as daunting as many people assume.

All the values in Java are divided into two categories: reference types and primitive types. We start with primitive types and operators as the natural entry point to any programming language. In this chapter, we will also discuss one reference type calledString (see theString types and literals section).

All primitive types can be divided into two groups: Boolean types and numeric types.

Boolean types

There areonlytwo Boolean type values in Java:true andfalse. Such a value can only be assigned to a variable of aboolean type, as in the following example:

boolean b = true;

Aboolean variable is typically used in control flow statements, which we are going to discuss in theJava statements section. Here is one example:

boolean b = x > 2;
if(b){
    //do something
}

In the preceding code, we assign to theb variable the result of the evaluation of thex > 2 expression. If the value ofx is greater than2, theb variable gets the assigned value,true. Then, the code inside the braces ({}) is executed.

Numeric types

Java numerictypes form two groups: integral types (byte,char,short,int, andlong) and floating-point types (float anddouble).

Integral types

Integral types consume the following amount of memory:

  • byte: 8 bits
  • char: 16 bits
  • short: 16 bits
  • int: 32 bits
  • long: 64 bits

Thechar type is an unsigned integer that can hold a value (called a code point) from 0 to 65,535 inclusive. It represents a Unicode character, which means there are 65,536 Unicode characters. Here are three records from the basic Latin list of Unicode characters:

The following code demonstrates the properties of thechar type (execute themain() method ofthecom.packt.learnjava.ch01_start.PrimitiveTypes class—see thecharType() method):

char x1 = '\u0032';
System.out.println(x1);  //prints: 2
char x2 = '2';
System.out.println(x2);  //prints: 2
x2 = 65;
System.out.println(x2);  //prints: A
char y1 = '\u0041';
System.out.println(y1);  //prints: A
char y2 = 'A';
System.out.println(y2);  //prints: A
y2 = 50;
System.out.println(y2);  //prints: 2
System.out.println(x1 + x2);  //prints: 115
System.out.println(x1 + y1);  //prints: 115

The last two lines from the preceding code example explain why thechar type is considered an integral type becausechar values can be used in arithmetic operations. In such a case, eachchar value is represented by its code point.

The range of values of other integral types is shown here:

  • byte: from -128 to 127 inclusive
  • short: from -32,768 to 32,767 inclusive
  • int: from -2.147.483.648 to 2.147.483.647 inclusive
  • long: from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 inclusive

Youcanalways retrieve the maximum and minimum value of each primitive type from a corresponding Java constant, as follows (execute themain() method of thecom.packt.learnjava.ch01_start.PrimitiveTypes class—see theminMax() method):

System.out.println(Byte.MIN_VALUE);      //prints: -128
System.out.println(Byte.MAX_VALUE);      //prints:  127
System.out.println(Short.MIN_VALUE);     //prints: -32768
System.out.println(Short.MAX_VALUE);     //prints:  32767
System.out.println(Integer.MIN_VALUE);   //prints: -2147483648
System.out.println(Integer.MAX_VALUE);   //prints:  2147483647
System.out.println(Long.MIN_VALUE);      
                                 //prints: -9223372036854775808
System.out.println(Long.MAX_VALUE);
                                  //prints: 9223372036854775807
System.out.println((int)Character.MIN_VALUE); //prints: 0
System.out.println((int)Character.MAX_VALUE); //prints: 65535

The construct (int) in the last two lines is an example of cast operator usage. It forces the conversion of a value from one type to another in cases where such a conversion is not always guaranteed to be successful. As you can see from our examples, some types allow bigger values than other types. But a programmer may know that the value of a certain variable can never exceed the maximum value of the target type, and the cast operator is the way the programmer can force their opinion on the compiler. Otherwise, without a cast operator, the compiler would raise an error and would not allow the assignment. However, the programmer may be mistaken and the value may become bigger. Insuch a case, a runtime error will be raised during execution time.

There are types that, in principle, cannot be cast to other types, though, or at least not to all types—for example, a Boolean type value cannot be cast to an integral type value.

Floating-point types

There are two types in this group of primitive types—float anddouble. These consume the following amount of memory:

  • float: 32 bit
  • double: 64 bit

Their positive maximum and minimum possible values are shown here (execute themain() method of thecom.packt.learnjava.ch01_start.PrimitiveTypes class—see theminMax() method):

System.out.println(Float.MIN_VALUE);  //prints: 1.4E-45
System.out.println(Float.MAX_VALUE);  //prints: 3.4028235E38
System.out.println(Double.MIN_VALUE); //prints: 4.9E-324
System.out.println(Double.MAX_VALUE);
                               //prints: 1.7976931348623157E308

The maximum and minimum negative values are the same as those just shown, only with a minus sign (-) in front of them. So, effectively, theFloat.MIN_VALUE andDouble.MIN_VALUE values are not the minimal values, but the precision of the corresponding type. A zero value can be either 0.0 or -0.0 for each of the floating-point types.

A special feature of the floating-point type is the presence of a dot (.) that separates integer and fractional parts of the number. By default, in Java, a number with a dot is assumed to be adouble type. For example, the following is assumed to be adoublevalue:

42.3

This means thatthe following assignment causes a compilation error:

float f = 42.3;

To indicate that you would like it to be treated as afloat type, you need to add eitherf orF. For example, the following assignments do not cause an error (execute themain() method of thecom.packt.learnjava.ch01_start.PrimitiveTypes class—see thecasting() method):

float f = 42.3f;
float d = 42.3F;
double a = 42.3f;
double b = 42.3F;
float x = (float)42.3d;
float y = (float)42.3D;

As you may have noticed from the preceding example,d andD indicate adouble type, but we were able to cast them to thefloat type because we are confident that42.3 iswell inside the range of possiblefloat-type values.

Default values of primitive types

In some cases, a variable has to be assigned a value even when a programmer did not want to do that. We will talk about such cases inChapter 2,Java Object-Oriented Programming (OOP). The default primitive type value in such cases is outlined here:

  • byte,short,int, andlong types have a default value of 0.
  • Thechar type has a default value of\u0000, with the code point 0.
  • float anddouble types have a default value of 0.0.
  • Theboolean type has a default value offalse.

Literals of primitive types

The representationof a value is called a literal. Theboolean type has two literals:true andfalse. Literals ofbyte,short,int, andlong integral types have anint type by default, as illustrated here:

byte b = 42;
short s = 42;
int i = 42;
long l = 42;

In addition, to indicate a literal of along type, you can append the letterl orL to the end, like this:

long l1 = 42l;
long l2 = 42L;

The letterl can be easily confused with the number1, so usingL (instead ofl) for this purpose is a good practice.

So far, we have expressed integral literals in a decimal number system. Meanwhile, literals ofbyte,short,int, andlong types can also be expressed in binary (base 2, digits 0-1), octal (base 8, digits 0-7), and hexadecimal (base 16, digits 0-9, and a-f) number systems. A binary literal starts with 0b (or 0B), followed by the value expressed in a binary system. For example, the decimal 42 is expressed as 101010 = 2^0*0 + 2^1*1 + 2^2*0 + 2^3 *1 + 2^4 *0 + 2^5 *1 (we start from the right 0). An octal literal starts with 0, followed by the value expressed in an octal system, so 42 is expressed as 52 = 8^0*2+ 8^1*5. A hexadecimal literal starts with 0x (or with 0X), followed by a value expressed in a hexadecimal system. So, 42 is expressed as 2a = 16^0*a + 16^1*2 because, in the hexadecimal system, the symbolsa tof (orA toF) map to the decimal values 10 to 15. Hereis the demonstration code (execute themain() method of thecom.packt.learnjava.ch01_start.PrimitiveTypes class—see theliterals() method):

int i = 42;
System.out.println(Integer.toString(i, 2));       // 101010
System.out.println(Integer.toBinaryString(i));    // 101010
System.out.println(0b101010);                     // 42
System.out.println(Integer.toString(i, 8));       // 52
System.out.println(Integer.toOctalString(i));     // 52
System.out.println(052);                           // 42
System.out.println(Integer.toString(i, 10));       // 42
System.out.println(Integer.toString(i));           // 42
System.out.println(42);                            // 42
System.out.println(Integer.toString(i, 16));       // 2a
System.out.println(Integer.toHexString(i));        // 2a
System.out.println(0x2a);                          // 42

As you can see, Java provides methods that convert decimal system values to systems with different bases. All these expressions of numeric values are called literals.

One feature of numeric literals makes them human-friendly. If the number is large, it is possible to break it into triples separated by an underscore (_) sign. Observe the following, for example:

int i = 354_263_654;
System.out.println(i);  //prints: 354263654
float f = 54_436.98f;
System.out.println(f);  //prints: 54436.98
long l = 55_763_948L;
System.out.println(l);  //prints: 55763948

The compiler ignores an embedded underscore sign.

Thechar type hastwo kinds of literals: a single character or an escape sequence. We have seen examples ofchar-type literals when discussing numeric types, and you can see some others here:

char x1 = '\u0032';
char x2 = '2';
char y1 = '\u0041';
char y2 = 'A';

As you can see, the character has to be enclosed in single quotes.

An escape sequence starts with a backslash (\) followed by a letter or another character. Here is a full list of escape sequences:

  • \b: backspace BS, Unicode escape\u0008
  • \t: horizontal tab HT, Unicode escape\u0009
  • \n: line feed LF, Unicode escape\u000a
  • \f: form feed FF, Unicode escape\u000c
  • \r: carriage return CR, Unicode escape\u000d
  • \”: double quote “, Unicode escape\u0022
  • \’: single quote ‘, Unicode escape\u0027
  • \\: backslash \, Unicode escape\u005c

From the eight escape sequences, only the last three are represented by a symbol. They are used when this symbol cannot be otherwise displayed. Observe the following, for example:

System.out.println("\"");   //prints: "
System.out.println('\'');   //prints: '
System.out.println('\\');   //prints: \

The rest are usedmore as control codes that direct the output device to do something, as in the following example:

System.out.println("The back\bspace");
                                        //prints: The backspace
System.out.println("The horizontal\ttab");
                                   //prints: The horizontal tab
System.out.println("The line\nfeed");
                                        //prints: The line feed
System.out.println("The form\ffeed");      
                                        //prints: The form feed
System.out.println("The carriage\rreturn");//prints: return

As you can see,\bdeletes a previous symbol,\t inserts a tab space,\n breaks the line and begins the new one,\f forces the printer to eject the current page and to continue printingat the top of another, and\r starts the current line anew.

New compact number format

Thejava.text.NumberFormat class presents numbers in various formats. It also allows formats to be adjusted to those provided, including locales. A new feature added to this class in Java 12 is called a compact or short number format.

It represents a number in a locale-specific, human-readable form. Observe the following, for example (execute themain() method of thecom.packt.learnjava.ch01_start.PrimitiveTypes class—see thenewNumberFormat() method):

NumberFormat fmt = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
System.out.println(fmt.format(42_000));          //prints: 42K
System.out.println(fmt.format(42_000_000));      //prints: 42M
NumberFormat fmtP = NumberFormat.getPercentInstance();
System.out.println(fmtP.format(0.42));          //prints: 42%

As you can see, to access this capability, you have to acquire a particular instance of theNumberFormat class, sometimes based on the locale and style provided.

Operators

There are 44 operators in Java. These are listed in the following table:

We will not describe the not-often-used&=,|=,^=,<<=,>>=,>>>= assignment operators and bitwise operators, but you can read about them in the Java specification (https://docs.oracle.com/javase/specs). Arrow (->) and method reference (::) operators will be described inChapter 14,Java Standard Streams. Thenew instance creation operator, the. field access/method invocation operator, and theinstanceof type comparison operator will be discussed inChapter 2,Java Object-Oriented Programming (OOP). As for the cast operator, we have already described it in theIntegral types section.

Arithmetic unary (+ and -) and binary (+, -, *, /, and %) operators

Mostof the arithmeticoperators and positive and negative signs (unary operators) are quite familiar to us. The modulus operator (%) divides the left-hand operand by the right-hand operand and returns the remainder, as follows (execute themain() method of thecom.packt.learnjava.ch01_start.Operators class—see theintegerDivision() method:

int x = 5;
System.out.println(x % 2);   //prints: 1

It is also worth mentioning that the division of two integer numbers in Java loses the fractional part because Java assumes the result should be an integer number2, as follows:

int x = 5;
System.out.println(x / 2);   //prints: 2

If you need the fractional part of the result to be preserved, convert one of the operands into a floating-point type. Here are a few ways (among many) in which to do this:

int x = 5;
System.out.println(x / 2.);           //prints: 2.5
System.out.println((1. * x) / 2);     //prints: 2.5
System.out.println(((float)x) / 2);   //prints: 2.5
System.out.println(((double) x) / 2); //prints: 2.5

Increment and decrement unary operators (++ and --)

The++ operator increases thevalue of anintegral type by 1, while the-- operator decreases it by 1. If placed before the variable (prefix), it changes its value by 1 before the variable value is returned. But when placed after the variable (postfix), it changes its value by 1 after the variable value is returned. Here are a few examples (execute themain() method of thecom.packt.learnjava.ch01_start.Operators class—see theincrementDecrement() method):

int i = 2;
System.out.println(++i);   //prints: 3
System.out.println(i);     //prints: 3
System.out.println(--i);   //prints: 2
System.out.println(i);     //prints: 2
System.out.println(i++);   //prints: 2
System.out.println(i);     //prints: 3
System.out.println(i--);   //prints: 3
System.out.println(i);     //prints: 2

Equality operators (== and !=)

The== operatormeansequals, while the!= operator means not equals. They are used to compare values of the same type and return atrue Boolean value if the operand’s values areequal, orfalse otherwise. Observe the following, for example (execute themain() method of thecom.packt.learnjava.ch01_start.Operators, class—see theequality() method):

int i1 = 1;
int i2 = 2;
System.out.println(i1 == i2);        //prints: false
System.out.println(i1 != i2);        //prints: true
System.out.println(i1 == (i2 - 1));  //prints: true
System.out.println(i1 != (i2 - 1));  //prints: false

Exercise caution, though, while comparing values of floating-point types, especially when you compare the results of calculations. Using relational operators (<,>,<=, and>=) in such cases is much more reliable, because calculations such as 1/3—for example—resultin a never-ending fractional part 0.33333333... and ultimately depend on precision implementation (a complex topic that is beyond the scope of this book).

Relational operators (<, >, <=, and >=)

Relational operators compare values and return a Boolean value. Observe the following, for example (execute themain() method of thecom.packt.learnjava.ch01_start.Operators class—see therelational() method):

int i1 = 1;
int i2 = 2;
System.out.println(i1 > i2);         //prints: false
System.out.println(i1 >= i2);        //prints: false
System.out.println(i1 >= (i2 - 1));  //prints: true
System.out.println(i1 < i2);         //prints: true
System.out.println(i1 <= i2);        //prints: true
System.out.println(i1 <= (i2 - 1));  //prints: true
float f = 1.2f;
System.out.println(i1 < f);          //prints: true

Logical operators (!, &, and |)

Logicaloperators can be defined as follows:

  • The! binary operator returnstrue if the operand isfalse; otherwise, it returnsfalse.
  • The& binary operator returnstrue if both of the operands aretrue.
  • The| binary operator returnstrue if at least one of the operands istrue.

Here is an example (execute themain() method of thecom.packt.learnjava.ch01_start.Operators class—see thelogical() method):

boolean b = true;
System.out.println(!b);    //prints: false
System.out.println(!!b);   //prints: true
boolean c = true;
System.out.println(c & b); //prints: true
System.out.println(c | b); //prints: true
boolean d = false;
System.out.println(c & d); //prints: false
System.out.println(c | d); //prints: true

Conditional operators (&&, ||, and ? :)

The&& and|| operatorsproduce thesame results as the& and| logical operators we have just demonstrated, as follows (execute themain() method of thecom.packt.learnjava.ch01_start.Operators class—see theconditional() method):

boolean b = true;
boolean c = true;
System.out.println(c && b); //prints: true
System.out.println(c || b); //prints: true
boolean d = false;
System.out.println(c && d); //prints: false
System.out.println(c || d); //prints: true

The difference is that the&& and|| operators do not always evaluate the second operand. For example, in the case of the&& operator, if the first operand isfalse, the second operand is not evaluated because the result of the whole expression will befalse anyway. Similarly, in the case of the|| operator, if the first operand istrue, the whole expression will be clearly evaluated totrue without evaluating the second operand. We can demonstrate this in the following code snippet:

int h = 1;
System.out.println(h > 3 && h++ < 3);  //prints: false
System.out.println(h);                //prints: 2
System.out.println(h > 3 && h++ < 3); //prints: false
System.out.println(h);                //prints: 2

The? : operator is called a ternary operator. It evaluates a condition (before the? sign), and if it results intrue, assigns to a variable the value calculated by the first expression (between the? and: signs); otherwise, it assigns a value calculated by the second expression (after the: sign), as illustrated in the following code snippet:

int n = 1, m = 2;
float k = n > m ? (n * m + 3) : ((float)n / m);
System.out.println(k);           //prints: 0.5

Assignment operators (=, +=, -=, *=, /=, and %=)

The= operator just assigns a specified value to a variable, like this:

x = 3;

Other assignment operators calculate a new value before assigning it, as follows:

  • x += 42 assigns tox the result of thex = x + 42 addition operation.
  • x -= 42 assigns tox the result of thex = x - 42 subtraction operation.
  • x *= 42 assigns tox the result of thex = x * 42 multiplication operation.
  • x /= 42 assigns tox the result of thex = x / 42 division operation.
  • x %= 42 assigns the remainder of thex = x + x % 42 division operation.

Here is how these operators work (execute themain() method of thecom.packt.learnjava.ch01_start.Operators class—see theassignment() method):

float a = 1f;
a += 2;
System.out.println(a); //prints: 3.0
a -= 1;
System.out.println(a); //prints: 2.0
a *= 2;
System.out.println(a); //prints: 4.0
a /= 2;
System.out.println(a); //prints: 2.0
a %= 2;
System.out.println(a); //prints: 0.0

String types and literals

We have justdescribed theprimitive value types of the Java language. All the other value types in Java belong to a category of reference types. Each reference type is a more complex construct than just a value. It is described by a class, which serves as a template for creating an object, and a memory area that contains values and methods (the processing code) defined in the class. An object is created by thenew operator. We will talk about classes and objects in more detail inChapter 2,Java Object-Oriented Programming (OOP).

In this chapter, we will talk about one of the reference types calledString. It is represented by thejava.lang.String class, which belongs, as you can see, to the most foundational package of the JDK,java.lang. The reason we’re introducing theString class so early is that it behaves in some respects very similar to primitive types, despite being a reference type.

A reference type is so-called because, in the code, we do not deal with values of this type directly. A value of a reference type is more complex than a primitive-type value. It is called an object and requires more complex memory allocation, so a reference-type variable contains a memory reference. It points (refers) to the memory area where the object resides, hence the name.

This nature of the reference type requires particular attention when a reference-type variable is passed into a method as a parameter. We will discuss this in more detail inChapter 3,Java Fundamentals. For now, we will see howString, being a reference type, helps to optimize memory usage by storing eachString value only once.

String literals

TheString class represents character strings in Java programs. We have seen several such strings. We have seenHello, world!, for example. That is aString literal.

Another example of a literal isnull. Any reference class can refer to anull literal. It represents a reference value that does not point to any object. In the case of aString type, it looks like this:

String s = null;

But a literal that consists of characters enclosed in double quotes ("abc","123", and"a42%$#", for example) can only be of aString type. In this respect, theString class, being a reference type, has something in common with primitive types. AllString literals are stored in a dedicated section of memory called a string pool, and two literals are equally spelled to represent the same value from the pool (execute themain() method of thecom.packt.learnjava.ch01_start.StringClass class—see thecompareReferences() method):

String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);    //prints: true
System.out.println("abc" == s1); //prints: true

The JVM authors have chosen such an implementation to avoid duplication and improve memory usage. The previous code examples look very much like operations involving primitive types, don’t they? But when aString object is created using anew operator, the memory for the new object is allocated outside the string pool, so references of twoString objects—or any other objects, for that matter—are always different, as we can see here:

String o1 = new String("abc");
String o2 = new String("abc");
System.out.println(o1 == o2);    //prints: false
System.out.println("abc" == o1); //prints: false

If necessary, it is possible to move the string value created with thenew operator to the string pool using theintern() method, like this:

String o1 = new String("abc");
System.out.println("abc" == o1);          //prints: false
System.out.println("abc" == o1.intern()); //prints: true

In the previous code snippet, theintern() method attempted to move the newly created"abc" value into the string pool but discovered that such a literal exists there already, so it reused the literal from the string pool. That is why the references in the last line in the preceding example are equal.

The good news is that you probably will not need to createString objects using thenew operator, and most Java programmers never do this. But when aString object is passed into your code as an input and you have no control over its origin, comparison by reference only may cause an incorrect result (if the strings have the same spelling but were created by thenew operator). That is why, when the equality of two strings by spelling (and case) is necessary, to compare two literals orString objects, theequals() method is a better choice, as illustrated here:

String o1 = new String("abc");
String o2 = new String("abc");
System.out.println(o1.equals(o2));       //prints: true
System.out.println(o2.equals(o1));       //prints: true
System.out.println(o1.equals("abc"));    //prints: true
System.out.println("abc".equals(o1));    //prints: true
System.out.println("abc".equals("abc")); //prints: true

We will talk about theequals() method and other methods of theString class shortly.

Another feature that makesString literals and objects look like primitive values is that they can be added using the+ arithmetic operator, like this (execute themain() method of thecom.packt.learnjava.ch01_start.StringClass class—see theoperatorAdd() method):

String s1 = "abc";
String s2 = "abc";
String s = s1 + s2;
System.out.println(s);              //prints: abcabc
System.out.println(s1 + "abc");     //prints: abcabc
System.out.println("abc" + "abc");  //prints: abcabc
String o1 = new String("abc");
String o2 = new String("abc");
String o = o1 + o2;
System.out.println(o);              //prints: abcabc
System.out.println(o1 + "abc");     //prints: abcabc

No other arithmeticoperator can be applied to aString literal or an object.

A newString literal, called a text block, was introduced with Java 15. It facilitates the preservation of indents and multiple lines without adding white spaces in quotes. For example, here is how a programmer would add indentation before Java 15 and use\n to break the line:

String html = "<html>\n" +
              "   <body>\n" +
              "       <p>Hello World.</p>\n" +
              "   </body>\n" +
              "</html>\n";

And here is how the same result is achieved with Java 15:

String html = """
               <html>
                   <body>
                       <p>Hello World.</p>
                   </body>
               </html>
              """;

To see how itworks, execute themain() method of thecom.packt.learnjava.ch01_start.StringClass class—see thetextBlock() method.

String immutability

Since allString literals can be shared, the JVM authors make sure that, once stored, aString variable cannot be changed. This helps not only avoid the problem of concurrent modification of the same value from different places of the code but also prevents unauthorized modification of aString value, which often represents a username or password.

The following code looks like aString value modification:

String str = "abc";
str = str + "def";
System.out.println(str);       //prints: abcdef
str = str + new String("123");
System.out.println(str);       //prints: abcdef123

But, behind the scenes, the original"abc" literal remains intact. Instead, a few new literals were created:"def","abcdef","123", and"abcdef123". To prove this, we have executed the following code:

String str1 = "abc";
String r1 = str1;
str1 = str1 + "def";
String r2 = str1;
System.out.println(r1 == r2);      //prints: false
System.out.println(r1.equals(r2)); //prints: false

As you can see, ther1 andr2 variables refer to different memories, and the objects they refer to are spelled differently too.

We will talk more about strings inChapter 5,Strings, Input/Output, and Files.

IDs and variables

From our school days, we have an intuitive understanding of what a variable is. We think of it as a name that represents a value. We solve problems using such variables asx gallons of water orn miles of distance, and similar. In Java, the name of a variable is called an ID and can be constructed by certain rules. Using an ID, a variable can be declared (defined) and initialized.

ID

Accordingto theJava Language Specification (https://docs.oracle.com/javase/specs), an ID (a variable name) can be a sequence of Unicode characters that represent letters, digits 0-9, a dollar sign ($), or an underscore (_).

Other limitations are outlined here:

  • The first symbol of an ID cannot be a digit.
  • An ID cannot have the same spelling as a keyword (see theJava keywords section ofChapter 3,Java Fundamentals).
  • It cannot be spelled as atrue orfalse Boolean literal or as anull literal.
  • And since Java 9, an ID cannot be just an underscore (_).

Here are a few unusual but legal examples of IDs:

$
_42
αρετη
String

Variable declaration (definition) and initialization

A variable hasa name (an ID) and a type. Typically, it refers to the memory where a value is stored, but may refer to nothing (null) or not refer to anything at all (then, it is not initialized). It can represent a class property, an array element, a method parameter, and a local variable. The last one is the most frequently used kind of variable.

Before a variable can be used, it has to be declared and initialized. In some other programming languages, a variable can also be defined, so Java programmers sometimes use the worddefinition as a synonym of declaration, which is not exactly correct.

Here is a terminology review with examples:

int x;      //declaration of variable x
x = 1;      //initialization of variable x
x = 2;      //assignment of variable x

Initialization and assignment look the same. The difference is in their sequence: the first assignment is called initialization. Without an initialization, a variable cannot be used.

Declaration and initialization can be combined in a single statement. Observe the following, for example:

float $ = 42.42f;
String _42 = "abc";
int αρετη = 42;
double String = 42.;

var type holder

In Java 10, a sortof type holder,var, was introduced. TheJava Language Specification defines it thus: “var is not a keyword, but an identifier with special meaning as the type of a local variable declaration.”

In practical terms, it lets a compiler figure out the nature of the declared variable, as follows (see thevar() method in thecom.packt.learnjava.ch01_start.PrimitiveTypes class):

var x = 1;

In the preceding example, the compiler can reasonably assume thatx has theintprimitive type.

As you may have guessed, to accomplish that, a declaration on its own would not suffice, as we can see here:

var x;    //compilation error

That is, withoutinitialization, the compiler cannot figure out the type of the variable whenvar is used.

Java statements

A Java statement is a minimal construct that can be executed. It describes an action and ends with a semicolon (;). We have seen many statements already. For example, here are three statements:

float f = 23.42f;
String sf = String.valueOf(f);
System.out.println(sf);

The first line is a declaration statement combined with an assignment statement. The second line is also a declaration statement combined with an assignment statement and method invocation statement. The third line is just a method invocation statement.

Here is a list of Java statement types:

  • An empty statement that consists of only one symbol,; (semicolon)
  • A class or interface declaration statement (we will talk about this inChapter 2,Java Object-Oriented Programming (OOP))
  • A local variable declaration statement:int x;
  • A synchronized statement: this is beyond the scope of this book
  • An expression statement
  • A control flow statement

An expression statement can be one of the following:

  • A method invocation statement:someMethod();
  • An assignment statement:n = 23.42f;
  • An object creation statement:new String("abc");
  • A unary increment or decrement statement:++x ; or --x; or x++; or x--;

We will talk more about expression statements in theExpression statements section.

A control flow statement can be one of the following:

  • A selection statement:if-else orswitch-case
  • An iteration statement:for,or while, ordo-while
  • An exception-handling statement:throw,try-catch, ortry-catch-finally
  • Abranching statement:break,continue, orreturn

We will talk more about control statements in theControl flow statements section.

Expression statements

An expression statement consists of one or moreexpressions. An expression typically includes one or more operators. It can be evaluated, which means it can produce a result of one of the following types:

  • A variable:x = 1, for example
  • A value:2*2, for example

It returns nothing when the expression is an invocation of a method that returnsvoid. Such a method is said to produce only a side effect:void someMethod(), for example.

Consider the following expression:

x = y++;

The preceding expression assigns a value to anx variable and has a side effect of adding 1 to the value of they variable.

Another example would be a method that prints a line, like this:

System.out.println(x);

Theprintln() method returns nothing and has a side effect of printing something.

By its form, an expression can be one of the following:

  • A primary expression: a literal, a new object creation, a field or method access (invocation).
  • A unary operator expression:x++, for example.
  • A binary operator expression:x*y, for example.
  • A ternary operator expression:x > y ? true : false, for example.
  • A lambda expression:x -> x + 1 (seeChapter 14,Java Standard Streams).
  • If anexpression consists of other expressions, parentheses are often used to identify each of the expressions clearly. This way, it is easier to understand and to set the expressions’ precedence.

Control flow statements

When a Java program is executed, it is executed statement by statement. Some statements have to be executed conditionally, based on the result of an expression evaluation. Such statements are called control flow statements because, in computer science, a control flow (or flow of control) is the order in which individual statements are executed or evaluated.

A control flow statement can be one of the following:

  • A selection statement:if-else orswitch-case
  • An iteration statement:for,while, ordo-while
  • An exception-handling statement:throw,try-catch, ortry-catch-finally
  • A branching statement:break,continue, orreturn

Selection statements

Selection statements are based on an expressionevaluation and have four variations, as outlined here:

  • if (expression) {do something}
  • if (expression) {do something}else {do something else}
  • if (expression) {do something}else if {do something else}else {do something else}
  • switch...case statement

Here are some examples ofif statements:

if(x > y){
    //do something
}
if(x > y){
    //do something
} else {
    //do something else
}
if(x > y){
    //do something
} else if (x == y){
    //do something else
} else {
    //do something different
}

Aswitch...case statement is a variation of anif...else statement, as illustrated here:

switch(x){
    case 5:               //means: if(x = 5)
        //do something
        break;
    case 7:             
        //do something else
        break;
    case 12:
        //do something different
        break;
    default:             
        //do something completely different
        //if x is not 5, 7, or 12
}

As you can see, theswitch...case statement forks the execution flow based on the value of the variable. Thebreak statement allows theswitch...case statement to be executed. Otherwise, all the following cases would be executed.

In Java 14, a newswitch...case statement has been introduced in a less verbose form, asillustrated here:

void switchStatement(int x){
    switch (x) {
        case 1, 3 -> System.out.print("1 or 3");
        case 4    -> System.out.print("4");
        case 5, 6 -> System.out.print("5 or 6");
        default   -> System.out.print("Not 1,3,4,5,6");
    }
    System.out.println(": " + x);
}

As you can see, it uses an arrow (->) and does not use abreak statement.

Execute themain() method of thecom.packt.learnjava.ch01_start.ControlFlow class—see theselection() method that calls theswitchStatement() method with different parameters, as follows:

switchStatement(1);    //prints: 1 or 3: 1
switchStatement(2);    //prints: Not 1,3,4,5,6: 2
switchStatement(5);    //prints: 5 or 6: 5

You can see the results from the comments.

If several lines of code have to be executed in each case, you can just put braces ({}) around the block of code, as follows:

switch (x) {
    case 1, 3 -> {
                    //do something
                 }
    case 4    -> {
                    //do something else
                 }
    case 5, 6 -> System.out.println("5 or 6");
    default   -> System.out.println("Not 1,3,4,5,6");
}

The Java 14switch...case statementcan even return a value, thus becoming in effect aswitch expression. For example, here is a case when another variable has to be assigned based on theswitch...case statement result:

void switchExpression1(int i){
    boolean b = switch(i) {
        case 0, 1 -> false;
        case 2 -> true;
        default -> false;
    };
    System.out.println(b);
}

If we execute theswitchExpression1() method (see theselection() method of thecom.packt.learnjava.ch01_start.ControlFlow class), the results are going to look like this:

switchExpression1(0);    //prints: false
switchExpression1(1);    //prints: false
switchExpression1(2);    //prints: true

The following example of aswitch expression is based on a constant:

static final String ONE = "one", TWO = "two", THREE = "three",
                    FOUR = "four", FIVE = "five";
void switchExpression2(String number){
    var res = switch(number) {
        case ONE, TWO -> 1;
        case THREE, FOUR, FIVE -> 2;
        default -> 3;
    };
    System.out.println(res);
}

If we execute theswitchExpression2() method (see theselection() method of thecom.packt.learnjava.ch01_start.ControlFlow class), the results are going to look like this:

switchExpression2(TWO);            //prints: 1
switchExpression2(FOUR);           //prints: 2
switchExpression2("blah");         //prints: 3

Here’s yet another example of aswitch expression, this time based on theenum value:

enum Num { ONE, TWO, THREE, FOUR, FIVE }
void switchExpression3(Num number){
    var res = switch(number) {
        case ONE, TWO -> 1;
        case THREE, FOUR, FIVE -> 2;
    };
    System.out.println(res);
}

If we execute theswitchExpression3() method (see theselection() method of thecom.packt.learnjava.ch01_start.ControlFlow class), the results are going to look like this:

switchExpression3(Num.TWO);        //prints: 1
switchExpression3(Num.FOUR);       //prints: 2
//switchExpression3("blah"); //does not compile

In case a block of code has to be executed based on a particular input value, it is not possible to use areturn statement because it is reserved already for the returning value from a method. That is why, to return a value from a block, we have to use ayield statement, as shown in the following example:

void switchExpression4(Num number){
    var res = switch(number) {
        case ONE, TWO -> 1;
        case THREE, FOUR, FIVE -> {
            String s = number.name();
            yield s.length();
        }
    };
    System.out.println(res);
}

If we execute theswitchExpression4() method (see theselection() method of thecom.packt.learnjava.ch01_start.ControlFlow class), the results are going to look like this:

switchExpression4(Num.TWO);        //prints: 1
switchExpression4(Num.THREE);      //prints: 5

Iteration statements

An iteration statement can take one of the followingthree forms:

  • Awhile statement
  • Ado...while statement
  • Afor statement, also called aloop statement

Awhile statement looks like this:

while (boolean expression){
      //do something
}

Here is a specific example (execute themain() method of thecom.packt.learnjava.ch01_start.ControlFlow class—see theiteration() method):

int n = 0;
while(n < 5){
 System.out.print(n + " "); //prints: 0 1 2 3 4
 n++;
}

In some examples, instead of theprintln() method, we use theprint() method, which does not feed another line (does not add a line feed control at the end of its output). Theprint() method displays the output in one line.

Ado...while statement has a very similar form, as we can see here:

do {
    //do something
} while (boolean expression)

It differs from awhile statement by always executing the block of statements at least once before evaluating the expression, as illustrated in the following code snippet:

int n = 0;
do {
    System.out.print(n + " ");   //prints: 0 1 2 3 4
    n++;
} while(n < 5);

As you can see, it behaves the same way when the expression istrue at the first iteration. But if theexpression evaluates tofalse, theresults are different, as we can see here:

int n = 6;
while(n < 5){
    System.out.print(n + " ");   //prints nothing
    n++;
}
n = 6;
do {
    System.out.print(n + " ");   //prints: 6
    n++;
} while(n < 5);

for statement syntax looks like this:

for(init statements; boolean expression; update statements) {
 //do what has to be done here
}

Here is how afor statement works:

  1. init statements initialize a variable.
  2. A Boolean expression is evaluated using the current variable value: iftrue, the block of statements is executed; otherwise, thefor statement exits.
  3. update statements update the variable, and the Boolean expression is evaluated again with this new value: iftrue, the block of statements is executed; otherwise, thefor statement exits.
  4. Unless exited, the final step is repeated.

As you can seehere, if you aren’t careful, you can get into an infinite loop:

for (int x = 0; x > -1; x++){
    System.out.print(x + " ");  //prints: 0 1 2 3 4 5 6 ...
}

So, you have to make sure that the Boolean expression guarantees eventual exit from the loop, like this:

for (int x = 0; x < 3; x++){
    System.out.print(x + " ");  //prints: 0 1 2
}

The following example demonstrates multiple initialization andupdate statements:

for (int x = 0, y = 0; x < 3 && y < 3; ++x, ++y){
    System.out.println(x + " " + y);
}

And here is a variation of the preceding code for statements for demonstration purposes:

for (int x = getInitialValue(), i = x == -2 ? x + 2 : 0,
             j = 0; i < 3 || j < 3 ; ++i, j = i) {
 System.out.println(i + " " + j);
}

If thegetInitialValue() method is implemented likeint getInitialValue(){ return -2; }, then the preceding twofor statements produce exactly the same results.

To iterate over an array of values, you can use an array index, like so:

int[] arr = {24, 42, 0};
for (int i = 0; i < arr.length; i++){
    System.out.print(arr[i] + " ");  //prints: 24 42 0
}

Alternatively, youcan use a more compact form of afor statement that produces the same result, as follows:

int[] arr = {24, 42, 0};
for (int a: arr){
    System.out.print(a + " ");  //prints: 24 42 0
}

This last form is especially useful with a collection, as shown here:

List<String> list = List.of("24", "42", "0");
for (String s: list){
    System.out.print(s + " ");  //prints: 24 42 0
}

We will talk about collections inChapter 6,Data Structures, Generics, and Popular Utilities.

Exception-handling statements

In Java, there are classes calledexceptions that represent events that disrupt the normal execution flow. They typically have names that end withException:NullPointerException,ClassCastException,ArrayIndexOutOfBoundsException, to name but a few.

All the exception classes extend thejava.lang.Exception class, which, in turn, extends thejava.lang.Throwable class (we will explain what this means inChapter 2,Java Object-Oriented Programming (OOP)). That’s why all exception objects have common behavior. They contain information about the cause of the exceptional condition and the location of its origination (line number of the source code).

Each exception object can be generated (thrown) either automatically by the JVM or by the application code, using thethrow keyword. If a block of code throws an exception, you can use atry-catch ortry-catch-finally construct to capture the thrown exception object and redirect the execution flow to another branch of code. If the surrounding code does not catch the exception object, it propagates all the way out of the application into the JVM and forces it to exit (and abort the application execution). So, it is good practice to usetry-catch ortry-catch-finally in all the places where an exception can be raised and you do not want your application to abort execution.

Here is a typical example of exception handling:

try {
    //x = someMethodReturningValue();
    if(x > 10){
        throw new RuntimeException("The x value is out
                                    of range: " + x);
    }
    //normal processing flow of x here
} catch (RuntimeException ex) {
    //do what has to be done to address the problem
}

In the preceding code snippet, normal processing flow will be not executed in the case ofx > 10. Instead, thedo what has to be done block will be executed. But, in thex <= 10 case, the normal processing flow block will be run and thedo what has to be done block will be ignored.

Sometimes, it is necessary to execute a block of code anyway, whether an exception was thrown/caught or not. Instead of repeating the same code block in two places, you can put it in afinally block, as follows (execute themain() method of thecom.packt.learnjava.ch01_start.ControlFlow class—see theexception() method):

try {
    //x = someMethodReturningValue();
    if(x > 10){
        throw new RuntimeException("The x value is out
                                    of range: " + x);
    }
    //normal processing flow of x here
} catch (RuntimeException ex) {
   System.out.println(ex.getMessage());   
   //prints: The x value is out of range: ...
   //do what has to be done to address the problem
} finally {
   //the code placed here is always executed
}

We will talk about exception handling in more detail inChapter 4,Exception Handling.

Branching statements

Branching statements allow breaking of thecurrent execution flow and continuation of execution from the first line after the current block or from a certain (labeled) point of the control flow.

A branching statement can be one of the following:

  • break
  • continue
  • return

We have seen howbreak was used inswitch-case statements. Here is another example (execute themain() method of thecom.packt.learnjava.ch01_start.ControlFlow class—see thebranching() method):

String found = null;
List<String> list = List.of("24", "42", "31", "2", "1");
for (String s: list){
    System.out.print(s + " ");         //prints: 24 42 31
    if(s.contains("3")){
        found = s;
        break;
    }
}
System.out.println("Found " + found);  //prints: Found 31

If we need to find the first list element that contains"3", we can stop executing as soon as thes.contains("3") condition is evaluated totrue. The remaining list elements are ignored.

In a more complicated scenario, with nestedfor statements, it is possible to set a label (witha : column) that indicates whichfor statement has to be exited, as follows:

String found = null;
List<List<String>> listOfLists = List.of(
        List.of("24", "16", "1", "2", "1"),
        List.of("43", "42", "31", "3", "3"),
        List.of("24", "22", "31", "2", "1")
);
exit: for(List<String> l: listOfLists){
    for (String s: l){
        System.out.print(s + " "); //prints: 24 16 1 2 1 43
        if(s.contains("3")){
            found = s;
            break exit;
        }
    }
}
System.out.println("Found " + found);  //prints: Found 43

We have chosen a label name ofexit, but we could call it any other name too.

Acontinue statement works similarly, as follows:

String found = null;
List<List<String>> listOfLists = List.of(
                List.of("24", "16", "1", "2", "1"),
                List.of("43", "42", "31", "3", "3"),
                List.of("24", "22", "31", "2", "1")
);
String checked = "";
cont: for(List<String> l: listOfLists){
        for (String s: l){
           System.out.print(s + " ");
                  //prints: 24 16 1 2 1 43 24 22 31
           if(s.contains("3")){
               continue cont;
           }
           checked += s + " ";
        }
}
System.out.println("Found " + found);  //prints: Found 43
System.out.println("Checked " + checked);  
                            //prints: Checked 24 16 1 2 1 24 22

Itdiffers frombreak by stating which of thefor statements need to continue and not exit.

Areturn statement is used to return a result from a method, as follows:

String returnDemo(int i){
    if(i < 10){
        return "Not enough";
    } else if (i == 10){
        return "Exactly right";
    } else {
        return "More than enough";
    }
}

As you can see, there can be severalreturn statements in a method, each returning a different value in different circumstances. If the method returns nothing (void), areturn statement isnot required, although it is frequently used for better readability, as follows:

void returnDemo(int i){
    if(i < 10){
        System.out.println("Not enough");
        return;
    } else if (i == 10){
        System.out.println("Exactly right");
        return;
    } else {
        System.out.println("More than enough");
        return;
    }
}

Execute thereturnDemo() method by running themain() method of thecom.packt.learnjava.ch01_start.ControlFlow class (see thebranching() method). The results are going to look like this:

String r = returnDemo(3);
System.out.println(r);      //prints: Not enough
r = returnDemo(10);
System.out.println(r);      //prints: Exactly right
r = returnDemo(12);
System.out.println(r);      //prints: More than enough

Statements are the building blocks of Java programming. They are like sentences in English—complete expressions of intent that can be acted upon. They can be compiled and executed. Programming is like expressing an action plan in statements.

With this, theexplanation of the basics of Java is concluded. Congratulations on getting through it!

Summary

This chapter introduced you to the exciting world of Java programming. We started with explaining the main terms, and then explained how to install the necessary tools—the JDK and the IDE—and how to configure and use them.

With a development environment in place, we have provided readers with the basics of Java as a programming language. We have described Java primitive types, theString type, and their literals. We have also defined what an ID is and what a variable is and finished with a description of the main types of Java statements. All the points of the discussion were illustrated by specific code examples.

In the next chapter, we are going to talk about theobject-oriented (OO) aspects of Java. We will introduce the main concepts, explain what a class is, what an interface is, and the relationship between them. The termsoverloading,overriding, andhiding will also be defined and demonstrated in code examples, as well as usage of thefinal keyword.

Quiz

  1. What does JDK stand for?
    1. Java Document Kronos
    2. June Development Karate
    3. Java Development Kit
    4. Java Developer Kit
  2. What does JCL stand for?
    1. Java Classical Library
    2. Java Class Library
    3. Junior Classical Liberty
    4. Java Class Libras
  3. What does Java SE stand for?
    1. Java Senior Edition
    2. Java Star Edition
    3. Java Structural Elections
    4. Java Standard Edition
  4. What does IDE stand for?
    1. Initial Development Edition
    2. Integrated Development Environment
    3. International Development Edition
    4. Integrated Development Edition
  5. What are Maven's functions?
    1. Project building
    2. Project configuration
    3. Project documentation
    4. Project cancellation
  6. Which of the following are Java primitive types?
    1. boolean
    2. numeric
    3. integer
    4. string
  7. Which of the following are Java numeric types?
    1. long
    2. bit
    3. short
    4. byte
  8. What is aliteral?
    1. A letter-based string
    2. A number-based string
    3. A variable representation
    4. A value representation
  9. Which of the following are literals?
    1. \\
    2. 2_0
    3. 2__0f
    4. \f
  10. Which of the following are Java operators?
    1. %
    2. $
    3. &
    4. ->
  11. What does the following code snippet print?
    int i = 0; System.out.println(i++);
    1. 0
    2. 1
    3. 2
    4. 3
  12. What does the following code snippet print?
    boolean b1 = true; boolean b2 = false; System.out.println((b1 & b2) + " " + (b1 && b2));
    1. false true
    2. false false
    3. true false
    4. true true
  13. What does the following code snippet print?
    int x = 10; x %= 6; System.out.println(x);
    1. 1
    2. 2
    3. 3
    4. 4
  14. What is the result of the following code snippet?
    System.out.println("abc" - "bc");
    1. a
    2. abc-bc
    3. Compilation error
    4. Execution error
  15. What does the following code snippet print?
    System.out.println("A".repeat(3).lastIndexOf("A"));
    1. 1
    2. 2
    3. 3
    4. 4
  16. Which of the following are correct IDs?
    1. int __ (two underscores)
    2. 2a
    3. a2
    4. $
  17. What does the following code snippet print?
    for (int i=20, j=-1; i < 23 && j < 0; ++i, ++j){         System.out.println(i + " " + j + " "); }
    1. 20 -1 21 0
    2. Endless loop
    3. 21 0
    4. 20 -1
  18. What does the following code snippet print?
    int x = 10;try {    if(x++ > 10){        throw new RuntimeException("The x value is out of the range: " + x);    }    System.out.println("The x value is within the range: " + x);} catch (RuntimeException ex) {    System.out.println(ex.getMessage());}
    1. Compilation error
    2. Thex value is out of the range: 11
    3. Thex value is within the range: 11
    4. Execution time error
  19. What does the following code snippet print?
int result = 0;
List<List<Integer>> source = List.of(
        List.of(1, 2, 3, 4, 6),
        List.of(22, 23, 24, 25),
        List.of(32, 33)
);
cont: for(List<Integer> l: source){
    for (int i: l){
        if(i > 7){
            result = i;
            continue cont;
        }
     }
}
System.out.println("result=" + result);
  1. result = 22
  2. result = 23
  3. result = 32
  4. result = 33
  1. Select all the following statements that are correct:
    1. A variable can be declared.
    2. A variable can be assigned.
    3. A variable can be defined.
    4. A variable can be determined.
  2. Select all the correct Java statement types from the following:
    1. An executable statement
    2. A selection statement
    3. A method end statement
    4. An increment statement
Left arrow icon

Page1 of 10

Right arrow icon
Download code iconDownload Code

Key benefits

  • A step-by-step guide for beginners to get started with programming in Java 17
  • Explore core programming topics including GUI programming, concurrency, and error handling
  • Write efficient code and build projects while learning the fundamentals of programming

Description

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.

Who is this book for?

This book is for those who would like to start a new career in the modern Java programming profession, as well as those who do it professionally already and would like to refresh their knowledge of the latest Java and related technologies and ideas.

What you will learn

  • Understand and apply object-oriented principles in Java
  • Explore Java design patterns and best practices to solve everyday problems
  • Build user-friendly and attractive GUIs with ease
  • Understand the usage of microservices with the help of practical examples
  • Discover techniques and idioms for writing high-quality Java code
  • Get to grips with the usage of data structures in Java

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date :Jul 29, 2022
Length:748 pages
Edition :2nd
Language :English
ISBN-13 :9781803245737
Category :

What do you get with eBook?

Product feature iconInstant access to your Digital eBook purchase
Product feature icon Download this book inEPUB andPDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature iconDRM FREE - Read whenever, wherever and however you want
Product feature iconAI Assistant (beta) to help accelerate your learning
OR

Contact Details

Modal Close icon
Payment Processing...
tickCompleted

Billing Address

Product Details

Publication date :Jul 29, 2022
Length:748 pages
Edition :2nd
Language :English
ISBN-13 :9781803245737
Category :
Concepts :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99billed monthly
Feature tick iconUnlimited access to Packt's library of 7,000+ practical books and videos
Feature tick iconConstantly refreshed with 50+ new titles a month
Feature tick iconExclusive Early access to books as they're written
Feature tick iconSolve problems while you work with advanced search and reference features
Feature tick iconOffline reading on the mobile app
Feature tick iconSimple pricing, no contract
$199.99billed annually
Feature tick iconUnlimited access to Packt's library of 7,000+ practical books and videos
Feature tick iconConstantly refreshed with 50+ new titles a month
Feature tick iconExclusive Early access to books as they're written
Feature tick iconSolve problems while you work with advanced search and reference features
Feature tick iconOffline reading on the mobile app
Feature tick iconChoose a DRM-free eBook or Video every month to keep
Feature tick iconPLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick iconExclusive print discounts
$279.99billed in 18 months
Feature tick iconUnlimited access to Packt's library of 7,000+ practical books and videos
Feature tick iconConstantly refreshed with 50+ new titles a month
Feature tick iconExclusive Early access to books as they're written
Feature tick iconSolve problems while you work with advanced search and reference features
Feature tick iconOffline reading on the mobile app
Feature tick iconChoose a DRM-free eBook or Video every month to keep
Feature tick iconPLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick iconExclusive print discounts

Frequently bought together


Learn Java 17 Programming
Learn Java 17 Programming
Read more
Jul 2022748 pages
Full star icon3.3 (12)
eBook
eBook
$31.99$35.99
$44.99
Domain-Driven Design with Java - A Practitioner's Guide
Domain-Driven Design with Java - A Practitioner's Guide
Read more
Aug 2022302 pages
Full star icon4.6 (25)
eBook
eBook
$29.99$33.99
$41.99
Java Memory Management
Java Memory Management
Read more
Nov 2022146 pages
Full star icon5 (5)
eBook
eBook
$25.99$28.99
$36.99
Stars icon
Total$123.97
Learn Java 17 Programming
$44.99
Domain-Driven Design with Java - A Practitioner's Guide
$41.99
Java Memory Management
$36.99
Total$123.97Stars icon

Table of Contents

22 Chapters
Part 1: Overview of Java ProgrammingChevron down iconChevron up icon
Part 1: Overview of Java Programming
Chapter 1: Getting Started with Java 17Chevron down iconChevron up icon
Chapter 1: Getting Started with Java 17
Technical requirements
How to install and run Java
How to install and run an IDE
Java primitive types and operators
String types and literals
IDs and variables
Java statements
Summary
Quiz
Chapter 2: Java Object-Oriented Programming (OOP)Chevron down iconChevron up icon
Chapter 2: Java Object-Oriented Programming (OOP)
Technical requirements
OOP concepts
Class
Interface
Overloading, overriding, and hiding
The final variable, method, and classes
The record class
Sealed classes and interfaces
Polymorphism in action
Summary
Quiz
Chapter 3: Java FundamentalsChevron down iconChevron up icon
Chapter 3: Java Fundamentals
Technical requirements
Packages, importing, and access
Java reference types
Reserved and restricted keywords
Usage of the this and super keywords
Converting between primitive types
Converting between primitive and reference types
Summary
Quiz
Part 2: Building Blocks of JavaChevron down iconChevron up icon
Part 2: Building Blocks of Java
Chapter 4: Exception HandlingChevron down iconChevron up icon
Chapter 4: Exception Handling
Technical requirements
The Java exceptions framework
Checked and unchecked exceptions
The try, catch, and finally blocks
The throws statement
The throw statement
The assert statement
Best practices of exception handling
Summary
Quiz
Chapter 5: Strings, Input/Output,and FilesChevron down iconChevron up icon
Chapter 5: Strings, Input/Output,and Files
Technical requirements
String processing
I/O streams
File management
Apache Commons’ FileUtils and IOUtils utilities
Summary
Quiz
Chapter 6: Data Structures, Generics, and Popular UtilitiesChevron down iconChevron up icon
Chapter 6: Data Structures, Generics, and Popular Utilities
Technical requirements
List, Set, and Map interfaces
Collections utilities
Arrays utilities
Objects utilities
The java.time package
Summary
Quiz
Chapter 7: Java Standard and External LibrariesChevron down iconChevron up icon
Chapter 7: Java Standard and External Libraries
Technical requirements
Java Class Library (JCL)
External libraries
Summary
Quiz
Chapter 8: Multithreading and Concurrent ProcessingChevron down iconChevron up icon
Chapter 8: Multithreading and Concurrent Processing
Technical requirements
Thread versus process
User thread versus daemon
Extending the Thread class
Implementing the Runnable interface
Extending Thread versus implementing Runnable
Using a pool of threads
Getting results from a thread
Parallel versus concurrent processing
Concurrent modification of the same resource
Summary
Quiz
Chapter 9: JVM Structure and Garbage CollectionChevron down iconChevron up icon
Chapter 9: JVM Structure and Garbage Collection
Technical requirements
Java application execution
Java processes
JVM’s structure
Garbage collection
Summary
Quiz
Chapter 10: Managing Data in a DatabaseChevron down iconChevron up icon
Chapter 10: Managing Data in a Database
Technical requirements
Creating a database
Creating a database structure
Connecting to a database
Releasing the connection
CRUD data
Summary
Quiz
Chapter 11: Network ProgrammingChevron down iconChevron up icon
Chapter 11: Network Programming
Technical requirements
Network protocols
UDP-based communication
TCP-based communication
UDP versus TCP protocols
URL-based communication
Using the HTTP 2 Client API
Summary
Quiz
Chapter 12: Java GUI ProgrammingChevron down iconChevron up icon
Chapter 12: Java GUI Programming
Technical requirements
Java GUI technologies
JavaFX fundamentals
HelloWorld with JavaFX
Control elements
Charts
Applying CSS
Using FXML
Embedding HTML
Playing media
Adding effects
Summary
Quiz
Part 3: Advanced JavaChevron down iconChevron up icon
Part 3: Advanced Java
Chapter 13: Functional ProgrammingChevron down iconChevron up icon
Chapter 13: Functional Programming
Technical requirements
What is functional programming?
Standard functional interfaces
Lambda expression limitations
Method references
Summary
Quiz
Chapter 14: Java Standard StreamsChevron down iconChevron up icon
Chapter 14: Java Standard Streams
Technical requirements
Streams as a source of data and operations
Stream initialization
Operations (methods)
Numeric stream interfaces
Parallel streams
Summary
Quiz
Chapter 15: Reactive ProgrammingChevron down iconChevron up icon
Chapter 15: Reactive Programming
Technical requirements
Asynchronous processing
Non-blocking APIs
Reactive
Reactive streams
RxJava
Summary
Quiz
Chapter 16: Java Microbenchmark HarnessChevron down iconChevron up icon
Chapter 16: Java Microbenchmark Harness
Technical requirements
What is JMH?
Creating a JMH benchmark
Running the benchmark
JMH benchmark parameters
JMH usage examples
A word of caution
Summary
Quiz
Chapter 17: Best Practices for Writing High-Quality CodeChevron down iconChevron up icon
Chapter 17: Best Practices for Writing High-Quality Code
Technical requirements
Java idioms, their implementation, and their usage
Best design practices
Code is written for people
Use well-established frameworks and libraries
Testing is the shortest path to quality code
Summary
Quiz
AssessmentsChevron down iconChevron up icon
Chapter 1 – Getting Started with Java 17
Chapter 2 – Java Object-Oriented Programming (OOP)
Chapter 3 – Java Fundamentals
Chapter 4 – Exception Handling
Chapter 5 – Strings, Input/Output, and Files
Chapter 6 – Data Structures, Generics, and Popular Utilities
Chapter 7 – Java Standard and External Libraries
Chapter 8 – Multithreading and Concurrent Processing
Chapter 9 – JVM Structure and Garbage Collection
Chapter 10 – Managing Data in a Database
Chapter 11 – Network Programming
Chapter 12 – Java GUI Programming
Chapter 13 – Functional Programming
Chapter 14 – Java Standard Streams
Chapter 15 – Reactive Programming
Chapter 16 – Java Microbenchmark Harness
Chapter 17 – Best Practices for Writing High-Quality Code
Why subscribe?
Other Books You May EnjoyChevron down iconChevron up icon
Other Books You May Enjoy
Packt is searching for authors like you
Share Your Thoughts

Recommendations for you

Left arrow icon
Debunking C++ Myths
Debunking C++ Myths
Read more
Dec 2024226 pages
Full star icon5 (1)
eBook
eBook
$27.99$31.99
$39.99
Go Recipes for Developers
Go Recipes for Developers
Read more
Dec 2024350 pages
eBook
eBook
$27.99$31.99
$39.99
50 Algorithms Every Programmer Should Know
50 Algorithms Every Programmer Should Know
Read more
Sep 2023538 pages
Full star icon4.5 (68)
eBook
eBook
$35.98$39.99
$49.99
$49.99
Asynchronous Programming with C++
Asynchronous Programming with C++
Read more
Nov 2024424 pages
Full star icon5 (1)
eBook
eBook
$29.99$33.99
$41.99
Modern CMake for C++
Modern CMake for C++
Read more
May 2024504 pages
Full star icon4.7 (12)
eBook
eBook
$35.98$39.99
$49.99
Learn Python Programming
Learn Python Programming
Read more
Nov 2024616 pages
Full star icon5 (1)
eBook
eBook
$31.99$35.99
$39.99
Learn to Code with Rust
Learn to Code with Rust
Read more
Nov 202457hrs 40mins
Video
Video
$74.99
Modern Python Cookbook
Modern Python Cookbook
Read more
Jul 2024818 pages
Full star icon4.9 (21)
eBook
eBook
$38.99$43.99
$54.99
Right arrow icon

Customer reviews

Top Reviews
Rating distribution
Full star iconFull star iconFull star iconHalf star iconEmpty star icon3.3
(12 Ratings)
5 star41.7%
4 star16.7%
3 star0%
2 star8.3%
1 star33.3%
Filter icon Filter
Top Reviews

Filter reviews by




Davi VieiraNov 27, 2022
Full star iconFull star iconFull star iconFull star iconFull star icon5
I have just finished reading Nick Samoylov's book Learn Java 17 Programming. He takes you into a fascinating Java journey by introducing you first to the language building blocks, such as the Java Class Library (JCL), and how the Object Oriented Programming (OOP) principles, such as inheritance, polymorphism, and encapsulation, are applied in Java. Once he prepares the ground with the basics, he describes the language's new features, such as Sealed and Record classes, while exploring exciting APIs such as Collections and Stream. Nick goes to further distances to give the reader a crisp description of the available techniques for reactive programming and performance benchmarking on Java. I like Nick's writing style because, whenever possible, he provides his personal views from previous experience when explaining a technical concept. It improves the learning experience and turns reading more enjoyable. Whether you are a beginner or a seasoned developer, Nick's book is a must-read if you want to learn Java or refresh your skills.
Amazon Verified reviewAmazon
amazonsdeNov 30, 2022
Full star iconFull star iconFull star iconFull star iconFull star icon5
Very clearly and simply written book with good examples to make home the point about the language. Really liked the author's approach to do away with unnecessary pedantic sounded approach, and explain the language in an approachable fashion, so as one can learn the language.Highly Recommended for all Java lovers.
Amazon Verified reviewAmazon
SarahNov 27, 2022
Full star iconFull star iconFull star iconFull star iconFull star icon5
This book is good for beginners and is great for anyone looking for references. I would recommend to anybody at any level of Java to read.
Amazon Verified reviewAmazon
SteveNov 23, 2022
Full star iconFull star iconFull star iconFull star iconFull star icon5
This book is great for someone learning Java or programming in general. It goes over the basic fundamentals and explains why certain structures are used when they are. I think it would make a great textbook or something for a newer programer to use as a refresher when they get stumped. I would highly recommend this book to anyone that wants to get a good foundation in Java.
Amazon Verified reviewAmazon
Kindle CustomerNov 23, 2022
Full star iconFull star iconFull star iconFull star iconFull star icon5
I enjoyed reading this book and learning about the latest features of Java 17. The author explained all the concepts with lots of code snippets, making this book a great resource for both new and experienced Java developers. Some of my favorite topics included the comprehensive list of IDEs for writing Java code, the way the concept of byte code is explained with an example, an explanation of how the toString() method is implemented in JDK, thorough coverage of the latest Java 17 features, like sealed classes and records. I also found the coverage of complex topics like streams, reactive programming, and micro benchmarking with JMH good enough to get started with it. The companion GitHub site for this book is also very helpful, allowing you to easily download and access code examples. Overall, an excellent book for those looking to master the latest version of Java and improve their coding skills.
Amazon Verified reviewAmazon
  • Arrow left icon Previous
  • 1
  • 2
  • 3
  • Arrow right icon Next

People who bought this also bought

Left arrow icon
50 Algorithms Every Programmer Should Know
50 Algorithms Every Programmer Should Know
Read more
Sep 2023538 pages
Full star icon4.5 (68)
eBook
eBook
$35.98$39.99
$49.99
$49.99
Event-Driven Architecture in Golang
Event-Driven Architecture in Golang
Read more
Nov 2022384 pages
Full star icon4.9 (11)
eBook
eBook
$35.98$39.99
$49.99
The Python Workshop Second Edition
The Python Workshop Second Edition
Read more
Nov 2022600 pages
Full star icon4.6 (22)
eBook
eBook
$36.99$41.99
$51.99
Template Metaprogramming with C++
Template Metaprogramming with C++
Read more
Aug 2022480 pages
Full star icon4.6 (14)
eBook
eBook
$33.99$37.99
$46.99
Domain-Driven Design with Golang
Domain-Driven Design with Golang
Read more
Dec 2022204 pages
Full star icon4.4 (19)
eBook
eBook
$31.99$35.99
$44.99
Right arrow icon

About the author

Profile icon Nick Samoylov
Nick Samoylov
LinkedIn iconGithub icon
Nick Samoylov graduated from Moscow Institute of Physics and Technology, working as a theoretical physicist and learning to program as a tool for testing his mathematical models. After the demise of the USSR, Nick created and successfully ran a software company, but was forced to close it under the pressure of governmental and criminal rackets. In 1999, with his wife Luda and two daughters, he emigrated to the USA and has been living in Colorado since then, working as a Java programmer. In his free time, Nick likes to write and hike in the Rocky Mountains.
Read more
See other products by Nick Samoylov
Getfree access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook?Chevron down iconChevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website?Chevron down iconChevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook?Chevron down iconChevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support?Chevron down iconChevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks?Chevron down iconChevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook?Chevron down iconChevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.


[8]ページ先頭

©2009-2025 Movatter.jp