Java: Syntax

Profile picture for user arilio666

In Java, we must follow a set of rules that are very helpful in executing the code.

Class

In java, every code should be written within the class, and the class should always start with an uppercase letter. Java is case-sensitive, meaning there is a difference between ClassJava and class java.

public class Testing
{
    public static void main(String[] args)
    {
        int x = 50, y = 50 , total;
        total = x + y;
        System.out.println(total);
    }
}

Output: 100

Every java file must be saved with a .java extension to run the code without any hassle.

Main Method

To execute your code, java needs to have a primary method (main()) to get the code up and run. Within this block of the main method, the code gets invoked and executed when we hit run.

public static void main(String[] args)

Verdict:

  • So java is a programming language with a class, and within that class, there should be the main method to run the code.
Tags