Java Syntax


In the previous chapter, we created a Java file called MyClass.java, and we used the following code to print "Hello World" to the screen:


MyClass.java


public class MyClass {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}


Example explained


Every 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.

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



Jay Kakadiya

Jay Kakadiya Creator

I am a computer field, & i am Web developer.

Suggested Creators

Jay Kakadiya