Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Java Projects
Java Projects

Java Projects: Learn the fundamentals of Java 11 programming by building industry grade practical projects , Second Edition

eBook
$27.98 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Java Projects

The First Real Java Program - Sorting Names

In the previous chapter, we got acquainted with Java, and especially with using the REPL tool and interactively executing some simple code. That is a good start, but we need more. In this chapter, we will develop a simple program. Using this code as an example, we will look at different build tools, which are frequently used for Java projects, and learn the basic features of the Java language.

This chapter will cover the following topics:

  • The sorting problem
  • The project structure and build tools
  • The Make, Ant, Maven, and Gradle build tools
  • Java language features related to the code example

Getting started with sorting

The sorting problem is one of the oldest programming tasks that an engineer deals with. We have a set of records and we know that we want to find a specific one sometime soon. To find it, we sort the records in a specific order that helps us find the record we want quickly.

As an example, we have the names of students with their marks on some cards. When students come to the dean's cabin asking for their results, we look through all of the cards one after the other to find the name of the inquiring student. However, it is better if we sort the cards by the names of the students alphabetically. When a student makes an inquiry, we can search the mark attached to the name much faster.

We can look at the middle card; if it shows the name of the student, then we are happy to have found the name and the mark. If the card precedes the name of the student...

Getting started with project structure and build tools

When a project is more complex than a single class, and it usually is, then it is wise to define a project structure. We will have to decide where we store the source files, where the resource files (the ones that contain some resource for the program but are not Java source) are, where the .class files should be written by the compiler, and so on. Generally, the structure is mainly the directory setup and the configuration of the tools that perform the build.

The compilation of complex programs cannot be feasibly done using the command line issuing javac commands. If we have 100 Java source files, the compilation will require that many javac commands to be issued. It can be shortened using wildcards, such as javac *.java, or we can write a simple bash script or a BAT command file which does that. First, it will be just 100...

Setting up the project with Maven

To start the project, we will use the directory structure and pom.xml, that was created by Maven itself when we started with the following command line:

$ mvn archetype:generate

It created the directories, the pom.xml file, and an App.java file. Now, we will extend this project by creating new files. We will code the sorting algorithm first in the packt.java11.example.stringsort package:

When we create the new package in the IDE, the editor will automatically create the stringsort subdirectory under the already existing src/main/java/packt/java11/example directory:

Creating the new Sort class using the IDE will also automatically create a new file named Sort.java in this directory, and it will fill in the skeleton of the class:

package packt.java11.example.stringsort;

public class Sort {
}

We will now have App.java containing the following...

Coding the sort

Maven and the IDE created the files for the sort program. They form the skeleton for our code, and now it is time to grow some muscles on them to let it move. We spent quite some time setting up the project by visiting the different build tools, only to learn how to compile the code.

I hope that this did not distract you much, but anyhow, we deserve to see some real code.

First, we will create the code for the sorting code, and after that, the code that invokes the sorting. The code that invokes the sorting is a kind of testing code. For simplicity, we will now simply use a public static void main() method to start the code. We will use the test framework in later chapters.

As for now, the code for the sorting will look like this:

var n = names.length;
while (n > 1) {
    for (var j = 0; j < n - 1; j++) {
        if (names[j].compareTo(names[j + 1]) &gt...

Understanding the algorithm and language constructs

The algorithm was explained at the start of the chapter. The implementation is in the Sort class inside the sort() method, and it consists only of a few lines:

var n = names.length;
while (n > 1) {
    for (var j = 0; j < n - 1; j++) {
        if (names[j].compareTo(names[j + 1]) > 0) {
            final var tmp = names[j + 1];
            names[j + 1] = names[j];
            names[j] = tmp;
        }
    }
    n--;
}

The n variable holds the length of the array at the start of the sorting. Arrays in Java always have a property that gives the length, and it is called length. When we start the sorting, we will go from the start of the array to the end of it and, as you may recall, the last element, Wilson, will walk up to the last position during this first iteration. Subsequent iterations will be shorter and, therefore...

Compiling and running the program

Finally, we will compile and execute our program from the command line. There is nothing new in this one; we will only apply what we have learned in this chapter using the following two commands:

$ mvn package

This compiles the program, packages the result into a JAR file, and finally executes the following command:

$ java -cp target/SortTutorial-1.0.0-SNAPSHOT.jar packt.java11.example.App

This will print the following result on the command line:

Summary

In this chapter, we have developed a very basic sort algorithm. It was made purposefully simple so that we could reiterate the basic and most important Java language elements, classes, packages, variables, methods, and so on. We also looked at build tools, so we are not empty-handed in the next chapters when projects will contain more than just two files. We will use Maven and Gradle in the upcoming chapters.

In the very next chapter, we will make the sort program more complex, implementing more efficient algorithms and also making our code flexible, giving us the opportunity to learn more advanced Java language features.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Explore advanced technologies that Java 11 delivers such as web programming and parallel computing
  • Discover modern programming paradigms such as microservices, cloud computing and enterprise structures
  • Build highly responsive applications with this practical introduction to Reactive programming

Description

Java is one of the most commonly used software languages by programmers and developers. In this book, you’ll learn the new features of Java 11 quickly and experience a simple and powerful approach to software development. You’ll see how to use the Java runtime tools, understand the Java environment, and create a simple namesorting Java application. Further on, you'll learn about advanced technologies that Java delivers, such as web programming and parallel computing, and will develop a mastermind game. Moving on, we provide more simple examples, to build a foundation before diving into some complex data structure problems that will solidify your Java 11 skills. With a special focus on the features of new projects: Project Valhalla, Project Panama, Project Amber, and Project Loom, this book will help you get employed as a top-notch Java developer. By the end of the book, you’ll have a firm foundation to continue your journey toward becoming a professional Java developer.

Who is this book for?

This book is for anyone who wants to learn the Java programming language. No programming experience required. If you have prior experience, it will help you through the book more easily.

What you will learn

  • Compile, package, and run a program using a build management tool
  • Get to know the principles of test-driven development
  • Separate the wiring of multiple modules from application logic
  • Use Java annotations for configuration
  • Master the scripting API built into the Java language
  • Understand static versus dynamic implementation of code

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 31, 2018
Length: 524 pages
Edition : 2nd
Language : English
ISBN-13 : 9781789130690
Vendor :
Oracle
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Aug 31, 2018
Length: 524 pages
Edition : 2nd
Language : English
ISBN-13 : 9781789130690
Vendor :
Oracle
Category :
Languages :

Packt Subscriptions

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

Frequently bought together


Stars icon
Total $ 146.97
Mastering Java 11
$48.99
Java Projects
$48.99
Learning Java by Building Android  Games
$48.99
Total $ 146.97 Stars icon

Table of Contents

11 Chapters
Getting Started with Java 11 Chevron down icon Chevron up icon
The First Real Java Program - Sorting Names Chevron down icon Chevron up icon
Optimizing the Sort - Making Code Professional Chevron down icon Chevron up icon
Mastermind - Creating a Game Chevron down icon Chevron up icon
Extending the Game - Run Parallel, Run Faster Chevron down icon Chevron up icon
Making Our Game Professional - Do it as a Web App Chevron down icon Chevron up icon
Building a Commercial Web Application Using REST Chevron down icon Chevron up icon
Extending Our E-Commerce Application Chevron down icon Chevron up icon
Building an Accounting Application Using Reactive Programming Chevron down icon Chevron up icon
Finalizing Java Knowledge to a Professional Level Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Half star icon Empty star icon Empty star icon Empty star icon 1.9
(9 Ratings)
5 star 11.1%
4 star 11.1%
3 star 0%
2 star 11.1%
1 star 66.7%
Filter icon Filter
Top Reviews

Filter reviews by




Atomsk Jan 23, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is great for someone with solid Java knowledge looking to improve their skills.
Amazon Verified review Amazon
Mihail Gaberov Sep 25, 2018
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Great for someone who wants to learn quickly the basics of Java language and use them directly in real life projects! Bravo!
Amazon Verified review Amazon
Jared Josafhat Hernández Zúñiga Jul 18, 2019
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Is really hard to follow if you don't have experience with "Maven" in the first chapter it "explains" how to create a basic project using archetype but in the second chapter it uses another approach to build the unit testing & sorting new project so this book is really hard to follow
Amazon Verified review Amazon
Saleh Abuo-Tamma Mar 28, 2020
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Unfortunately I waste my money and time to try this book. I tried really really hard to follow the book and downloaded Linux distro, downloaded all the build tools, and did every possible choice, yet I couldn't understand the book. The writer spends almost 150 pages (out of 514) talking about build tools and very technical details that totally distract the reader! Even as an Android programmer, I suffered A LOT with this book! The projects' ideas are good, PLEASE make a new version with clearer demonstrations and much lesser building distractions...
Amazon Verified review Amazon
anil petkar Sep 21, 2019
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I am not satisfied with this book
Amazon Verified review Amazon
Get free 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 icon Chevron 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 icon Chevron 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 icon Chevron 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 icon Chevron 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 icon Chevron 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 icon Chevron 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.