Syntax of a Java Program
Well, the syntax is nothing but a particular structure of statements that follows inside a program. In short, you can say that syntax is like some structure of a program. Not following the program syntax can result in compilation failures. So, we are now going to see the syntax of the Java program –
public class SyntaxExample{
public static void main(String[] args) {
//some code here
}
}
JAVA Syntax
So, here, the above class is named as SyntaxExample, and it contains a method called main. If you are not familiar with the concept of classes, objects, methods, etc, don’t worry.
Just consider that these lines that are written above are going to appear in almost every java program that we are going to write (maybe except that line which says some code here, because it is a comment, and it is ignored at the time of compilation. If you have no idea about what comments are, don’t worry again, because we are going to study the concept of comments)
So, with this syntax in our hands now, we are all set to write our very first java program, which will just print something as our output. So, there are multiple methods to implement this too, but let’s use one of these methods here, and we will explore the other methods afterward.
So, the method for printing that we use here is the System.out.println() method. This method is going to print something (which we are going to implement now)
Note that while you execute the program, the name of the below program should be saved as SyntaxExample.java or the program won’t execute.
The thing is that the name of the program has to be the same as the class in which you are writing the main method. So, in the below example, we have our main method inside the class SyntaxExample, so the name of the program has to be SyntaxExample.java
Also, keep in account that the “s” in the SyntaxExample is capital. We have to keep it as it is in the name of the program. So, it should be Syntaxexample.java. Well, it is not compulsory to keep the first letter or your class as capital, but it is a practice.
This .java is the extension of a java file. Every java file is going to have the extension as .java
You can consider that the .java is the last name of a java file. It is used to identify the java file and help to open it into the appropriate application. Different types of files have different extensions, like .pdf, .c, .py, .txt, .cpp, .pptx, .docx, and a lot more, you can explore.
So, here is our very first Java program –
public class Syntaxexample{
public static void main(String[] args) {
System.out.println(“Hello… from GyaniPandit!”);
}
}
To execute the above program, with the help of an IDE, we simply have to click on the run option(or whatever we are required to do in order to execute the above program). You can also make use of the terminal to execute the program.
This takes us to learn how to execute a java program when we are not using an IDE.
Well, here we are going to use the terminal, and enter some commands there, using which, we can execute the java program.