Java Syntax Explanation in detail

Java is an Object-oriented language. Now, we are discussing the syntax in detail way. Let’s compile a basic program of java language. You should work a little bit on this below program.

Java Syntax

/*
This is a simple Java program.
call this file "Syntax.java".
*/
class Syntax {
//your program begins with a call to main().
public static void main(string args[]) {
System.out.println("This is a simple Java Program.");
}
}

Output:

This is a simple Java Program.

Explanation of java syntax

The first thing that you must learn about Java is that the name you give to a source file is very important. For the above example, the name of the source should be Syntax.java. In java, a source file is called a compilation unit.

Above program the name of the class defined by the program os also Syntax. By convention, the name of the main class should match the name of the file that holds the program. The reason for this is that java is case-sensitive. This makes it easier and organizes your programs.

compilation of the program

After completion of the program, you can execute your program in the command prompt.

C: \>javac Syntax.java

The java compiler creates a file called Syntax.classs that contains the bytecode version of the program. The java bytecode is the intermediate representation of your program. It contains instructions the Java Virtual Machine will execute. Thus, the output of javac is not code that can be directly executed.

c:\>java Syntax

To run the program in the command prompt you should give the above command. When the program is run below output is given.

This is a simple Java program

When the program is compiled, each individual class is put into its own output file named after the class and using the .class extension. That’s why it good to use the same name of the class to the name of the filename.

Example

 public class AddTwoNumbers
{
public static void main(String[] args)
{
int num1 = 5, num2 = 15, sum;
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}
output:
Sum of these numbers: 20

Leave a comment