There is a long running tradition of writing a program to print Hello World! as your very first program while learning a language. The program is quite simple and if one is able to run it successfully, it can be safely assumed that the development environment is fine.
For our purposes, we will print something better and more meaningful – “Hello Coding Raptor!”. To follow along with this tutorial, you don’t need any IDEs but you need to ensure that you have installed JDK. You can use your favorite editor if you want to keep things simple. Read through the code below once, each facet of the program is explained in detail. Though the lesson is simple enough, you may want to go through previous lessons describing Installing JDK, Using Netbeans, Using Eclipse, Using Editors and Compiling & Running Java Programs.
Hello CodingRaptor! Program
Ladies and gentleman, without further ado here is your first Java program –
123456789101112 | package com.codingraptor; /* * My First Java Program * Hello Coding Raptor! */ public class HelloCodingRaptor { public static void main(String[] args) { System.out.println(“Hello, Coding Raptor!”); }} |
We will dissect it line by line and understand the meaning of each of the words.
Dissecting Hello CodingRaptor! Program
Line 1:
The very first line says that package name is com.codingraptor. A package is used to segregate code. Having package name such as com.codingraptor means your source code file must reside in a directory called codingraptor which in turn must reside inside a directory by the name com. As we will learn in the next few lessons this packaging method avoid clashes and also acts as a security mechanism.
Line 3:
A multiline comment can be written inside /* and */. Anything inside /* and its corresponding */ are ignored by the compiler. Java also supports two other types of comments – single line comments and Javadoc comments. We will be covering in them in future lessons.
Line 8:
This line has couple of important keywords. It is essential to understand the meaning of each of the words –
- public – this implies that the definition that follows has public visibilty. Having public visibility for a class mandates that it be stored in a file named same as the class name with a .java extension. So our source code must reside inside HelloCodingRaptor.java
- class – this keyword tells the compiler that a class definition follows.
- HelloCodingRaptor – this is the name of the file. If everything goes fine, this class definition will be stored in the method area to be referenced by the execution engine.
Line 9:
This line is studded with keywords. Let’s look at each of them –
- public – we have already seen this term in context of class. In case of methods, public visibility means that it can be called by anyone having either a reference to an object of this class or this class’s Class object.
- static – this keyword implies that whatever definition follows is on a per class basis. You don’t need to instantiate a class to be able to use the static methods. static methods are called on the Class object.
- void – this is the return type of the method. void return type means that nothing will be returned. void in Java is different from null. null is used to indicate that something was expected but nothing was encountered. void on the other hand signifies that nothing is expected.
- main – this is the most important Java method aand starting point of all Java programs. Java method signature includes method name and arguments, so it is not enough to have main method. The signature of the main method must be proper or your Java interpreter will complain about main method not found
- String[] args – This says that the argument to main method is an array of String with the name args. Beginning from Java 5 when varargs was introduced, you can also write String… args instead of String[] args
Line 10:
This line contains the most famous and most often used Java statement – System.out.println() . We will be covering the System class later on, for the moment suffice it to say that System.out.println prints whatever is specified as an argument and also appends a newline to it. There are other ways of printing to console in Java, but System.out.println or SOP for short is by far the most popular of them.
Other Lines :
As you might have noticed after visually inspecting the code, Java delimits code blocks using { and } . So, a class definition lies inside a { and } . Similarly, method definition is contained in { and }. As we will examine in the upcoming lessons, statements such as if, for, while which introduce their code blocks also use braces for defining their respective scopes.
Next we will examine how to compile and run the Hello CodingRaptor program from command line.