Java Syntax

We understand Java Syntax by using an example. So First We created a Java file called S4gTest.java, and we used the following code to print “Study4Geeks” to the screen.

public class S4gTest{
  public static void main(String[] args) {
    System.out.println("Study4Geeks");
  }
}

output:

Study4Geeks

Please note that all codes like variables, functions, etc are placed between the curly braces {} and each code statement must end with a semicolon.

Example explained

In our example, we named the class S4gTest. A class should always start with an uppercase letter. In Java, all code is placed inside the class.

The name of the java file must have the same as the class name. Please note that if you saving the file you must have to save it with the same name of the class and will place the .java at the end of its name.

main Method

In every Java program, The main() method is required. Without the main method, you can not run any program.

public static void main(String[] args)

public: It is an access specifier and the public is a keyword that is used for publicly visible to JVM. Because JVM can identify the execution point of the program.

static: It is a keyword. Static keyword used before any methods and variables and can be directly accessed with the help of Class, which means that there is no need to create objects.

void: Every method in java has the return type. If you do not return any value then simply write void keyword at the starting of the method name.

main(): main is the method name and It’s fixed and when we start a java program, it looks for the main method.

String args[]: It is used for accepts data from the user. It also used to accepts a string array.

System.out.println()

print() is the method used to display the message in the console which is written inside the print() method.

Do you know?
Java Syntax This article is contribute by Study4Geeks If you want share your knowledge on Study4Geeks then go to login section.