0 votes
in JAVA by
Syntax Java

1 Answer

0 votes
by
Syntax Java

Below is an example for syntax of Java

MyClass.java

public class MyClass {

  public static void main(String[] args) {

    System.out.println("Hello World");

  }

}

Java Example explaination

Each line of code that runs in Java must be inside a class. In our example, we named the class MyClass. A class should always start with an uppercase first letter.

Please Note: Java is case-sensitive: "MyClass" and "myclass" has different meaning.

The name of the java file must match the class name. When saving the file, save it using the class name and add ".java" to the end of the filename. To run the example above on your computer, make sure that Java is properly installed: Go to the Get Started Chapter for how to install Java. The output should be:

Hello World

The main Method

The main() method is required and you will see it in every Java program:

public static void main(String[] args)

Any code inside the main() method will be executed. You don't have to understand the keywords before and after main. You will get to know them bit by bit while reading this tutorial.

Please remember that every Java program has a class name which must match the filename, and that every program must contain the main() method.

System.out.println()

Inside the main() method, we can use the println() method to print a line of text to the screen:

public static void main(String[] args) {

  System.out.println("Hello World");

Related questions

0 votes
asked Nov 24, 2020 in JAVA by sharadyadav1986
+1 vote
asked May 30, 2020 in JAVA by Robindeniel
...