Java Syntax

We already wrote and executed our first program. Now lets understand everything about that java program. In future, we have to follow same syntax for almost all programs.

Previous Program

Lets take a look program we executed before:

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

Components of Java program

If you notice program above, you see it have 3 components. They are:

  • public class (Class)
  • public static void main() (Method)
  • System.out.println() (Statements)

Class

Every line of code that runs in Java must be inside a class. In our example, we named the class HelloWorld. 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. public is the keyword used to give permission to access or modify. It is also called Access Modifier. Though we will learn about class later more clearly.

Method

Any code inside the main() method will be executed. You don't have to understand the keywords before and after main. You may understand Method as function for now. You will get to know them bit by bit while reading this tutorial. But you have to remember that every java program must have at least one method in it.

Statements

Whatever we want to do in program goes here. Our actual code lies inside method. Here System.out.prinltn() is used for outputs. System.out means output stream. And println means we want to print that output to the screen. You can write print or println, which means in same line and in different line respectively. Anything we want to print goes inside parenthesis ( ) after println.


First Program in Java Previous Next Things to Remeber for Java