Java is a high-level programming language that has been developed by Sun Microsystems and it was released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
Below is an example of a program written in Java syntax.
// This program will display the message Programming is great fun
public class Animals
{
public static void main(String[] args)
{
System.out.println(“Programming is great fun”);
}
}
Now lets learn what each statements means.
PROGRAM FEATURES/ BASIC SYNTAX
- Classes
In programming a class is defined as a collection of variables or methods.
Any Java program is composed of one or more classes, In the example above there is one class named Animals.
The following are rules on how to name a class
- Write one class per file
- Class names begin with an upper case letter (Is known as Upper Camel Casing) Like in the example above we named it Animal.
- Java is a case-sensitive language. This means that characters written in Uppercase and lowercase are different for example Animal is different from ANIMALS and different from animals.
- The file saved on disk for this class should be saved as Animal.java (ClassName.java)
NB: A java file may contain many classes but may only have one public class. But If a .java file has a public class, the class must have the same name as the filename.
2. Methods
A method is a series of statements. Each class may contain a number of methods. In the Animal example, there is one method called main.
The following are rules in naming a method
- Method names begin with a lower letter (Lower Camel Casing)
- Java programs begin with the method named main
3. Statements.
A statement is a single command. Each method may contain any number of statements.
In our Animal example, there is one statement that is used “System.out.println(“Programming is great fun”);”
System.out.println() and System.out.print() statements are used in java to produce a console output
At the end of each statement they should be terminated with semicolons. Ignoring the semicolon will return an error.
4. Comments
Comments are used for people to read (not used by the computer). They inform the reader of what the code does. Comments are usually ignored by the compiler.
In our Animal example, there is one comment // This program will display the message Programming is great fun
The two slashes (//) at the beginning of the line indicates that this is a comment.
Comments are ignored by the compiler
