Skip to content

Your first line of code

Display "Hello World"

  • Lesson
  • 2 min read
  • 1 of 15 in this chapter

What we see

Create a new project and name it HelloWorld. We always want our projects to have the first letter capitalized. If you make a new project and get a blank slate type this in…

Java
public class HelloWorld {
 public static void main(String[] args) {
 
 }
} 

The part that says ’public class HelloWorld’ is the set of code that we are in. The basic syntax for the main part of a java file is this ‘public class FileName’. The rest of our code will mostly go inside the curly braces ‘{}’ that is next to the ‘public class filename’. The ‘public static void main(String[] args)’ shows us where our main function is. Inside the main function is where we put our runtime code ‘code that runs one time’. All code runs sequentially, so the computer reads the first line, then the second line, then the third,etc.

Hello World

To print an output onto our terminal we use the print or println command. Thus we can output our code onto the screen by using this command in the “main” area

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

And we should get an output like this…

Java
Hello World

Congratulations, you just printed out your first line of code!

What is the difference between print and println?

With the “print” command, the cursor is put right after the output. In the “println” command the cursor is moved to the next line.

For example:

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

The output would be...

Java
Hello WorldHello World

But when we use the “println” command…

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

The output would be...

Java
Hello World
Hello World