Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn Java with Projects

You're reading from   Learn Java with Projects A concise practical guide to learning everything a Java professional really needs to know

Arrow left icon
Product type Paperback
Published in Nov 2023
Publisher Packt
ISBN-13 9781837637188
Length 598 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Maaike van Putten Maaike van Putten
Author Profile Icon Maaike van Putten
Maaike van Putten
Dr. Seán Kennedy Dr. Seán Kennedy
Author Profile Icon Dr. Seán Kennedy
Dr. Seán Kennedy
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Preface 1. Part 1: Java Fundamentals
2. Chapter 1: Getting Started with Java FREE CHAPTER 3. Chapter 2: Variables and Primitive Data Types 4. Chapter 3: Operators and Casting 5. Chapter 4: Conditional Statements 6. Chapter 5: Understanding Iteration 7. Chapter 6: Working with Arrays 8. Chapter 7: Methods 9. Part 2: Object-Oriented Programming
10. Chapter 8: Classes, Objects, and Enums 11. Chapter 9: Inheritance and Polymorphism 12. Chapter 10: Interfaces and Abstract Classes 13. Chapter 11: Dealing with Exceptions 14. Chapter 12: Java Core API 15. Part 3: Advanced Topics
16. Chapter 13: Generics and Collections 17. Chapter 14: Lambda Expressions 18. Chapter 15: Streams – Fundamentals 19. Chapter 16: Streams: Advanced Concepts 20. Chapter 17: Concurrency 21. Index

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.

You have been reading a chapter from
Learn Java with Projects
Published in: Nov 2023
Publisher: Packt
ISBN-13: 9781837637188
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image