What is the basic syntax of Java?

Basic Syntax in java

Basic Syntax of Java

The basic syntax of Java is composed of at least one class where at most one class can be public. Java programs consist of objects which communicate with each other through message passing via methods invocations. To get started with Java, a basic code that does nothing but prints a statement to the console is provided down below with an explanation about every single line that makes it complete.

Example showing basic syntax of Java:

//Basic Java Program that prints a statement to the console
class Basic{
    public static void main(String[] args){
        System.out.print("StudyExperts: A platform where learning is easy and doubts remain no more.");
    }
}

Output:

StudyExperts: A platform where learning is easy and doubts remain no more.

Comments:

    • First line of the code starts with 2 forward slashes and a string of words. Anything starting with 2 forward slashes is a single-line comment.
    • A comment in Java is something that is overlooked by the compiler while compiling and is there only for the programmer to know what a particular piece of code does or for someone who hasn’t seen the code before and has to go through it to improve it or do some changes in it.
    • Without comments, understanding a program would be quite daunting, even for a skilled programmer.

class keyword:

    • Second line of the code starts with a keyword “class” followed by a word “Basic”. Basic is the name of the class.
    • This is how a class is Java is declared along with the curly braces which holds data members and member functions inside it.

public static void main(String[] args) Syntax in Java:

    • Third line of the code is made up of public static void main(String[] args). The main method in Java is declared public so that JVM (Java Virtual Machine) can access it from outside the class since it is not present inside the class.
    • public is an access specifier which specifies who can and from where a class member can be accessed.
    • static keyword is used with the main() method so that the JVM doesn’t have to instantiate the class containing the main() method to invoke it. When a method is definition is preceded by the keyword static, it becomes class related which means instantiating the class it is in is not needed to invoke it.
    • void here is the return type of the main() method. Return type of main is void because main does not return anything. Even if it did, the value will be returned to JVM which doesn’t have anything to do with it. So, basically void specifies that main() method returns nothing.
    • main() is the method name which is the first method to be called in any Java program.
    • String[] args contains command line arguments passed to a Java program and are of type java.lang.String.

 

Note:

    • If a java program has more than one class and none has been specified as public, then the one containing main() method is considered public and name of the file should be same as the name of that class or the public class if any.
    • If a program has a public class, then the main method should be inside it and it should be the only public class inside that file as the name of the file is same as the name of the public class.
    • args used inside the main() method is not a permanent or mandatory identifier that your Java program should have. Any word of you choice used instead of args inside the main() method would make the program run just perfectly fine.

System.out.print():

    • Fourth line of the code consists of System.out.print() with a long string of characters inside it.
    • System is a final class in Java defined in java.lang package.
    • out is an instance of PrintStream type which is a public and static member field of the class System.
    • print() is a method that all instances of PrintStream type have, so it can be invoked using out as well. It takes parameters of different types and prints them on the console.
    • Anything written within double quotes (“”) is treated as a string.
    • There is a slight variation to print in Java which is the println() method which does everything same as the print() does but adds a newline character at the end of the parameter thus moving the cursor to the next line after writing the string to the console.

 

Case Sensitivity

Java is a case-sensitive language which means main and Main are different. Java follows a convention when it comes to naming a class and its methods which is as follows:

    • Name of a Java class begins with a capital letter and if it is made of more than one word, then all the word’s first letters should be capital.
    • Name of a method begins with a small-case letter and if it is made up of more than one word, then all the other words begin with a capital letter.

 

Semi Colons

Basic Syntax of Java includes programs having a class and print statements that ends with a semicolon. The semicolon is a statement terminator indicating that a statement has ended and that the next words following it represent a new statement.

 

Also read, what are variables in Java?

Share this post

Leave a Reply

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