AlphaCodingSkills

Java - Syntax


Advertisements

Previous Page Next Page

Although the "Hello World!" program looks simple, it has all the fundamental concepts of Java which is necessary to learn to proceed further. Lets break down "Hello World!" program to learn all these concepts.


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

Output

Hello World!.

First line is a single line comment which starts with //. Generally, comments are added in programming code with the purpose of making the code easier to understand. It is ignored by compiler while running the code. Please visit comment page for more information.

Every line of code in Java must be within a class. In this example, the name of class is MyClass. It is a public class too. Every java program file can have maximum one public class. Public class name and java program file name must be same. For example, in this example name of java program file is MyClass.java. To learn about class, please visit class page.

Please note that Java is case-sensitive. Along with this, body of the class must be within curly brackets.

The Main Method()

In Java, every public class must contain main method which is executed when public class is called. Please note that public class name and java program file name must be same.

System.out.println()

It is used to print a line on the screen. Inside println(), we can use multiple variables each separated by +.

Example:


public class MyClass {
  public static void main(String[] args) {
    System.out.println("Hello World!.");
    System.out.println("Learning Java" + " is fun.");
  }
}

Output

Hello World!.
Learning Java is fun.

Previous Page Next Page