Creating a new project using IntelliJ IDEA

This tutorial will show you how to create your first programme in Java. It assumes you have already installed the IntelliJ IDEA IDE. If you have not, download and install it. The free 'Community' edition will suffice for this module.

  1. Start by Launching IntelliJ from the start menu
  2. If you are running it for the first time, choose the default options for installation. You should end up with a screen similar to the one below, though you could see existing projects on the left hand side IntelliJ Launch Screen
  3. Choose the create new project option, and in the first screen that appears, ensure 'Java' is selected at the top left
  4. Check also that the Project SDK dropdown list does not say none. if it does, configure an SDK using this tutorial
  5. Once a project SDK is selected, Click 'next'. No other options need to be selected
  6. When prompted to 'create project from template' check the box and choose the 'Command Line App' option, it will likely be the only option
  7. Name the Project HelloWorld, check that the location of the project is suitable (amending it if not) and click Finish
  8. You should now see a screen that looks similar to the one shown here: IntelliJ project showing a main class with boilerplate code
  9. The programme you have created includes a bit of code to get you started - a 'Main' method and a 'Main' class. Whilst later tutorials will cover the concepts of methods and class, the key thing to know is that the 'main' method is the starting point for your (or any other) programme. You should see that it has the following code
    package com.company;
    
    public class Main {
    
        public static void main(String[] args) {
    	// write your code here
        }
    }
    The only thing currently inside the 'main' method is a comment - comments are ignored by the computer, and used to provide information to anyone reading the code. Anything on a line which is preceded by two forward slash characters // is a comment. You can safely modify or remove comments and there will be no effect on how the programme runs.
  10. You can now run the programme and whilst it should 'work' it won't do anything, but we can check everything is working as it should
  11. Locate the green 'play' button to the top right hand side of IntelliJ IDEA and click it. A pane at the bottom of the IDE should appear and two lines of code should appear, the bottom one saying Process finished with exit code 0 provided no errors appear, this indicates that programme has run successfully - which it should as we've not written any code to go wrong yet!

Writing code

  1. The next step is to write some code so the programme does something when we run it, modify the existing code so it appears exactly as follows (taking care to include the semi-colon at the end of the line):
    package com.company;
    
    public class Main {
    
        public static void main(String[] args) {
    	// write your code here
            System.out.println("Hello World");
        }
    }
    This line of code tells the System that it should output something, in this case a line of text containing the words "Hello World". More specifically we are calling a method, with the method's behaviour being to output some value, in this case the text "Hello World"
  2. Run the programme again this time you should see the text appear in the pane at the bottom of the IDE, and this time you should see the text "Hello World" printed out before the 'process finished' message.

Code completion

As you were typing the line of code, you may have noticed that the IDE provides suggestions for what you might want to type (a suggestion list). This helps you identify what you might want to type, and will become more useful as time goes on.

Task - more output

  1. Add another line of code, immediately after the one you previously added, so the programme outputs "Hello" followed by your name. Run the programme and verify it behaves as expected
  2. Add a third line of code, between the existing two lines, this time to to say "Hello Java", but pause after you type the 'p' after typing System.out. Look at the options in the suggestion list and select an alternative piece of code which will not create a new line at the end of the output. Run the programme and verify this has happened. If it does not, or you get an error, try an alternate option.

Task - comments

Using two forward slashes is not the only method for creating comments in Java, another approach exists which is more suitable for adding multiple consecutive lines of text as a comment. Carry out research to identify the syntax for this type of comment, and practise using it in the programme you've just created.

Task - line numbers

Invariably, we will write programmes that crash. When they do, IntelliJ IDEA will show us the line number of the line which caused the crash. Find out how to make IntelliJ IDEA show line numbers next to the code, and enable it in on your machine.