Following the “Hello, World” trend, let’s create a new IntelliJ project.
Create a new folder to hold your Java practice files. Since you will be
creating lots of small projects as you move through this course, we
suggest that you also add sub-folders with names corresponding to the
related chapters and projects. Something like
java-practice/chapter-name/project-name
.
Select the New Project option from the welcome screen.
Clicking New Project opens a window with a series of project settings to select. This window is called the new project wizard.
The SDK allows IntelliJ to compile our Java code when we launch our programs. Click Next in the lower right corner of the window to continue selecting settings.
In the second window, select Create project from template. This gives us some of the Java project scaffolding to save us some time with project infrastructure.
On the next window, enter HelloWorld
for the name of the project.
Click on the “3-dot” button to select a location to save the project. Here you can
choose the Java projects folder you created in step one. You do not need to change the
base package.
Click Finish to create the project. Below is the view of your new project. Click on the item labelled Project with an icon that looks like a file folder on the left of the project window.
The section on the left is the project’s file tree.
Clicking the triangle next to the project name, HelloWorld
, displays the src
file,
followed by the base package we created, and finally our Main.java
file.
Main.java
is also opened on the right in this initial project view.
In line 1, package com.company
, establishes a package, which Java uses to help
organize and encapsulate our code.
We’ll dive into the use of a main
function and Main
class later. At this point,
let’s just get right to printing our greeting. Where the project template tells you to write your
code on line 6, add the following:
System.out.println("Hello, world!");
Ok sure, we haven’t gone over this exact syntax yet. But you can take a guess at what this line will do.
To run your program in IntelliJ, you have several options.
You can click on either of the green arrows indicated above, or choose Run from your top menu bar.
Once run, IntelliJ will generate a third panel in your view, with your program’s output:
This is just the start of your relationship with IntelliJ. Now that we know the fundamentals, let’s return to Java basics so we can start writing more code.
Question
Given the code below, which line is responsible for printing a message?
1 2 3 4 5 6 7 | public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
|
Question
In the sourcecode above, which line is responsible for defining the class?