
Let the Coding Begin
We will begin by writing a simple program that will display a message. Before we continue, you should have installed the JDK and an IDE. If you haven't done so, please visit the Home page for information about doing so..
Now let's begin. Check the code below for the first program we're going to write in Java.
public class FirstProgram
{
public static void main(String args[])
{
System.out.println("Welcome to the Java Judge Tutorial");
}
}
Output:
Welcome to the Java Judge Tutorial
Whoa! isn't that a lot of code now. Let us explain to you each line of code so that you will have a basic understanding of how a Java program is written.
public class FirstProgram
Now to note something before we begin, notice that the name of the file should be FirstProgram.java similar to the class name FirstProgram, so as you may have guessed, the class name should always be the same as the file name. Moving on, this statement declares a class named FirstProgram which will contain code to run. For now, just memorize this step because in later tutorials we will explain fully what is a Class.
public static void main(String args[])
Now here is something special. This is called the main method and it's task is to run the program. Everything within this method will execute when the program runs. Moreover, for now do not bother if you do not know what public, static, or void means. All of these will be explained later on.
System.out.println("Welcome to the MKM Java Tutorial");
As we said before, everything inside the main method will be executed. Therefore, this statement will be executed and it will do something. How about you compile and run this program and see what happens. Yes, you guessed it. This statement prints to the console whatever is written between the quotations.
Congratulations
You have finished your first Java program and got it to run. Now you can move on to the next tutorial but before please check the important notes below.
Important Notes
Always be tidy
Whenever you code, try to make your code easier to read by using tabs and spaces. A tidy code is easier to maintain and check for errors so simply do not be messy.
Remember the Braces
Always remember that every open brace "{" needs a closed brace "}". Not doing so will result in an error.
The Mighty Semicolon
Always remember to end your statements with a semicolon ; . Not doing so will also result in an error.