codingstreets
Search
Close this search box.
java-syntax

Get Started: Java Syntax

In This Article, You Will Learn to Work With Java Syntax.

Java Syntax – Before moving ahead, let’s know a bit about Java First Program.

Table of Contents

Java Syntax

Syntax – It is the systematic way to write the code in a right manner. 

Take a look at the previous lesson, “Java Hello World.”

We created a Java file named Main.java in the previous lesson. Then, we used the following code for printing “Hello World” on the terminal.

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

It is mandatory to be presented every line of code in Java Class. In the above example, the Class named is Main.

Note:  A class name in Java should always start with the uppercase first letter. Since Java is a case-sensitive language, here, “class_one and “Class_one” is considered different.

Make sure the Java file name and Class name are the same. Save the file using extension .java to the end of the file name. Now to run the file, Java must be installed on the computer. See the lesson on how to install Java.

Output:

				
					Hello World
				
			

The main Method

To write any program file in Java, creating a main method within the Java class is mandatory. 

You will see the main() method in every program file in Java.

				
					 public static void main(String[] args)
				
			

All code within the main() method will be executed. Just forget about keywords before and after the main keyword. As you read further in an upcoming lesson, these keywords will be easier to remember.

Note: Remember that Java programs must have a name that matches the filename. Every program must also contain the main()method.

				
					System.out.println()
				
			

To print a line of text in Java, use the println() method inside the main()

Let’s consider the above example, “Hello World.”

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

Note: The curly braces are the beginning and end of a block code.

System (built-in Java class) contains valid members such as out. This is short for “output.” The println() method (short for print line) displays a value on the terminal.

System out, println() are not necessary. You only need them to print stuff to your screen.

Also, you should note that every code statement must be terminated with a semicolon (;).

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on