The Java Workshop
eBook - ePub

The Java Workshop

A Practical, No-Nonsense Introduction to Java Development

David Cuartielles, Andreas Göransson, Eric Foster-Johnson

Share book
  1. 604 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

The Java Workshop

A Practical, No-Nonsense Introduction to Java Development

David Cuartielles, Andreas Göransson, Eric Foster-Johnson

Book details
Book preview
Table of contents
Citations

About This Book

Get to grips with the fundamentals of Java programming and learn to build useful applications with the help of real-world examples and engaging practical activities

Key Features

  • Build a solid foundation in Java and focus on developing real-world applications
  • Implement the fundamental concepts of object-oriented programming in your code
  • Work with external data storage systems and learn how to efficiently handle data

Book Description

Java is a versatile, popular programming language used across a wide range of industries. Learning how to write effective Java code can take your career to the next level, and The Java Workshop will help you do just that. This book is designed to take the pain out of Java coding and teach you everything you need to know to be productive in building real-world software.

The Workshop starts by showing you how to use classes, methods, and the built-in Collections API to manipulate data structures effortlessly. You'll dive right into learning about object-oriented programming by creating classes and interfaces and making use of inheritance and polymorphism. After learning how to handle exceptions, you'll study the modules, packages, and libraries that help you organize your code. As you progress, you'll discover how to connect to external databases and web servers, work with regular expressions, and write unit tests to validate your code. You'll also be introduced to functional programming and see how to implement it using lambda functions.

By the end of this Workshop, you'll be well-versed with key Java concepts and have the knowledge and confidence to tackle your own ambitious projects with Java.

What you will learn

  • Write clean, well-commented Java code that's easy to maintain
  • Debug logical errors and handle exceptions in your Java programs
  • Implement object-oriented and functional programming paradigms
  • Use regular expressions to search for information in text data
  • Work with information stored in databases using JDBC
  • Make HTTP requests from Java applications and parse the response data
  • Secure your data with cryptography and encryption
  • Write unit tests to validate your code with JUnit

Who this book is for

This Java coding book is designed for anyone who is new to Java. Whether you're an aspiring software developer, or are just curious about learning to code, then this book will get you on the right track. No prior programming experience is required.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is The Java Workshop an online PDF/ePUB?
Yes, you can access The Java Workshop by David Cuartielles, Andreas Göransson, Eric Foster-Johnson in PDF and/or ePUB format, as well as other popular books in Informatique & Programmation. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781839218118
Edition
1

1. Getting Started

Overview
In this chapter, we will be covering the fundamentals of Java. You will first learn to write and compile your first "Hello World!" program—traditionally the first step to practicing any new language. We will then discuss the differences between the command-line interface (CLI) and Graphical User Interface (GUI), and the relative benefits of both. By the end of this chapter, you will understand the basic concepts behind variables, know how to hold data within them, and, even, how to comment on your own code.

Introduction

When learning how to program in almost any programming language, the first example you will typically test is called "hello world." It is the simplest application possible; the aim is to write the expression "hello world" to whatever user interface the programming environment offers. Executing this program will introduce you to the basics of writing code using the IntelliJ editor, utilizing different types of data to be printed to the user interface and adding comments to your code.
When writing your first program, you will also discover how Java's syntax is constructed and how it relates to other languages such as C or C++. Understanding the syntax is key to starting to read code. You will learn how to distinguish where commands and functions begin and end, how parameters are passed over between blocks of code, and how to leave comments that will help you when revisiting your software in the future.
This chapter covers the basics of writing and testing programs as a first step toward building all the code that you will find in this book.

Writing, Compiling, and Executing Your Hello World Program

In the preface, you saw how to install the IntelliJ development environment. While it is possible to write Java code with literally any text editor, we believe it is good to see how to create applications using state-of-the-art tools such as the aforementioned software package.
However, prior to guiding you step by step through getting your first program to run, we should take a look at the code that will become your first executable running on Java. The following code listing shows the program. Read through it, and we will later revise what each one of the parts is doing:
public class Main {
public static void main (String[] args) {
System.out.println("Hello World!");
}
}
The first line is what we call a class definition. All programs in Java are called classes. A program might consist of several classes. Classes carry inside them everything they need to perform the task they were designed for. For a class to be executable in Java, it must contain a method called main. In this program, you can see how the Main class contains a method called main that will be printing the sentence "Hello World!" to the system's default output.
The code included in the class definition (public class Main) indicates that the class itself is public, which means that it will be accessible from other programs running on your computer. The same happens for the method definition (public static void main(String[] args)). There is, however, a series of other things that require our attention:
  • static signifies that there is nothing in the system instantiating the main method. Because of the way the Java Virtual Machine works, the main method needs to be static, or it will not be possible to execute it.
  • void indicates that the main method will not be returning anything to any code calling it. Methods could, in fact, send an answer to a piece of code executing it, as we will see later in the book.
  • main is the name of the method. You cannot assign this a different name, since it is the method that makes the program executable and needs to be named this way.
  • String[] args are the parameters of the main method. Parameters are passed as a list of strings. In other words, the program could take arguments from other parts within your computer and use them as data. In the particular case of the main method, these are strings that could be entered on the command-line interface (CLI) when calling the program.

Exercise 1: Creating Your Hello World Program in Java

IntelliJ provides you with a pre-made "hello world" template. Templates help you to get started faster with your code, as they provide the components you may need to speed up development. Templates can also be used for educational purposes; this is the case when it comes to testing "hello world."
For this first exercise, start with the editor. We will leave some options as they are by default. We will later see how to personalize some of the options to better suit our needs:
  1. Open IntelliJ and you will see a window giving you several options. Click on Create New Project. It should be the first option in the list:
    Figure 1.1: Creating a new project on IntelliJ IDE
    Figure 1.1: Creating a new project on IntelliJ IDE
  2. A new interface should appear. The default options here are meant for creating a Java program, so you just need to click Next:
    Figure 1.2: Creating a new Java project
    Figure 1.2: Creating a new Java project
  3. Check the box to create the project from a template. Click on Java Hello World and then click Next:
    Figure 1.3: Create a Java Hello World project from template
    Figure 1.3: Create a Java Hello World project from template
  4. Name the project chapter01. Then, click Finish:
    Figure 1.4: Create a Hello World Project
    Figure 1.4: Create a Hello World Project
  5. As we haven't chosen a folder to store the projects (intentionally), IntelliJ will offer you the possibility to create a default project folder inside your user space. Click OK:
    Figure 1.5: Default project folder option on IntelliJ IDE
    Figure 1.5: Default project folder option on IntelliJ IDE
  6. You will see a popup with tips on how to use the software. If you have never used a development environment of this type before, then this is a good way to get information about how it functions every time IntelliJ boots up. Choose your preferences and then click Close:
    Figure 1.6: Tip on how to use the IDE
    Figure 1.6: Tip on how to use the IDE
  7. IntelliJ reminds you regarding the possibility of using a special tab dedicated to learning more about the environment in relation to programming. Click Got It.
  8. The editor presents a menu bar, a code navigation bar, a project navigation area, and the actual editor where you can see the code we explained earlier. Now it is time to test it. Click on the Run button (this is the triangle on the right-hand side of the code navigation bar).
    Figure 1.7: Execute the program by clicking on the Run button
    Figure 1.7: Execute the program by clicking on the Run button
  9. When the program runs, a terminal window unfolds at the bottom of IntelliJ. Here, you can see how the software called your JVM, the program's outcome, and a line from the ...

Table of contents