The basic structure of a Java program is as follows:
class ClassName {
}The class name must be the same as the file name, for example.
For our program, we will call it Tut1. We will call the file name Tut1.java so the structure will be:
You will notice that there are brackets after the method name. You can place variable names within these brackets which will receive the values passed to the method. I will talk more about this at a later stage.
The most common method that you will use is the ‘main’ method. The class that is actually executed must have this method in the class. However not every class must have a ‘main’ method. The structure of the mainmethod is as follows:
public static void main(String args[])
{
}Note the capital ‘S’ for String. This is important or you will receive an error.
I believe that there are many different ways of outputting text. I will use one method which uses the standardjava package.
System.out.println("Welcome To My First Java Program ");Again in this example, note the capital ‘S’ for System.
This code basically prints a line of text which is contained within the quotes. The line is terminated, like most lines in java, with a semi colon.
If we put all this code together, it will look like this:
class Tut1 {
public static void main(String args[])
{
System.out.println("Welcome To My First Java Program");
}
}This program will output the following on the screen:
Welcome To My First Java Program
No comments:
Post a Comment