Java — A quick overview

Ruvinda Lakdini Madapatha
5 min readOct 18, 2021

--

(The summary of chapter 1 -Headfirst Java book)

Java version 1.02 is the first version of Java released to the public. From then on, Java has become a widely-used programming language because of its’ many benefits.

Java is an object-oriented language with friendly syntax, memory management, and portability. The special feature in Java called write once/run anywhere allows us only to write the code once and run it on any device. We don’t have to write the whole program from scratch for every device we intend to implement. It helps developers to manage the time.

How Java works

We can write a program using Java. The program should be saved with the extension .java, and the Java compiler takes that saved file, compiler it, and gives a .class file as the output. The JVM runs the .class file and finally provides us the output.

How Java works

The above diagram shows how the components in Java helps to write and execute a Java program.

Code structure in Java

The basic structure of a Java file

The above is the overview of a Java program file. Let’s take a look at each of the components.

  1. The source file (File with the .java extension) has Java classes. A class is a piece of the program. It has an opening curly brace and an ending curly brace which is used to separate space in the program for that particular class.
  2. The class file has one or more methods. Methods hold instructions to perform a particular task, so they basically act as a function. They should be defined inside a class. A method uses curly braces to separate space for it to work within the class file.
  3. Methods have statements that are instructions stating how to perform that method.

Class Anatomy

When we run a Java program, JVM loads the class and looks for a method called the main method. If the method exists, JVM will start to run the program. The program runs until all the code inside the main method is executed.

The main method in Java

An application can have many classes, but there can be only one main method for the entire application.

Every Java program must have the main method and at least one class to properly run the program. The main method is where a Java program starts running. So, we should defiantly have it in the program.

Anatomy of a simple java class with the main method

The above image shows the full view of a simple Java program. It has one class named A and the main method to execute the class. The program prints out a text as the output.

  1. First, we save this class as a Java class by giving a .java extension. It should be saved as A.java
  2. Then the compiler compiles the program to a class file using the command javac A.java, and we get the A.class file as a result.
  3. The JVM runs the class file using the command java A, giving us “Class A” as the output.

What can we use inside methods?

Like we talked about before, methods are procedures we can use to perform tasks. Either inside the main method or in any other method, we can define tasks to do something. For that, we can use statements, looping, and branching.

Statement: declarations, assignments, method calls etc.

Examples;

  1. int y ; (declaring a variable)
  2. y = 10+5; (assigning values to a variable)
  3. String name = “Apple”; (declaring and assigning values to a variable)
  4. System.out.println(“name”); (method calling)
  5. //this is a statement (a comment)

Loops: for and while

Examples;

for(int z= 0;z<10;z++){ System.out.println(z); }

while(x > 10){ x = x+1; }

Branching: if/else tests

Examples;

if(x == 10){ System.out.println(“x”);

}else{ System.out.println(“Not x”);}

When using statements, loops, and branching, there are specific points we should consider.

  1. Each statement must end with a semicolon [ int x = 10; ]
  2. A single-line comment begins with two forward slashes.
  3. We should define classes and methods within a pair of curly braces.
  4. Assignment operator in Java has only one equal sign [ int x = 10; ] while equal operator has two equal signs [ if (x == y){} ]
  5. In Java, System.out.println inserts a new line, and System.out.print keeps printing on the same line.

Looping constructs

Java has three standard looping constructs. They are while, do-while, and for.

In these loops, if the condition is true, then everything inside the loop block is executed. Otherwise, execution moves to the code immediately after the loop block.

A loop block is defined by an opening curly brace and an ending curly brace. The execution of the loop should take place only within this loop block.

Conditional tests

The execution of a loop depends on conditional tests. A conditional test gives out the result as either true or false so, they are also called simple boolean tests.

Conditional tests are performed by checking the value of a variable using a comparison operator.

There are three comparison operators;

  1. > (greater than operator)
  2. < (less than operator)
  3. == (equal operator)

Below example shows how conditional tests are used with a while loop and an if/else loop.

Code with a while and an if/else loops

In the above code, as long as the condition of a while is fulfilled, the while loop will run. It gives two outputs as Value of x=1 and Value of x=2.

In the if /else loop, if the condition satisfies the if block in the if/else loop, then that block will execute, and if not, the else block will execute. In this case, if block satisfies the condition, hence we get its output as Value of y=4.

--

--

Ruvinda Lakdini Madapatha

An enthusiastic leaner who loves to explore new and interesting facts. Aspiring to be a software engineer with avid knowledge of Java