Hello World Breakdown In Java
Suleiman Ibrahim
Posted on August 10, 2021
Hello World 👋.
Today, we will be going through the famous Hello World!
program in Java. Yes, the famous Hello World! program. It's more like a culture in the developer's circle to first write the Hello World
program as their first line of code in a new language. Enjoy
Introduction
Java is a high-level, interpreted, and object-oriented programming language developed by James Gosling at Sun Microsystem (now acquired by Oracle) and released in the year 1995. It's a general programming language and platform-independent (It allows developers to write once and run anywhere - WORA). This implies that compiled Java code can run on any platform and operating system that supports Java without recompilation.
Java was designed to look similar in syntax with languages like C or C++ so that application programmers would find it familiar.
Definition of terms
- Keyword: A keyword in Java is any word that is reserved for use by Java. Java has 52 of these words and this implies that these words can't be used as names for variables, methods, classes, or any other identifier. It is otherwise known as reserved keyword. It is always spelled in lowercase.
- Access modifier: An access modifier is a keyword that determines the accessibility of a class, variable, method, and other members in a class to other subclasses or objects. It can either be public, private, protected, or default. Check here for more on access modifiers.
- Variable: A variable is a location in the computer's memory that is used to store value for later use. Java is a statically typed language (variable types are explicitly declared and therefore determined as compile-time) so therefore variable types are declared before the variable.
- Identifier: A series of characters made up of letters, digits, underscores (_), and a dollar sign ($). It does not contain spaces and does not begin with any digit.
- Class: A class is a blueprint of an object. A class allows us to define the state, properties, and behaviors of our methods.
- Method: A method in Java is a block of code delimited by curly brackets ({)that performs a particular function in our class.
-
Subclass: A subclass is a class that is derived from another class. The class it is derived from is called the super class or base class. All classes in Java are by default subclasses because they inherit from the
Object
superclass. - Object: An instance of a class. Classes can have multiple instances and these instances have their separate copy of the attributes of the class.
- Data types: Data type in Java is a constraint that determines the value that a variable can take. It tells the Java compiler how to use the data.
- Instance variable/method: An instance variable creates a separate copy of itself when used inside an object. Modifying an instance variable does not affect the class it was created from. The same also applies to methods.
Code
// HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Breakdown
- Line 1
// HelloWorld.java
- This signifies a single line of comment in Java. It is used to make our code easier to understand by other programmers and for future references. Java has 3 types of comments namely:
- Single line comment: As we've seen above. They are mostly used to describe code functionality.
-
Double line comment:
/* comment here */
. It is used to comment on multiple lines of code. Documentation/Javadoc comment:
/** comment here */
. Used when writing code for projects or software packages. Check here for more info about Javadoc comment.Line 2
public class HelloWorld {
Now let's consider this step by step. Firstly,
public
- a keyword in Java and hence can't be used as an identifier. It signifies that the class HelloWorld
is public and therefore global in our program and project as a whole.
class
- also a keyword in java that informs the Java compiler that a class is about to be created. A Java source file may only contain one public class but can contain multiple classes with non-public access modifiers.
HelloWorld
- is an identifier that is used to name the class that is being created. Java has naming conventions for identifiers like variables, classes, methods, etc. The naming convention for a class in Java is the Pascal case - The first letter of every word is capitalized. Identifiers in Java also have their rules guiding them, one of which a class name must be in Pascal's case.
{
- open curly bracket, signifies the beginning of our class. Unlike other languages like Python, Java uses open curly brackets to mark the start of our classes or methods. This signifies the boundaries of our class or method and can also be referred to as the body of the class or method.
- Line 3
public static void main(String[] args) {
public
- also implies that the method has public access
- other classes can use this method.
static
- signifies that the method is only visible to this class and other classes that this particular class is offering services to e.g. subclasses. This means that objects of this class cannot access this method because it is declared static
.
Note that the keyword static is omitted, then the method can be classified as an instance method.
void
- is a return type in Java and it indicates that this method will perform a function and won't return any information. Other return types might be String
, short
, byte
int
, long
, double
, char
. These are also data types in Java. Any method having a return type other than void
must end with the keyword return
followed by a value that is of the same type as the return type
public static int age() {
int age = 19;
return age;
}
main
- is the name of the method and it is also an identifier. This is the starting point of every Java application. When we run any Java application, this is the first place it begins its execution, no matter its location in the program. The naming convention of a method is the Camel case - The first letter of every other word must be capitalized except the first word, which is in lower case. Java requires that every program must have the main method within which the program starts its execution.
(String[] args)
- is called the parameter list in Java. The parameter list is used to receive data into our methods that can only be used within the method's body. Java demands that the main method's parameter list takes in an array of String
objects and an identifier args
which is the default name by convention, is used to store the data received. Methods that take in parameters in Java are called Parametric methods.
Example: the
public static void main(String[] args)
method takes in an array of Strings and stores it in variableargs
.
while methods that don't take parameters are called Non-parametric methods.
Example:
public static String getName()
- here, the parameter list is empty.
{
- The opening curly bracket here similar to the one in our class definition also performs the same function. It delimits the method and can also be called the method's body. Any statement, variables, or expressions within the opening curly bracket and the closing bracket belong to the method. They can't be used outside the method's scope.
The method's scope is defined by the opening and closing curly brackets.
- Line 4
System.out.println("Hello world");
System.out
- an object in Java already known as the standard output object that is already imported by default into all Java programs by java.lang package. It is used to display information on the command window from our Java programs.
println("Hello world");
- a System.out
method that prints string literals within but not including the double quotation marks to the command window. Some other methods are printf()
, print()
.
Most statements in Java ends with a semicolon to signify the end of that statement.
- Line 5 and Line 6
}
}
`
are both closing curly brackets that close our method and class respectively. They signify the end and scope of the method, and the class as well.
Conclusion
Please note that the above is just a minimal breakdown of the popular Hello World!
program and it is more complex and deeper than explained above. Some of the details are not included because they need some prerequisite knowledge before they can be understood. Please refer to the official Java documentation for more details.
Posted on August 10, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.