Java Syntax – Quick Guide

There is a syntax for every programming language, Learners can benefit from this post to better understand Java syntax.

A Java program is written and executed according to a collection of rules called syntax.

Java Syntax describes all the main rules, commands, and constructions of the language that the compiler and computer “understands ”.

It is intended for beginners and those familiar with other programming languages who wish to understand the basic syntax of the Java programming language.

Java Syntax Example

We printed “This is my first Java File” to the screen in the previous chapter by creating a Java file called Main.java:

Main.java

public class Main {
 public static void main(String[] args) {
  System.out.println("This is my first Java File");
  }
}

Execute

Example: 

public class Main { public static void main(String[] args) { System.out.println("In this session, we are discussing about Java Syntax "); } }

Example explained

Every Java line of code must be in a class.

We named our class Main in our example. Class names should always start with the Uppercase first letter.

Caution:

  • As Java is case-sensitive, “Main” and “main” mean different things.
  • The Java file name must match the class name.
  • The file should be saved using the class name and should contain the extension “.java“.
  • Java must be installed on your computer to run the example above – Else visit our chapter regarding Java installation on Windows, Mac, and Linux.

The output should be:

This is my first Java File



The main() Method

In Java Syntax, the main() method is required, and you will find it in every program:

public static void main(String[] args)

In the main() method, any code will be executed. There is no need to understand the keywords before and after the main.

While reading this tutorial, you will learn more about them.

Remember that every Java program must have a class name that matches its filename, and every Java program must have a main() method.


System.out.println()

Java’s System class contains useful members, such as out, which stands for “output”. Println() prints a value to the screen (or to a file) using the “print line” method.

A line of text can be printed to the screen using the println() method inside the main() method:

Example:

public class Main { public static void main(String[] args) { System.out.println("This is my second line to print "); } }

Example: 

public class Main{ public static void main(String[] args) {System.out.println("We are learning the syntax of the Java Language !!"); } }

Please note: A block of code begins and ends with curly braces { } and The coder must use semicolons ( ; ) at the end of every code statement.

Advice: It is not necessary to pay too much attention to System, out, and println(). The only thing you need to know is that you need both of them to print stuff.

You will learn how to comment Java code in our next article.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *