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

Compiling and running Java programs

Now that we have our first program written, let’s discuss how we can compile and run it. We will cover the basics of the compilation process, the role of the JVM, and how to compile and run Java code using the command line and an IDE.

Understanding the compilation process

The source code is written in a human-readable format using the Java programming language. Or at least, we hope that this is your opinion after this book. Before the code can be executed, it must be transformed into a format that the computer can understand. You already know that Java is a compiled language and that this process is called compilation.

During compilation, the Java compiler (javac) converts the source code (.java files) into bytecode (.class files). Once the bytecode is generated, it can be executed by the JVM. We have already learned that the JVM is the bytecode executer and that every platform has its own custom JVM enabling the WORA feature of Java.

Compiling the code with javac on the command line

To compile a Java program using the command line, follow these steps:

  1. Open a terminal (Command Prompt on Windows, Terminal on macOS or Linux).
  2. Navigate to the directory containing your Java source code file (for example, the directory of your previously created HelloWorld.java file). In case you don’t know how to do that, this can be done with the cd command, which stands for change directory. For example, if I’m in a directory called documents and I want to step into the subfolder called java programs, I’d run the cd "java programs" command. The quotes are only needed when there are spaces in the directory name. It’s beyond the scope of this book to explain how to change directories for any platform. There are many excellent explanations for every platform on how to navigate the folder structure using the command line on the internet.
  3. Once you’re in the folder containing the Java file, enter the following command to compile the Java source code:
    javac HelloWorld.java

    If the compilation is successful, a new file with the same name but a .class extension (for example, HelloWorld.class) will be created in the same directory. This file contains the bytecode that can be executed by the JVM.

Let’s see how we can run this compiled code.

Running the compiled code with Java on the command line

To run the compiled Java program, follow these steps:

  1. In the terminal, make sure you are still in the directory containing the .class file.
  2. Enter the following command to execute the bytecode:
    java HelloWorld

The JVM will load and run the bytecode, and you should see the output of your program. In this case, the output will be as follows:

Hello world!

It’s pretty cool that we can write Java in Notepad and run it on the command line, but the life of a modern-day Java developer is a lot nicer. Let’s add IDEs to the mix and see this for ourselves.

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