Java

TOPIC 05 – YOUR FIRST JAVA PROGRAM

 

 

LESSON NOTE

 

 

WHICH IDE SHOULD I USE?

 

During the lessons, Mr. Campeau will likely suggest an IDE to start with.  However, if you really prefer using a different IDE, then you are welcome to do so.

 

FILENAMES

 

Some IDEs require that you enter the source code’s filename before you even type in the source code.  So we will discuss filenames right now.

 

FILE EXTENSION

 

Java source code files end with the .java extension.

 

FILENAME VS CLASS NAME

 

The filename you choose has to match the class name that you put in your source code.  There are certain rules that apply to class names and hence apply to file names.  We will look at this more closely later on.

 

For now, just remember that they have to be the same.

 

JAVA PROGRAM TEMPLATE

 

Here is the template for all simple Java Programs:

 

public class FirstProgram

{

   public static void main(String[] args)

   {

      //All code goes here (for now)

   }

}

 

Note that the above template would work in a file called FirstProgram.java.

 

JAVA PROGRAM TEMPLATE EXPLANATION

 

Here is an explanation of the template:

 

At first, the template may seem cumbersome or messy, but you will quickly get use to it.

 

OUTPUT STATEMENT

                                                       

In java, we use the following statement to output a message to screen:

 

    System.out.print("Hello world");

 

If we want the cursor to be placed on the next line after outputting the message, we use:

 

   System.out.println("Hello world");    

 

It is easy to forget the semicolon at the end.  Also, Java is case sensitive to make sure that everything is exactly the same.

 

SIMPLE PROGRAM

 

We can now write our very first simple program in Java.  We simply write the template and place an output statement at the appropriate location.

 

public class FirstProgram

{

   public static void main(String[] args)

   {

      System.out.println("Hello world");

   }

}

 

INDENTING

 

Notice how the code inside the curly brackets is indented by three spaces.  This is considered good programming practice and should always be done (even if the compiler won’t complain about it).  Be sure to indent code in your programs!

 

COMMON ERRORS

 

  • The filename of your source code must match the class name.

  • Java is case sensitive.  The word Print and print are different.

  • The semi-colon is needed at the end of most statements inside a method.  However, no semi-colon is used at the end of the class and function declarations in the template.

  • The Java Virtual Machine uses a .class file.  If you did not compile your program before trying to execute it, you will get an error.

  • After updating your code, be sure to compile it.  Otherwise, you may be executing an old copy of your program.

  • Do not forget your curly brackets.  While some textbooks and websites maybe prefer that you organize them differently, it is best that you follow the way shown in the template.