Chapter 1 — Head first Java

Sathyajith Saliya
5 min readMar 15, 2022

--

Breaking the surface….

Have you ever heard a great book written about Java? Yes, there are good books, but the point is young generations are not much love reading. So let’s read a great book of Java chapter by chapter.

Kathy Sierra’s and Bert Bate’s Head First Java book is one famous book written about Java. It is not the official reference, but that helps a lot for beginners new to programming. This article series will bring you the essentials of each chapter of that book. So let’s start.

As the first step of your Java journey, you should learn how Java works. We have Java-enabled machines around us. Like your mobile phone, personal computer, PDA’s, and much more devices are there. We write some Java programs that can run on those machines and do what we want. That is the simple explanation.

So how do we do that? First, we write our idea in a document by using the Java language. That is called the source document. That will go through a Java compiler. What the compiler does is, convert what we wrote in the source document into a machine language. So that can be understood by the computer. Also, the compiler checks errors, and it won’t compile if you have made any errors while writing the source document.

Then the compiler will create a new intermediate document called Java bytecode. That bytecode can be interpreted or translated into something it can run. This bytecode is platform-independent, which means you can run that bytecode file on Linux, Mac, or Windows machines without doing any modifications.

In simple words, what you do in Java is, type a source code file, compile it using the Javac compiler, and then run the compiled bytecode in a Java virtual machine.

Ok. let’s have a brief understanding of java history.

First, we had Java 1.02, which contains 250 classes. Class means it is a template to create objects and do what we want. Let’s discuss classes and objects in detail in a later chapter. It was slow and had a lot of bugs, but it had a cute name and a logo. The Applets are the new invention.

Then we had Java 1.1, which contains 500 classes. It was a litter faster than the previous version, and it was becoming trendy, and it had better GUI code.

Then it came Java 2 ( versions 1.2–1.4). It had 2300 classes and was much faster. It comes in three main versions. Micro Edition(J2ME), Standard Edition (J2SE) and Enterprise Edition(J2EE).

Then it evolved to Java 5.0 (versions 1.5 and up). It had 3500 classes and was more powerful and easier to develop with. This version is known as “Tiger.” This Java 5.0 version added significant changes to the language, making it easier for programmers to implement new things.

Now we discuss the code structure in Java. I mentioned that we write java programs in a secure document. Within that source file, we put the class file. The class file contains methods. A method has statements to perform some tasks.

put a class in a source file

put a method in a class

put statements in a method.

code structure of Java

Source file: A source code file has one class definition with the .java extension. The class is a small piece of the program. We need at least a single class to perform any small task. The class must reside within a pair of curly braces.

What resides in a class?: a class has one or more methods. The method is a set of steps to perform a tiny task. We write instructions on how this should happen. We need to declare this method inside a class.

What is inside a method?: we write instructions to perform some task. Method code is a set of statements. First, do this, then do that likewise. A method is a kind of function or procedure.

I think you have some knowledge of the Java Virtual Machine. If you do not, please read the JVM, JRE, and JDK article that I wrote. for the basic understanding, we can say that the Java Virtual Machine generates the bytecode.

When the JVM starts running, it will search for the class you try to run. Then JVM executes all the instructions of the main method. Every java program has at least one class and the Main method. That is the starting point of the Java program.

This is the class structure.

public static MyFirstApp {

public static void main (String [] args) {

System.out.println(“Java is awasom”);

}

}

Let’s go through it word by word. I use the illustration that the present in the book. That is easy to grab the essentials.

illustration from the Head First Java book

In Java, we implement all the statements in a class. We type source code file with a .java extension. Then it loaded into the java compiler. If there is no error, it will produce the class file. Now it hands it over to the JVM to run the file. Running a program means telling JVM to load the class within that class file. Then it starts executing its main() method.

We can perform different tasks by saying to the JVM. These are the general things in programming. If I listed down those things,

we can have,

statements: declarations, assignments method calls

loops: for and while loops to perform iterative tasks

branching: if/else tests

When we develop programs using Java, we have to care about syntax.

statements end in a semicolon :

curly braces are used to define code blocks

to declare a variable with a type and a name

assign operator use one equal sign =

equals operator uses two equal signs ==

We have to have a clear idea of two different components. Those are the compiler and the Java Virtual Machine.

The compiler is used to convert source code into the class file. The output of the compiler is a class file.

Then we hand over that class file into the JVM. It will run the program.

Ok, there is more to learn. Let’s stop for this article and see you in another article. Until then, stay safe goodbye….!

References — Kathy Sierra’s Head First Java Book

--

--