Basic Java Syntax

//Java program to add two numbers

public class apnaStudy{

    public static void main(String args[ ]){
    System.out.println(“Hello World!”)
}
}

1.public class ApnaStudy{

public : public makes the class accessible from other parts of your code. Or It allows other programs to use the apnaStudy class if needed anywhere in part of code.


class : This is a template or blueprint or prototype from which we can create objects. class doesn’t occupy any memory.


ApnaStudy: This is class name.By convention,class names start with an upperCase.

2.public static void main(String args[]){

public: public makes the class accessible from other parts of your code. This makes the “main” method accessible from anywhere in your code.


static: This allows you to call the “main” method without creating an object of the ApnaStudy. Or if you want to access any function or method without creating its object class you can do it by using a static function.


void: The void keyword in java is a reserved type used mainly to specify that a method does not return any value or data type after its execution.


String args[]: This is an argument to the main method.It is an array of strings.It is used to pass command line arguments when running the program.

3.System.out.println(“Hello world!”)

System.out: This is builtin class in java.It provides access to various System functionalities like Input/Output operations.


Println: This is the method that prints the provided text to the console,followed by a newline character.


Print: This is the method that prints the provided text to the console,without a newline character.


Hello world!: This is the string literal that you want to print to the console.

Some important notes:

Note: Source File Name: If your source file contains only one public class then the source file should exactly match the public class name with the extension of .java.

Ex: apnaStudy.java, My.java,Car.java etc


Note: If your source file contains multiple non-public classes or no public classes are available in your source code at all,then you can give any valid filename with the .java extension.


Note: :Case sensitive: Java is a case sensitive language. So ab,Ab,aB,AB all are considered different in java.


Note: Java is statically type language that means you need to declare a variable with its proper data type before using it.

Comments in java:

You can imagine comments in java as sticky notes for your code.Comments help you to understand what your code is doing. Comments are ignored by the java compiler during the compilation process.


Single line comment:     // This variable stores the user’s :roll number


Note: If your source file contains multiple non-public classes or no public classes are available in your source code at all,then you can give any valid filename with the .java extension.


Multi-line :
/*This method calculates the area of the triangle.
*It takes three parameters Length, Breadth and Height
*/


Documentation comment: /** Documents **/

Previous Next