Hello World in Java

Hello World in Java

By TechCogs | Programming and Coding | 28 Feb 2020


Java is a very common language these days.  It's largely used for servers and especially for Android applications, being the main language for Android.

Java syntax is inspired by C++ but in Java, unlike in many other programming languages, everything is object-oriented.  Everything must be contained in a class.  Because of this the basic Hello World program will look a little different than in many of the other languages.

class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

In java, the filename of the code must always match the main class in that file.  For instance, the above program would have to be stored in a file called "HelloWorld.java".  The name of the class, "HelloWorld", would have to be the same as the name of the file it was in, "HelloWorld.java".

Next comes the main function declaration.  Java is fairly particular in that it wants you to name things and organize things in particular ways.  Here "public" means this function is public and can be accessed directly by anything outside of the class.  Following that is "static" which means the function can be accessed without being part of an instantiated object.

Because Java is completely object-oriented, normally to work with any variables or functions / methods within a class, you have to first create an instance of that class object.  Consider the following code.

class FunGame {
public void doSomethingFun() {
System.out.println("This is fun!");
}
}

In a normal program like the above in order to access the doSomethingFun() method we would first have to create an instance of the FunGame object.

FunGame fun = new FunGame();

Then we could call the doSomethingFun() method on the fun object.

fun.doSomethingFun();

However, if we declared the doSomethingFun() method to be static, then we could call it directly without having to create an instance of the FunGame object first.  If we did that, instead of calling the doSomethingFun() method on an instance of the object, we would call the method directly on the class itself.

class FunGame {
public static void doSomethingFun() {
System.out.println("This is fun!");
}
}

FunGame.doSomethingFun();

This is what the "static" identifier means in our Hello World program.  Because the main function is the main entry point of the program it has to be declared as static so it can be called without needing to create an instance of an object first.

The "void" identifier just means the function doesn't return a value, similar to many other programming languages.

Finally we have the name of the function itself and its parameter list.  The code "String[] args" shows the function accepts an array of Strings.  These are the arguments passed into the program from the command line.

The next line is a static call to System where we access the function that prints text to the terminal or command line.

And that's how the Hello World program is written in Java.  It may be a bit longer and more complex than in other languages but each part has a reason for being there.

How do you rate this article?

3


TechCogs
TechCogs

I work with Linux, technology, computers, and science.


Programming and Coding
Programming and Coding

The wonderful world of software engineering and software creation, centered around languages like D, Java, Python, Dart, and C++.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.