Introduction
To start this great journey, we'll use the typical "Hello World" program. Yes, it is not very exciting, not very motivational, but before we start running we must learn how to walk.
At this point you should have the JDK installed, and a text editor available. If you have no idea what a JDK is, then you should go and check the previous tutorial: Tutorial 1 - Introduction (The Absolute Basics)
Open your favorite (or available) text editor and lets start!
Hello World
Just for this example, we'll use a normal text editor instead of IntelliJ. This is just to show you how you compile java code. In the next tutorials we'll start making use of IntelliJ Community Edition, which makes it a lot simpler to compile and run your projects. But don't skip this bit, you should really know how to compile and run your code by hand!
Grab the text editor, start a new file and copy this bit of code into it:
public class Lesson1{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
After you copied this, create an empty folder, which we will be using for this particular tutorial, and save the file with the name Lesson1.java. Java is a case sensitive language, so copy the code exactly the wait it was presented before and save the file with that precise name. We'll get more into that, don't worry.
There should now be a Folder on your system, with just one file named Lesson1.java .
Remember: Java is a case sensitive language
Open a command prompt if you are in windows (type cmd.exe in the "Run" option on the Windows Start Button) , or a Shell if you are in a Unix based system and go to that Folder.
JAVA and JAVAC
To compile the Lesson1 program we are making, which simply prints "Hello World", we must first compile it! To compile we use the Java Compiler (JAVAC).
On the command prompt or shell, inside the folder where the Lesson1.java file exists, type in:
javac
If a sea of text just flashed by, then the JDK is correctly installed and you can proceed to the next step. If on the other hand there was an error message like : javac: command not found , then the JDK is not correctly installed and you should check the previous tutorial before proceeding.
On the command prompt or shell, type in:
javac Lesson1.java
If everything went right, you should now have exactly 2 files inside that folder:
- Lesson1.java - The source code
- Lesson1.class - The Compiled Java Bytecode
Now in that same command prompt or shell, type in:
java Lesson1
It should print out "Hello World"! Congratulations, this is your first program in Java! It doesn't do much, but it is a start! Lets look at the source code and understand what it all means.
Looking at the Code
As it was said in the previous tutorial, Java is a Object Oriented (OO) language, and that means that our program is structured in Objects that may or may not have data inside, and may or may not have code inside.
A Class
When a programmer writes a file like this:
public class Lesson1{
}
The programmer is in fact creating a Class , which you can think of as a Template for Objects, with the name Lesson1.
Because our "Hello World" program is really small, it only has one Class, but if it was more complex it could have many many many more Classes.
From that code public class Lesson1
, we know that we are creating a Class named Lesson1, and that Class is Public. We'll forget about the Public bit for now, but we'll get back to it once we look at Access Modifiers in the Tutorial 5 - Packages and Access Modifiers .
A Method
Then, inside the Lesson1 class (which is the bit between {
and }
, we have:
public static void main(String[] args) {
}
This is a Method, and it is called "main". We'll break it down:
public
: like in the class, it will be talked about in the next tutorialstatic
: we'll also forget about this for the moment, we'll get back to it in a few tutorialsvoid
: this means that the method does not return anything.main
: the name of the method we are creating(String[] args)
: the arguments the method receives, in this case it is an Array of Strings, called args.
This part of the method public static void main(String[] args)
is called the Signature of the method. Obviously all methods will have a signature, where:
- the name of the method is specified,
- the arguments it receives are specified,
- what it returns is specified
This method, named "main", with a signature as described before, is a very important method! It is the method that the JVM (Java Virtual Machine) will grab and execute, and therefore this is where a program starts its execution.
Notice that the class named Lesson1 starts with a capital letter. Now notice that the main
method start with a lowercase letter. This was not something random that just happened to be this way, it was on purpose. Although there is no technical reason why this is so, it is a convention, and every Java programmer knows this and will do it this way. We will adhere to it :
Names of classes start with Capital letters
Names of methods start with Lower case letters
Names of variables start with Lower case letters (except for some exceptions well see later)
Inside the method we find:
System.out.println("Hello World");
In simple terms it is a Statement that prints a line with "Hello World" to the console (or command prompt, or shell) .
In sightly more complex term (which you don't need to completely understand now, and will be explained later on) we are calling a method called println
that exists inside a variable called out
, that exists inside a System
object. We know this because of the naming conventions, and it will all make a lot more sense really soon.
So, what do we know up to this point?
- A Class is a template for Objects
- A Class name always starts with a Capital Level
- A Method has a signature that defines the input and output
- A Method is named with a starting lowercase letter
- A Class is compiled using the Java Compiler javac
- A Compiled program is run using the Java Virtual Machine: java
Next we'll talk about variables and just a little bit more OO theory. We are about to start the good stuff!
Exercises
To get a bit more practice, consider doing the following exercise:
- Create a class named Exercise1 that prints "My First Exercise" when it is executed.
Next tutorial: Tutorial 3 - Variables