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 WikipediaOpens in a new tab. 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 JavaOpens in a new tab.. In addition, we’ll want to get a copy of Hello World in Java from GitHubOpens in a new tab..

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 TutorialsPointOpens in a new tab..

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. Otherwise, here are some related resources:

See you next time!

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

← Previous Post: [#3] Hello World in Python Next Post: [#5] Hello World in C →

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts