Project Setup
Let's Launch IntelliJ
IntelliJ is our Integrated Development Environment (IDE). IDEs integrate common development tasks with our editor.
Create a Project
Make it a Gradle Project
Name your project hello-world
New Java Class
• It will take about 30 seconds for Gradle to build your project environment.
Name your Java Class HelloWorld
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) {
// TODO Auto-generated method stub
}
}
Let's Break It Down
Java Classes
All Java code is found inside a class. This class is called HelloWorld
.
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Java Methods
Think of Methods as the messages that we send in Java. All Java applications start with a main
method.
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
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 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
… as well as methods.
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
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) {
// TODO Auto-generated method stub
}
}
Here, Eclipse has created a `TODO` comment for us to remind us that we need to do something—namely, write some code inside the method! 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 As > Java Application. Your program will run, and your Console view should display something like the following:
Hello, World!
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.