Kevin's Guides
Decorative intro photo for: Chapter 2: Hello, World.

Chapter 2: Hello, World.

Hello, World.

This chapter will introduce you to the IntelliJ IDE. We will explore the basics of building a program called Hello World, which simply prints the message "Hello, World." out to the console. This is a classic first lesson for new programmers - a time-tested tradition dating back to at least 1972[1]. Although it may be unoriginal to use this exercise, we're going to stick with tradition and plow forward. All the early programs in this series will be console based, so knowing how to print to the console is a must!

If you have any issues viewing the images in this section, you may click them for an enlarged view, or to download the full size image.

2.1: IntelliJ IDEA – Creating a New Project

First, download and install IntelliJ IDEA if you have not done so already. If you have selected to use a different IDE you may skip the part on IntelliJ. Just follow the instructions for creating a new project in your respective IDE.


2.2: IntelliJ IDEA – Layout Overview

Look at this screenshot which shows some of the key features of the IntelliJ Editor.

As you can see, a file has already been created for us. This is a Java source file. As you might expect, the “Main” source file will be the one we start off with – the entry point to our program.

overview of the intellij interface

Let’s inspect the Project Explorer on the left side of our IDE so we can see how our project is structured. We can see IJ has created a base “HelloWorld” folder. In this folder, there’s another folder called “src” which is short for source code. This is where we will keep all our source code files for this project.

overview of intellij idea project explorer

IntelliJ then further breaks down the source folder based on package name – so a com folder, kevinsguides folder, and helloworld folder in this case. The blue C icon next to the open file in the package explorer means this is a Java class file. Classes are the building blocks of objects in Java. You could think of the Main class as being the Main object for this program.

Additionally, there is a .idea folder – this is a folder created by IJ to store project settings and such. You should not need to change anything in the .idea folder.

2.3: Structure of the Generated Main Class

You’ll notice that our Main Java class already contains some basic code. IntelliJ added this when we created a console project from a template, so we can easily jump in. The template has structured the class file for us, declared the package name, and generated the program’s main method. Let’s examine what each line is doing.

package com.kevinsguides.helloworld;
public class Main {

    public static void main(String[] args) {
        // write your code here

Classes, Methods, and Statements

The first line, package com.kevinsguides.helloworld; is a package declaration – which is where we declare the name of the package. Because we told IntelliJ to use com.kevinsguides.helloworld as our base package, IntelliJ has automatically added this line in for us. This is also a statement because it ends with a semicolon. Statements are complete commands that are meant to be executed by the CPU. Here, the command is telling the computer what package this file is part of.

diagram showing parts of the pregenerated code

The third line public class Main { is the start of our Main class. We are telling Java to create a public class called “Main” and then anything between the opening and closing curly brace is part of that class. Note that this line does not end with a semicolon – we are not telling the computer to do anything yet, we are just giving the class some structure. Classes start with opening curly braces (line 3) and end with closing curly braces (line 8). Note that the name of a class should be the same as the name of the file it’s in – so the Main class is in a file called

The fifth line, public static void main(String[] args) { is the start of our main method. A method in Java is a set of code statements are grouped together to perform a certain operation. Methods should be named based on the operations they perform, and you may also hear them referred to as functions. The statements contained in a method are executed lineraly from top to bottom. Like classes, methods are contained within a set of {curly braces}. The main method is the entry point to a Java program. Java looks for this method when it starts executing the program and any code entered here is executed first. We will cover the specific details of how the main method works in a later chapter.

For simple programs like HelloWorld, all the statements we make will be within the body of the main method. The bulk of our code for the first few chapters will take place in the main method, though we may be calling or using other methods accessed from outside classes. Down the road, we will learn to create more advanced applications that use multiple methods and classes.

how java projects are organized into packages and classes Methods are parts of classes, and classes are grouped by packages in Java projects.


Finally, on line 6, we see //write your code here. The two forward slashes mean this is a comment. Comments are sections of code ignored by the Java compiler when writing programs. They are used to take notes and explain what is going on. It is a good practice to use comments in your programs so you can understand what your code is doing when you come back to it later, or to explain what the code is doing to people reviewing your work. This is just a placeholder IntelliJ put in to tell us that this is where you start coding.

Comments can be written in a single line or block comments can be created using forward slashes and asterisks as shown below.

//this is a comment in a single line
/* this is a 
multi line comment
block that Java will also ignore */


highlighting keywords in a java program

Study the code again. In the diagram, notice all the words outlined in purple. These are all keywords. Keywords are words reserved by Java that you cannot use to name things. This means, for example, that you cannot call your class file “package” or your method “class.” You can’t give your package the name public. We cannot use them as names because Java is using them for something else.

Case Sensitivity

When coding, always remember that Java is case-sensitive. Case-sensitive means it makes a difference when you’re typing if you use capitalized or lower case letters. A method named “Main” is not the same as a method named “main”. If you accidentally change the capitalization of any words, your program may run into errors.

We must also follow certain naming conventions when writing Java programs. We name classes with Pascal Case, which is a naming system that starts each word with a capital letter and has no spaces between words. So, for example, if we were to create a class file representing a pirate ship, we might call it public class PirateShip.

In other situations, such as method names, we use camel Case. camel Case starts with the first letter of the first word being lower case, and every word thereafter beginning with a capitalized letter. This is why the word “main” in public static void main(String[] args) is written lower case. If it was a longer method name, it might look like this: thisIsAnotherMethod(){

Here’s a final example: ThisIsPascalCase and thisIsCamelCase

2.4: Our First Program & The println() Method

Our first program is called HelloWorld. It will probably be the simplest, most boring program you ever work on. All it does is one thing – print the message "Hello, World." to the computer’s console.

So we just need to start writing some code where it says // write your code here

The line of code we will add to the main method is as follows:

System.out.println("Hello, World.");

The entire file should now look something like this:

package com.kevinsguides.helloworld;
public class Main {

    public static void main(String[] args) {
        //print a message to the console
        System.out.println("Hello, World.");

When we press the start/play button in IntelliJ, it should look like this:

executing a java console program in intellij idea

Note that the line of code System.out.println("Hello, World."); ends with a semicolon, like the very first line. This means that this is another statement. We are commanding the computer to print out a message that says “Hello, World.” Also, note how everything is indented – the class is on the outside, the main method is indented one tab space, and the contents of the main method are indented two tab spaces. You don’t technically have to indent like this for the code to work, but it is a highly recommended practice as it makes the hierarchy of code groups easy to determine.

So what’s going on with the code? The first thing to look at is the System.out object. We’re already using objects! As was explained before – objects are essentially just things in Java that can do more things for us. In this case, the System.out object represents the standard output stream – where consoles applications send their text. So we are accessing the output stream and calling its println method. The println method is basically a function of the output stream which allows us to print a line of text to the console. println stands for print line. It takes a string, or sequence of text characters, encapsulated by quotation marks as its argument. An argument is a piece of information we pass on to a method.

an analysis of the hello world print statement in java

So in summation, in this statement we are calling the println method of the standard output stream and passing it an argument with the message “Hello, World.” as a string of text.

Println With No Argument

What happens if you use the println method, but don’t give it a String as an argument? What if we just leave the argument blank?


If we use the println method with no argument it will simply print out a blank line, as is shown below:

System.out.println("Hello,"); //First line
System.out.println(); //Create an empty line
System.out.println("World."); //Third line

The Debugger & Troubleshooting Issues

If you get any error messages they will be displayed in the lower area where the terminal is. The Java Development Kit includes a debugger, which is the software used to help trace and report errors. If you have any errors, they will be reported by the debugger with some sort of explanation as to what went wrong. IntelliJ automatically displays messages from the debugger when things go wrong.

If you encountered any errors, you should go back and double-check that everything is typed correctly, that every statement ends with a semicolon, and that you typed every word in the proper case. For example, if you typed system.out.println instead of System.out.println – your program will not work. The screenshot demonstrates this.

IntelliJ even colored the word system in red to indicate that something’s wrong. There are red underlines all over the project explorer indicating what files have issues.

the intellij idea debuggers provides information about errors and exceptions that occur when running programs
Common Issues

Here are some of the most common errors you might run across:

  • Misspelled Words
    • If you type a method name wrong, like System.out.prntln() instead of System.out.println() your program will not work.
  • Forgetting Semicolon
    • All statements must end with semicolons. If you forget one or more your program will not work.
  • Forgetting Keywords
    • Keywords like class, public, static, etc. are essential to the function of a program. If you forget to type the static keyword in the main method your program will not run.
  • Failing to use curly braces properly
    • All classes and methods must begin and end with an opening { and a closing } curly brace. Forgetting one or accidentally adding one where it does not belong will make your program fail to run.
  • Logic Errors
    • These are harder to solve because they might not be caught by the debugger. Things like using the wrong formula to calculate something, or not doing things in the proper order could be considered logic errors. Your program may behave unexpectedly because you did not account for something properly in the logic when you designed it.
    • Even experienced programmers have logic errors sometimes. It can take a lot of hunting in large projects to figure out exactly what’s going wrong.

If you have tried all of the above steps and you still cannot get your program to run, try copy pasting the entire program. Make sure your class file is named Main in the project explorer, just like the example image. The package declaration on line 1 is not really necessary in this example, so you may remove that. If nothing works, you may try to download the entire project and import it into IntelliJ. Finally, there may be a more technical problem with your installation of the JDK or JRE. Visit Oracle and try to download and reinstall the JDK. You may sign into Kevin's Guides and post a comment using the form at the bottom of this page for any additional issues.


So how did I know to use System.out.println() in order to print this message to the console in the first place? Well I memorized it. In time, you will memorize a lot of commonly used methods from the API. However, if you need to look something up everything is explained in the Java API Specification. This documentation provides details about what each pre-built object in the API is used for and what each of their methods do. If you forget a particular method name or don’t know how to do something – Google is your friend. Understanding how to look at the API and read documentation to figure things out is an essential skill for programmers.

2.5: Additional Print Methods

In addition to println, there are several other methods we can use to print text to the output stream. They are the printf() and print() methods. Let’s take a look at the differences between them.

First off, println prints out a line of text. The sample code below contains two println statements.

public static void main(String[] args) {
System.out.println("Hello, ");

The output of that code in the terminal is as follows:


Note how the words “Hello” and “World” are on separate lines. That’s because the println method always starts printing on a new line.

Now let’s compare this to the print method, which does not add a new line.

public static void main(String[] args) {
System.out.print("Hello, ");

The output of the code using print instead of println is “Hello, World.” all on one line as shown below:

Hello, World.

Finally, we have the printf method – which stands for printing a formatted string of text. We’ll get more into the details of this later, but for now understand that it behaves similarly to the print method.

public static void main(String[] args) {
System.out.printf("Hello, ");
Hello, World.

Note that if we use the print or printf methods with no arguments, the program will crash. We can only use the println() method with no arguments to print a line of code, as there's no point in printing nothing.


The assignment for this chapter is to try out all the code in this chapter. Create your project and try modifying the HelloWorld code to print out whatever messages you want. Try each different type of print method. Basically, as long as you got your message to print to your terminal without errors, you have completed this chapter.


This concludes Chapter 2. In this chapter, we covered how to create new Java projects in IntelliJ IDEA, how to print to the console using print methods, and basic debugging tips. In the next chapter, you will learn how to use variables to store and manipulate information.

Chapter 2 References
  • The earliest recorded instance of hello world as a training exercise was in 1972, in a tutorial for the B programming language by B. W. Kernighan. It was published in 1973. The use of the program may predate this though, as there is evidence that it was used in even older languages, but perhaps not for training purposes.

Also see: Appendix 1: Series Credits

Be the first to comment on “Chapter 2: Hello, World."

Login to post a comment

Related Guides

Chapter 4: Decision Making with Control Statements
You will learn how to use control statements (If/Else/Switches) to make decisions in Java. You'll also learn about the equals method and build a tax calculator.
Chapter 1: Introduction to Java
This section discusses key components of computers and how the Java programming language works. You will also learn about the tools developers use to code.
Chapter 3: Variables, Scanners & Strings
This chapter explains core Java concepts such as variables, data types, strings, and scanners to read user input.
Chapter 5: BigDecimals, Mutability & Basic Memory Concepts
This chapter discusses how to use BigDecimals to perform precise calculations, along with an introduction to mutability and how the JVM handles memory.
Chapter 6: Methods
This chapter discusses how to create your own methods, void methods, and return methods. You will also learn a little bit about scope.
Chapter 7: Loops
Learn how to use different loops in order to do repetitive tasks. The seventh free chapter of KG's Intro to Java series.
Main Menu
Kevin's Guides
Full size image will appear here.