Lesson 3 - Creating Your First Program (Exciting)!

Creating a New Project

Upon opening IntelliJ for the first time, you are likely greeted with a prompt to create a new project. (If you are greeted with a prompt to register your software, please follow the instructions here.) Follow these steps to create our first project, which we are going to call HelloWorld.

  1. In the first box, you will assign your project a Name. This will be the name of the file in which all of the program's contents are stored. In the box, type "HelloWorld".
  2. The second box allows you to assign a Location in which your file is stored. This is defaulted as "~/IdeaProjects" by the IDE. If you'd like to save your projects to a different folder, click the file button on the right side of the box and locate the file that you'd like to store your projects in.
  3. The next setting asks us to select a Language. You should select Java, since we are learning Java. However, note that there are multiple options available. The plus sign allows you to install plugins for other supported programming languages.
  4. The remaining options are more advanced. We can leave those as default. Note that there is an option under Location which a checkbox that says "Create Git repository". This option creates a subfolder in your program that tracks changes made to any and all files in your project. Learn more here. It's not important for us to activate this feature, but feel free to do so to see how it works.
  5. Once all settings are in place, we can hit the blue Create button.

What Am I Looking At?

Now that we've created our new project, you may be curious as to what you're looking at. (If you're an experienced programmer, you may want to skip to the next lesson at this point.)

The window is divided into three sections.

  1. In the main screen in the center is the code editor window, which allows you write the code for your program. We will dissect the code in this window a bit later in this lesson.
  2. On the bottom is the Console window, which allows us to see how to code runs, along with any errors that may occur when attempting to run the program.
  3. On the left is the Project window. This is where the code for the program is sourced from and contains all files that are necessary for your program to function. It'll include files that you create as part of the program including class files, which we'll learn about later.

Now Let's Look at the Code

In order to begin dissecting the code that we see in the code editor, I must introduce a few vocabulary terms.

The Compiler is the built-in tool that converts your code into machine language. Remember, your computer only understands 1's and 0's. This is known as Binary Code. Binary Code is a programming language all on it's own, but you don't have to worry about this. The compiler helps us out by converting our code into the 1's and 0's that our computer can use.

A Class is a set of instructions that the compiler reads to run the program. I like to think of the class as a car engine or battery. You can't run your car without an engine or battery to power it.

public class Main {
insert code here
}

A class can be made either public or private. A public class means that the contents of the class can be accessed by other classes. A private class means those contents are specific to that class alone. We will cover classes in more detail later. To learn more now, see /InsertClassLessonHere/. Note that private and public carry the same definition and characteristic when used with other data, such as methods or variables. More on this later.

It is important to note the syntax of a Java class. Syntax is the specific way that a code needs to be written for the compiler to be able to interpret. We declare a class as private or public, then followed by the word "class" and the class name. Java programs have a Main class to start. More classes may be created depending on the needs of the program. In order for us to input code into the class, we must use open and closed brackets { }. The code will go inside of these brackets. Anything outside of the brackets will not run, and will likely cause an error.

Let's dive further into the class.

The Main Method is where the class's main instructions are carried out. In order to start your car, your car needs to have specific instructions to be carried out once the key is turned or the start button has been pushed.

public class Main {

public static void main(String[] args){
insert code here
}
}

Similarly, all methods have to follow the same syntax. A method is invoked and followed by brackets { }, and code is inserted into these brackets. Again, methods can be declared as public or private. Notice a new word: static. A method can be declared static or non-static depending on the use of the method. Static methods are class methods, while non-static methods are instances of the class. More on methods Math Class Library. A method also takes in arguments. In this case, the array that is referenced is a String[] array. More on this later.

Inside the method, we can provide instructions.

public class Main {

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

Notice the syntax of our code here. Any simple statement in Java must end with a semicolon ";" . In this case, we have instructed the code to run a print statement. We use the command System.out.println( ). This calls a Java built-in method, known as the print. This prints the requested code as a new line. A similar method is the command System.out.print( ) method, which prints the requested code consecutively, without creating a new line. Java has many built-in methods using the java.lang package to help us run code easily. We will run into more of these built-in methods during our exploration.

Now that we understand what's going on in our code, let's run it using the play button on top. You can also run using
{ #R}
on Mac.

In the console window below, you should see the output of your program, which should read "Hello world!".

Congratulations! You have successfully set up your first program.

Oh! Let's also look at errors for a brief moment. Errors are issues in our code that prevent our program from running how we desire, or from running at all. There are three types of errors:

  1. Runtime error: A runtime error is an error that does not necessarily look like an error. This kind of error is identified when a program runs perfectly fine, but not with the desired output. For example, if we have a program that we'd like to print a receipt after a customer purchase and the program displays a message that says "Receipt print successful" but no receipt is printed, then this could be a miscommunication with the receipt printer. While the program ran to completion, the outcome was not as desired.
  2. Syntax error: A syntax error is an error in which you make a mistake in the way that you have written your code. It can be something as simple as missing a semicolon or forgetting a closing bracket }.
  3. Logic error: A logic error is similar to a syntax error. It comes down to how you write your code. However, it does not have to do with the syntax of the code, but rather the logic in your code. For example, assigning variables to incorrect data types, or creating a mathematical program and dividing by zero.

In cases 2 and 3, the console will identify the error and give you the line in which the error was found. In case 1, it becomes slightly more difficult as you have to find and identify where the code went wrong, since the program still runs successfully.

Next, we will cover some basic data types and how we can manipulate them to store information for later reference.

Next Lesson: Lesson 4- Data Types and Variables

Table of Contents: Table of Contents