Hello World

Your First Java Program

Project Setup

Let's Launch IntelliJ

IntelliJ is our Integrated Development Environment (IDE). IDEs integrate common development tasks with our editor.

Open a Java Project

• From the File Menu, select Open.

• Navigate to your 'hello-world' project and select the folder containing the project.

• Click the Ok< button.

Opening a Java Class File

• Open the Project tab on the left side of IntelliJ. (if it isn't already).

• Select the hello-world project folder at the top of the Project tab, then select src > org.wecancodeit > HelloWorld.

• Double click on the HelloWorld file and you should see the file open up in the main section of IntelliJ.

Now You've Got Class!

You should see code similar to the following in your editor:

public class HelloWorld {

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

}

Let's Break It Down

Java Classes

All Java code is found inside a class. This class is called HelloWorld.

public <mark>class HelloWorld</mark> {

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

    }

}

Java Methods

Think of Methods as the messages that we send in Java. All Java applications start with a main method.

public class HelloWorld {

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

    }

}

Curly Brackets

Curly brackets ({ and }) indicate code blocks in Java. An opening curly bracket ({) must always, always, always have a corresponding closing curly bracket (}).

This applies to classes…

public class HelloWorld <mark>{</mark>

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

    }

<mark>}</mark>

… as well as methods.

public class HelloWorld {

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

    <mark>}</mark>

}
public class HelloWorld {

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

    <mark>}</mark>

}

Comments

We can add comments to our code to explain it. They don't do anything.

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

    }

}

Here, your project template has created a comment for you to help point you to where you should add the code for your 'Hello World' project. We'll do that next.

Hello World!

A common first program for people to write in a language is a program that says "Hello, World!" That's just what we'll do. Change your code to look like the following:

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

println is short for "print line". System.out represents our console/terminal/command line. Here we are sending a println message to System.out with the content "Hello, World!"

Let's run it, and see what happens!

A Simple Greeting

Right click inside your editor (the pane where you've been modifying the code), then select Run 'HelloWorld.main()'. Your program will run, and your Run tab should display something like the following:

... Misc Java Stuff

Hello, World!

Process finished with exit code 0

Climactic, eh?

You've written your first Java program. Congratulations!

Next Steps

Java is a strongly-typed language. The type of "Hello, World!" is String, which is denoted by the double quotation marks:

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

We can also do this with other Strings:

System.out.println("I can code it!");

Other Data Types

We can also display numbers and more complex expressions.

System.out.println(42);
System.out.println(1.23);
System.out.println(2 + 3);

Try a few of your own! We'll talk about other data types and expressions in days to come.