Hello World in Java

Hello World in Java Featured Image

The next language in our quest to implement Hello World in Every Language is Java. After Python, writing Hello World in Java is going to seem ridiculous, but that’s all a part of the fun.

Table of Contents

Java Background

Once again, I took to Wikipedia to learn a bit about Java and its history.

According to Wikipedia, Java is a general-purpose language that was designed with portability in mind. In other words, compile once and run anywhere. Originally called Oak, Java was developed in 1991 where the syntax was largely influenced by C/C++.

Today, Java exists as one of the most popular languages in the industry with uses that range from database management to app development. Since its inception, Java has gone through nine iterations and two companies: Sun Microsystems and Oracle Corporation, the current owners.

As a language, Java has received several complaints. For instance, many developers despise Java for its verbosity and performance. That said, the language has many great features like garbage collection which offset some of these complaints.

Hello World in Java

As someone whose first language was Java, I can completely sympathize with how insane the following code snippet is:

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

In order to implement Hello World in Java, developers need to understand several concepts like objects, classes, main methods, and strings. As a beginner, this can be extremely confusing, so I’ll share the basics.

Before we can do anything in Java, we have to create a class. In this case, we’ve called our class HelloWorld. Convenient, huh?

From there, we need to create a main method. A main method is how every Java program knows where to start. Therefore, every program must have exactly one of these main methods implemented. Don’t worry too much about the syntax. Just know that we need a main method.

Finally, we have to output our favorite string to the command line. To do so, we have to leverage a static method out of the System package. It works essentially the same as our print function from Python. It’s just a bit more verbose.

And, that’s it. Hello, World! If you need more information, I wrote a tutorial awhile back on class structure in Java.

How to Run the Solution

At this point, we should actually try running the solution. To do so, we’ll need to grab the latest version of Java. In addition, we’ll want to get a copy of Hello World in Java from GitHub.

With everything in place, all we need to do is navigate to our folder with our new file and run the following commands:

javac HelloWorld.java
java HelloWorld

The first line actually compiles the program and the second line runs the solution.

In addition, we can try out the solution with the online Java compiler over at TutorialsPoint.

Sample Programs in Every Language

So far, we’ve learned how to write Hello World in both Python and Java. Up next, we’ll dive a bit lower into C/C++. After that, we’ll see an implementation of Hello World in C#. Stay tuned!

As always, you can share your thoughts below in the comments. If you liked what you saw today, consider sharing it with your friends.

Series Navigation← Hello World in PythonHello World in C →
Advertisements

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.