How Java Works and Understanding Code Structure of Java – Part 2

In our last post ‘What is Java and History of Java‘ we had covered What is Java, features of Java in details, release history and its naming as well as places where Java is utilized.

Working of Java Understanding Java Code
Working of Java Understanding Java Code – Part 2

Here in this post we will be going through working and code structure of Java Programming Language. Before we proceed let me remind you that Java was developed keeping in mind “Write Once Run Anywhere/Anytime (WORA)” means to ensure that the application developed should be architecturally neutral, Platform Independent and portable.

Working of Java

Having these goals in mind Java was developed with the below working model which can be classified into four stages.

Stage 1

Write the source file. This file contains all the procedure, method, class and objects within established protocol for Java Programming Language. The name of source file should be the name of the class or vice-versa. The source file name must have extension .java. Also the filename and class name are case sensitive.

Stage 2

Run the Java Source Code file through Java Compiler. Java Source code Compiler checks for error and syntax in the source file. It won’t let you compile your source code without satisfying Java compiler by fixing all errors and warning.

Stage 3

Compiler creates classfile. These classfile inherit the same name as the Source code file name, but the extension varies. The Source file name has extension 'filename.java', where as the extension of classfile created by compiler is 'filename.class'. This classfile is coded into bytecode – bytecodes are like magic.

Stage 4

This classfile created by Java Compiler is portable and architecturally neutral. You can port this classfile to run on any processor architecture and Platform/device. All you need is a Java Virtual Machine (JVM) to run this code no matter where.

Now understand the above four stages using an example. Here is a small sample Java Program code. Don’t worry if you don’t understand the code below. As of now just understand how it works.

public class MyFirstProgram
{
    public static void main(String[] args)
    {
        System.out.println("Hello Tecmint, This is my first Java Program");
    }
}

1. I wrote this program and defined class name MyFirstProgram. It is important to notice that this program must be saved as 'MyFirstProgram.java'.

Remember stage 1 above – The class name and file name must be same and filename must have extension .java. Also java is case sensitive hence if your classname is ‘MyFirstProgram‘, your source file name must be ‘MyFirstProgram.java‘.

You can not name it as ‘Myfirstprogram.java‘ or ‘myfirstprogram.java‘ or anything else. By convention it is a good idea to name your class based upon what the program is doing actually.

2. To compile this Java Source file, you need to pass it through Java compiler. Java compiler will essentially check the source code for any error and warning. It wont compile the source code until all the issues are solved. To compile java source code, you need to run:

$ javac MyFirstProgram.java

Where MyFirstProgram.java is the name of the source file.

3. On successful compilation you will notice that the Java compiler created a new file in the same directory the name of which is MyFirstProgram.class.

This class file is coded in bytecodes and can be run on any platform, any processor architecture any number of time. You may run the class file inside of JVM (Java Virtual Machine) on Linux or any other platform simply as:

$ java MyFirstProgram

So all you learnt above can be summarized as:

Java Source Code >> Compiler >> classfile/bytecode >> Various devices running JVM 

Understanding Code Structure in Java

1. Java source code file must contains a class definition. One Java Source file can contain only one public class/top-level class however it can contain lots of private class/inner-class.

The outer class/top class/public class can access all private class/inner class. The class must be within curly braces. Everything in Java is an object and class is a blueprint for object.

A demo of public/private class in Java:

public class class0
{
...
	private class1
	{
	…
	}

	private class 2
	{
	…
	}
...
}

2. Class contain one or more methods. Method must go within the curly braces of the class. A dummy example is:

public class class0
{
	public static void main(String[] args)
	{
	…..
	…..
	}
}

3. A method contain one or more statement/instruction. The instruction(s) must go within the curly braces of method. A dummy example is:

public class class0
{
	public static void main(String[] args)
	{
	System.out.println("Hello Tecmint, This is my first Java Program");
	System.out.println("I am Loving Java");
	…
	...
	}
}

Also important to mention at this point – Every Statement must end with semicolon. A dummy example is:

System.out.println("Hello Tecmint, This is my first Java Program");
...
...
System.out.println("I am Loving Java");

Writing your first Java Program with detailed description. The description is being put as comments here (// means commented out) in this example. You should write comments within a program.

Not only because this is a good habit but also because it makes the code readable ab you or anyone else at anytime later.

// Declare a Public class and name it anything but remember the class name and file name must be same, say class name is MyProg and hence file name must be MyProg.java
public class MyProg

// Remember everything goes into curly braces of class?
{
 

// This is a method which is inside the curly braces of class.
   public static void main(String[] args)

    // Everything inside a method goes into curly braces	
    {
        
    // Statement or Instruction inside method. Note it ends with a semicolon
    System.out.println("I didn't knew JAVA was so much fun filled");
    
    // closing braces of method
    }

// closing braces of class
}

A detailed technical description of the above simple Java Program.

public class MyProg

Here in the above name of class is MyProg and MyProg is a Public class which means everyone can access it.

public static void main(String[] args)

Here the method name is main which is a public method, means it can be accessed by anyone. The return type is void which means no return value. 'Strings[] args' means the arguments for the method main should be array which is to be called args. Don’t worry about the meaning of ‘static‘ as of now. We will be describing in details about it when required.

System.out.println("I didn't knew JAVA was so much fun filled");

System.out.ln ask JVM to print the output to standard output which is Linux command Line in our case. Anything that is in between braces of println statement gets print as it is, unless it is a variable. We will be going into details of variable later. The statement is ending with semicolon.

Even if something is not clear now you need not worry about this. Also you don’t need to memories anything. Just go through the post and understand terminologies and working even when the picture is not very clear.

That’s all for now. Keep Connected to Tecmint. Provide us with your valuable feedback in the comments below. We are working on the next part “class and Main method in Java”and will be publishing soon.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Avishek
A Passionate GNU/Linux Enthusiast and Software Developer with over a decade in the field of Linux and Open Source technologies.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

4 Comments

Leave a Reply
  1. Thank you so much for this explanation!

    I am so much getting the understanding through your descriptions! in the beforehand, it was awfully mis-understandable and didn’t know what I didn’t get to know.

    But, I can see more clearly. Thank you! I will look forward your posts!

    Reply
  2. Thank you for the explanations, I have already read another article before, but still confused about java. But now I’m quite understand about java. I can’t wait for next chapter :D

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.