Understanding Java with Hello World!
While working, improving existing projects, practicing algorithm problems, and job hunting, I decided to add learning a new programming language to the mix. I enrolled in the Java Programming Masterclass course offered by Udemy in order to learn Java. I’m excited to learn this language primarily based on it’s popularity within the tech industry. So starting off with learning this language, I will go in depth in explaining how to print “Hello World!” to our terminal successfully. Below is what we will start off in order to start our solution.
class
HelloWorld
{
// Your program begins with a call to main().
// Prints "Hello, World" to the terminal window.
public
static
void
main(String args[])
{
System.out.println("Hello, World!");
}
}
There are three primary components: class component, main method, and the source code comments.
The class component is defined by typing class and the class name which in this instance it’s HelloWorld. Following the class name, we would need to use a open curly brace (“{“). Within the open curly brace, we have our source code comments that states the below code is the main method. The main method starts with
public static void main(String[] args)
public: So that JVM can execute the method from anywhere.
static: Main method is to be called without object.
The modifiers public and static can be written in either order.
void: The main method doesn't return anything.
main(): Name configured in the JVM.
String[]: The main method accepts a single argument:
an array of elements of type String.
JVM is Java Virtual Machine
Then within the main method we have System.out.println("Hello, World");
System is a predefined class that provides access to the system, and out is the variable of type output stream that is connected to the console. Output is done by the built-in println( ) method. Within the printIn() method we have “Hello World!”.
As easy as this was, this is the launchpad for me to start to understand the Java programming language and better familiarize myself with a similar Hello World problem in which I learned with Ruby and JavaScript.