How do I create a Java Program? – J002

by | Apr 11, 2015

DeegeU Course

This “How do I create a Java Program?” is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on the “Free Java Course Online” syllabus.

Try at home

  1. Type in the Hello World program
  2. Try creating errors in your program and compiling it


When learning a new programming language, we usually start with a simple Hello World. There are several reasons for this. The first is its a very simple starting point. It’s so simple, it’s really difficult to mess up. The second is to give a simple program that can be compiled to test your environment. If we can compile and run this program, its going to eliminate other possible problems. So let’s go look at Hello World. We’re going to ignore most of the details. For example we’ll cover the class parts of hello world later.

In this lesson we’re looking at the different ways you can leave notes to yourself in code. These are called comments. You include them to describe your code. It makes it clearer what you are trying to do. There’s no affect on your application. They’re ignored and get removed when you compile.

We’ll also look at how to compile your Java application.

You need to know what compiling produces.

How to run it.

And we’ll also cover a few ways to get stuff to and from the user.

A Java comment can be created in two ways. If you want a single line comment, you use two forward slashes.

// This is a single line comment

Two forward slashes say ignore the rest of the line.

/* This is a multi line comment
   that spans
   several lines */

When you want a comment that spans multiple lines, you need to use a forward slash followed by a asterisk. Then everything following is a comment until the compiler reaches a asterisk followed by a forward slash. Confused? That was a mouthful. Lets look at this in the hello world program.

To start, lets create a java file. It’s just a text file. We need to name it where the H and W are capitalized. The case is important, because it needs to match the name inside the file.

Do a listing, and it looks like the file is there. Time to edit it.

Java comments - Free Java Course Online - Create a Java program video

Comments can be on one line, or span multiple lines.

I’m opening the file in Textwrangler. You can use what ever text editor you like. In future lessons we’ll be using an IDE. We just want to make sure Java works. If we do it in an IDE and there’s a problem, we couldn’t be sure it was the IDE or Java. Ok the document should be blank.

The first thing I’ll add is a multi line comment. This is a comment that spans several lines. It starts with a slash asterisk, and then ends with an asterisk slash. The whole comment goes from line 1 to line 10. The compiler will ignore anything you put between the start and end. You can add anything inside.

The other comment is a single line comment. It’s just two slashes. If you put comments on the next line, Java won’t know what to do with it. It will be an error. This is just for one line.

So the next bit I’ll show you, we haven’t really covered yet. Unfortunately there’s no way to create a Java program without this. Just know we’ll cover everything in detail later. We’re adding public class HelloWorld with two braces. The braces note the beginning and end of our class. The name HelloWorld must match the name of the file. That’s why I said earlier, case is important.

public class HelloWorld {


Inside the braces we’ll add our program. The start of every Java program is a method called static public void main. This has braces to start and end the method too. Again we’ll cover this in detail later, just know this is the place where Java starts to run code. The part between the parentheses is important as well. Basically that’s the command line arguments we can pass to our program. Again, something we’ll cover later. The important take away is, Java starts running here.

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


To print things to the screen we use a method called system dot out dot println. The ln at the end is read as line. That might not make any sense to you. This is one of those chicken-egg things. We haven’t covered classes or strings yet. We will, but just know when you call this, what ever is inside the parenthesis gets printed. This is what we use to print hello world.

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

Inside the method braces, add the statement to print to the screen.

To compile our program we type

javac hello world dot java

We should get no messages. That’s a good thing. When we do a listing now, there are two files. The java file and a class file. The class file contains instructions for the JVM to run. It’s the result of compiling the Java file.

To run the program we type java hello world. The case is important, but we don’t need to add the dot class part. Java is smart enough to figure that out. It should print Hello world and then return to the prompt on the next line.

Now what if we wanted it to say something else.

Let’s change the phrase to say Hello Gracie. We just change it. Save the file. Recompile it using the same javac command. And run it again. Now it says Hello Gracie.

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

The last thing I want to show you is what happens when you have an error. Let’s introduce an error into our code. I’m going to remove a semicolon and try compiling.

Now lets go back and compile. When we compile we get an error. The error says a semicolon is expected. It even tells you the line the error happened.

If we put the semicolon back it should compile again.

And it does.

Now what would we need to change, if we wanted to ask the user their name, and then say hello to the user. We need a way to get input from the user.

To get input, we simply use this code. Again it might look like alot. Just know that when we use these lines, it prompts the user to enter something. Let’s change our application to ask the user’s name. Then when they hit enter, we will print hello name. Name is the text the user enters. It will be clearer once we do it.

OK first let’s clear out the existing code of our program. Next we’re going to add a line to create something called a scanner. This is an object can parse primitive types and strings. We’re parsing things entered from the command line. You don’t really need to follow this, just know these statements get input from the command line.

The next line gets the text the user enters. Finally we print out hello whatever you typed in. Lets try compiling and see it in action.

public class HelloWorld {
    public static void main(String[] args) {
        java.util.Scanner scanIn = new java.util.Scanner(;
        String userName = scanIn.nextLine();
        System.out.println("Hello " + userName);

Enter a name, and there it is. Hello DJ. And on that note, lets go to the next lesson.

Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Get the code

The source code for “How do I create a Java application? ” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don’t miss another video!

New videos come out every week. Make sure you subscribe!



DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This