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 IDEA (from the start menu on Lab PCs)
  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 right hand side IntelliJ Launch Screen
  3. Choose the create new project option, and in the first screen that appears, ensure 'Java' is selected as the language
  4. Check also that the JDK dropdown list does not say none. if it does, select one from the list, or add one, using the option at the bottom. Orackle OpenJDK, version 19 would suffice. Alternatively you can find moe about how to configure an Java SDK (JDK) using this tutorial
  5. Check the 'Add Sample code' box
  6. Click 'Create'. No other options need to be selected
  7. When prompted to 'create project from template' check the box and choose the 'Command Line App' option, it will likely be the only option
  8. Name the Project HelloWorld, check that the location of the project is suitable (amending it if not) and click Finish
  9. You should now see a screen that looks similar to the one shown here: IntelliJ project showing a main class with boilerplate code
  10. 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
    public class Main {
    
        public static void main(String[] args) {
            System.out.println("Hello world!");
        }
    }
  11. Replace the contents of the line with the text System.out.println("Hello world!");, and in it's place add // write your code here
  12. Now, 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.
  13. 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
  14. 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):
    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.