KnowledgeBoat Logo

Hello Java

Our First Java Program

ICSE Computer Applications



Now that we have discussed Object-Oriented concepts and have developed some basic familiarity with Java, we are ready to look at our first Java program.

/* 
    First Program of KnowledgeBoat's Computer Applications course
    Name this file "HelloJava.java" 
*/ 

class HelloJava { 

    //Your program starts with a call to main() 

    public static void main(String args[]) { 

        System.out.println("Hello Java!!!"); 

    } 
}

This is a short and simple Java program which prints Hello Java!!! on the console. Although this program is short, it includes several key features common to all Java programs. We will go through the program line by line and understand these key features of Java programs. Don’t worry if you get confused and don’t understand all the things in this program. This is just an overview of the concepts which will help us get started. We will discuss these concepts in depth in subsequent lectures.

/* 
    First Program of KnowledgeBoat's Computer Applications course
    Name this file "HelloJava.java" 
*/ 

These lines at the start of the program form a comment. The compiler ignores comments. Comments are most commonly used by programmers to document their code. Think of comments as notes that you can write in the source code of your program. You can use comments to explain the logic of your code. It will help anyone who is reading the code to better understand it.

Java provides 3 types of comments. This one is a multi-line comment. It starts with a forward slash and an asterisk /* and ends with an asterisk and forward slash */. Anything between these 2 comment symbols makes up the text of the comment and is ignored by the compiler.  As the name suggests, a multi-line comment can be several lines long.

Moving on to the next line,

class HelloJava { 

it begins with the keyword class. The class keyword tells the compiler that a new class is being defined. HelloJava is an identifier that is the name of the class. The complete class definition including all its members are enclosed between the opening and closing curly braces. In Java, all programs start with a class. No program activity occurs outside classes. We will take a closer look at identifiers and spend a lot of time on classes later in the course.

Let’s go to the next line.

//Your program starts with a call to main()

This is a single line comment. It begins with a double forward slash // and ends at the end of the line. You can use multi-line comments for longer descriptions and single line comments for brief line-by-line explanation of code. We have now seen 2 types of comments single line and multi-line. The third type of comment is known as documentation comment.

Moving on to the next line,

public static void main(String args[]) { 

it begins with the main method. As the name suggests, the main method is the main entry point to your program. Program execution begins with the main method. Java is case sensitive so write the word main exactly as I show it here. If you change the case of main then Java will consider it as a normal method not the main entry point to your program because of which your program may not run.

The public keyword is an access modifier. It controls the visibility of class members. We make a class member public by adding the public keyword before its declaration. We can access public class members outside the class where we declare them. We need to make the main method public because it will be called by code outside of its class when the program is started.

Next comes the keyword static. When we declare a method inside a class as static, we can call it without creating the object of that class. We need to make the main method static because Java Virtual Machine (JVM) will call it to start the program even before any objects of the class HelloJava are created.

Methods can return a value. In Java, methods need to explicitly state in their definition, what kind of value they will return. Will they return a number or will they return a string or will they not return anything at all? The main method will return nothing which it tells the compiler through the keyword void in its definition.

We can pass information to methods in the form of variables. These variables are specified inside the set of parentheses that follow the name of the method. These variables are called parameters of the method. main method accepts one parameter named args which is an array of strings. You must be wondering what is this array of strings, what does it even mean. And who supplies values for args parameter? This is a relatively advanced concept to explain in this overview. You need to have some understanding of arrays and command line arguments to comprehend this. For now you can ignore this, we won't be using args in next set of programs we write and I promise to you, I will explain this once we have covered arrays.

The opening curly brace { starts the body of main method. All code of the method is enclosed within its opening and closing curly braces.

Moving on to the next line,

System.out.println("Hello Java!!!");

it prints Hello Java with three exclamation marks on the screen by using the built-in method println. The println method prints the string passed to it on the screen followed by a new line so that the next output on the screen is printed on the next line. The line begins with System.out. System is a pre-defined class of the Java standard library that provides access to the system and out is the output stream that is connected to the screen. Don’t worry if the last sentence didn’t make any sense to you. Just remember that we use System.out.println method to output some string on the screen.

The next 2 closing curly braces mark the end of main method and HelloJava class, respectively.

That brings us to the end of our first Java program. In case you felt that you didn’t understand all the things in the overview of this program and are not comfortable with it don’t worry. It is perfectly fine to feel that way at this stage. We have just looked at our first Java program. We have the entire course ahead of us to dive deeper into each of these concepts and understand them properly. So congrats on your first Java program, you should feel happy about it. In the next lesson, we will learn how to execute this program in BlueJ.

PrevNext