Java- Basic Syntax, Naming Conventions, Keywords

Code Computer Skills Java

In this article, you will start with writing programs in Java. You will get an overview of the basic syntax, naming conventions and keywords in Java.

I hope you have got NetBeans or Eclipse installed in your system. If not, refer to this article on Java environment setup. If you don’t have any IDE, you can still try this code with JDK itself.

Components of a simple java program:

A java program should always have the following features:

  1. Class
    A java program should always have a class. The statements to be executed should always be written inside a class.
  2. Main method: main()
    The main method in Java is written as the follows

    public static void main(String[] args){
        // Your code
    }

    Java application starts running from the main method. It is mandatory for a java application to have the main method.
    (We have an article dedicated for detailed analysis of the main method. You can refer to that article to exactly understand what each word of the main method mean)

  3. An output statement ( generally used -> System.out.println() )
    This statement is to display a message or any value of a variable on the console or output screen.
  4. A semicolon at the end ;
    All the statements in a program should end with a semicolon (;).

Let us now proceed with writing a basic program.

Steps to write and execute a Simple Java program :

  1. Write down the following code in notepad.
    public class HelloJava {
        public static void main(String[] args) {
            System.out.println("Hello Guys!");
            System.out.println("Welcome to the world of Java!");
        }
    }
  2. Save the file with “.java” extension. For this program, save the file as HelloJava.java.
  3. Open the command prompt and navigate to the folder where you have saved this file.
  4. Once you are into the folder, you can compile and run the program.
  5. To execute the program, type the following commands.
    • javac filename.java : For compiling the code
    • java filename : For running the code.

HelloJava output in terminal

Here is how my output looks.

Note: The filename and the class name should be the same.

Now let me tell you, what each of the above statement means.

  1. Class name for our program here is: HelloJava
  2. Our main method is: public static void main(String[] args) {}
  3. Output statement:
    I have used System.out.println(). It prints the exact message written inside the “double quotes” on the screen.
    For this program

    // println gives a line break after the statement
    System.out.println("Hello Guys!");
    System.out.println("Welcome to the world of Java!");

    Output:
    Hello Guys!
    Welcome to the world of Java!

  4. A semicolon at the end ;
    For instance, System.out.println(“Hello Guys!”);

There are some conventional rules for writing a Java program. We will list it below.

Basic Naming Conventions for a Java Program:

There are some basic traditional conventions that are followed worldwide. Keep a note that violating these rules will not result in any errors. But for good programming practice, you should follow these rules.

  1. Class:
    The first and foremost rule is every java class has a class. And the class name is the same as the file name.
  2. Class Name:
    The class name usually starts with an upper case letter.
    For example:
    class Shape , class DataIf you want to combine 2 words to form the class name, the first letter of each word should be in upper case, using CamelCase convention.
    For example:
    class HelloJavaclass WriteReport,  class AddNumbers
  3. Method, Variable Name:
    In the case of writing, variables, methods and package names, the first letter is in lower case.
    For example:
    int result; string name;
    If you want to combine 2 words for naming purpose, write the first letter of the following words I upper case.
    For example:
    float basicSalaryPayString fisrtNameint addResult
  4. Curly Braces:
    A class contains opening and closing curly braces. And so do the methods. All the statements written inside a class or a method should be enclosed within curly braces.

    class name {
    
        // write the code here
    
        public static void functionName() { 
            // write your code here 
        }
    }
  5. Readability:
    Always remember to use meaningful names for classes. This practice increases program readability.
    A program must be written in such a way that people can understand it easily. The method names must relate to the work they are performing.
    Always use indentations to write programs.
  6. Case-sensitive:
    Java is a case sensitive language. String and string will have different meanings in Java. Similarly, Integer and integer will refer to different words.You can have 2 variables int Number and int number.
    Both will point to different variables and operate separately.These are some of the rules and points that you must keep in mind in order to write a good Java Program.

Java Keywords:

Java has got a set of predefined keywords. These reserved words have certain meanings that are already defined in Java. They cannot be used by any programmer to define user-defined variables, methods or classes.

The keywords are listed below:

abstract    assert    boolean    break    byte    case    catch    char

class    continue    default    do     double    else    enum    extends

final     finally    float    for    if    implements    import

instanceof     int    interface     long     native    new   null

    package    private     protected    public    return   short  

   static    strictfp    super    switch    synchronized   this

  throw    throws    transient    try   void   volatile    while

  • const and goto are keywords, but not currently in use.
  • true, false and null are keywords reserved for literals.

Here in this section, we have written a simple Java program to print a message.

Next, we will learn about Java variables. It includes how to define variables, assign values to the variables and display the results.