Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learn Java with Projects
  • Table Of Contents Toc
  • Feedback & Rating feedback
Learn Java with Projects

Learn Java with Projects

By : Dr. Seán Kennedy, Maaike van Putten
4.9 (51)
close
close
Learn Java with Projects

Learn Java with Projects

4.9 (51)
By: Dr. Seán Kennedy, Maaike van Putten

Overview of this book

Learn Java with Projects bridges the gap between introductory Java guides and verbose, theoretical references. This book is crafted to build a strong foundation in Java programming, starting from the Java environment itself. It goes far beyond a superficial review of the topics; it demonstrates, with practical examples, why these fundamentals are crucial for developing a deep understanding of the language. You'll not only learn about classes and objects but also see how these concepts are used in practical scenarios, enhancing your ability to write clean, efficient code. The engaging projects throughout the book provide real-world applications of complex topics, ensuring you can connect theoretical knowledge with practical skills. What makes this book stand out is the expertise of its authors. Seán, a seasoned university lecturer with over 20 years of experience, brings academic rigor and real-world insights, thanks to his work with a prestigious software company. Maaike, a passionate software developer and award-winning trainer, brings hands-on experience and a love for teaching. By the end of this book, you'll not only understand Java's core concepts and the critical advanced ones, but also gain practical experience through projects that mimic real-life challenges.
Table of Contents (22 chapters)
close
close
1
Part 1: Java Fundamentals
9
Part 2: Object-Oriented Programming
15
Part 3: Advanced Topics

Writing our first program

Before diving into the process of compiling and running Java programs, let’s create a simple Java program using a basic text editor. This will help you understand the structure of a Java program and how to write and save a Java source code file. For this example, we will create a “Hello world!” program that will be used to demonstrate the process of compilation and execution.

Hello world

You may have heard of “Hello world!” programs. They are a common way to start learning a new programming language. It’s a simple program that prints the message "Hello world!" to the console. Writing this program will provide you with a very basic understanding of Java syntax, and it will help you to become familiar with the process of writing, compiling, and running Java code.

Steps to create the program

Alright, let’s start coding. Here are the steps:

  1. First, open a basic text editor on your computer. Notepad on Windows, TextEdit on macOS, or Gedit on Linux are suitable options.
  2. Write the following Java code in your text editor:
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello world!");
        }
    }
  3. Save the file as HelloWorld.java in a directory of your choice. Don’t forget the .java extension when saving the file. This indicates that the file contains Java source code. The code should not have .txt after .java. This happens sometimes in Windows, so make sure to not select the text file in the filetype dropdown.

TextEdit – file extension issues

The later versions of macOS have some issues with TextEdit. You can’t save it as a Java file by default. In order to enable this, you need to go to Format | Make Plain Text and select UTF-8.

After this, you can save it as a .java file. You may still run into encoding errors; the problem is with the encoding, and fixing it might be a lot of effort missing the goal of this exercise. It might be better to download Notepad++, TextPad, or Sublime for this part. Or go ahead and download the HelloWorld.java file from our GitHub repository.

Understanding the program

Let’s have a look at the code we just used. First of all, be aware that this is case-sensitive. That means that when you look at the code, most things will not work as you expect if you mix up lowercase and uppercase.

First, we created a class named HelloWorld with a main method. We’ll cover classes and methods in a lot more detail, of course. But a class is the fundamental building block of Java applications, and it can contain methods. Methods can be executed to do things – things being executing statements.

The main method is a special method. It is the entry point of our Java program and contains the code that will be executed when the program is run. The line with System.out.println("Hello world!"); writes the Hello world! message to the console. Please note, that println stands for print line, so it uses a lowercase L and not an uppercase i.

With the HelloWorld.java file saved, we are now ready to move on to the next section, where we will learn how to compile and run the Java program using the command line and an IDE.

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY