Skip to main content

Java Syntax

In this section include all basic information to deal with Java program. Such as how to install Java Development Kit JDK in our machine. How to run Java programming code etc.

Installation of Java

There are many version are available in java. We can install anyone of those version and use that features in our local machine. Here given few link which are provides complete installation process of current all java version.

Version Reference link
JDK 15 JDK Installation
JDK 14 JDK Installation
JDK 13 JDK Installation
JDK 12 JDK Installation
JDK 11 JDK Installation
JDK 10 JDK 10 and JRE 10 Installation
JDK 9 JDK 9 and JRE 9 Installation
JDK 8 JDK 8 and JRE 8 Installation
JDK 7 JDK 7 and JRE 7 Installation Guide

I will recommend to choose any one of the following version (1,12,13).

Comments

Comment text is an documented text which are used to describe instruction, logic and expressed code description. Comment text are ignore by compiler when compile source code. Because that is useful for programmer. Java are support basically two type of comment. single line comment and multiline comment.

As the name of single line comment that is used to describe in one line small overview of code. That are defined by // (double forward slash). And double line are start /* ( forward slash) and end with the */ (star forward slash).

//Single line Comment

/*
  Multiline Text Comment 
  Describe over here
*/ 

Structure of java Program

Java is a pure object oriented programming Language. Java program execution are start within the main function and this function can be define any particular class. Note that single source file contains many class. But only one class are define main method.

//First Java Program
public class FirstProgram{
  
  //That is main function of program
  //Program execution are start of  within this main function 
    public static void main(String[] args){
      //display message on console screen
      System.out.println("Hello, Programmer");
    }
}
//FirstProgram.java

Save Java Program Java source code file name is same as class name, and .java extension are append at the end. When source code are contains more than one class and one class are defining main method then we are saved the source code file of the containing the main method class name. This program are save by the name of (FirstProgram.java) extension. Here FirstProgram is the name of class. Note that we can write java code in any text editor.

Run java program In above section given some useful link to how to install jdk in your system. After that installing JDK we are need to set environment variables. After set java environment variable we can run java code of any directory (folder) location in our system.

//Check java version
java -version

This command are display information about which version JDK are installed in your system. Run this command by command prompt (command line terminal). After that open a terminal on location of save java source code file. And run this command to compile the java source code file.

javac FirstProgram.java

If terminal are not view any warning or error then we can run this code by this command.

java FirstProgram

For example

java@regularCodes:/media/code/program$ javac FirstProgram.java
java@regularCodes:/media/code/program$ java FirstProgram
Hello, Programmer

Display Output

There are displaying message to console screen we can use print() and println() function. Note that we cannot use print and println function directly into our code because System class and out is an static variable of System class and print and println is a method. For detail System class are exist in java.lang package . static variable of out is an instance of java.io.PrintStream. And print and println is method of java.io.PrintStream package.

/*
*  Java example of print and println method
**/
public class Display{

  public static void main(String[] args) {
    //Example of print to display result
    System.out.print(1);
    System.out.print(" Two");
    System.out.print(" Three "+ 3);
    

    //Example of println to display result
    System.out.println("\nPrinln Example");

    System.out.println(1);
    System.out.println("Two");
    System.out.println("Three "+ 3);
  }
}

Output

1 Two Three 3
Prinln Example
1
Two
Three 3

Note that print and println function are used to display message. println are add line feed character to end of resultant. And we can include new line, by using '\n' character in string literals.

Java printf() function are used to formatting output result by using format specifier. That is similar to C programming.

Specifiers Overview
%c For a Single character
%d For Decimal integer
%f Decimal values floating-point
%x , %X For Hexadecimal representation
%o for octal representation
%s %S For display strings
%b %B For display Boolean
class DisplayResult{

    public static void main(String[] args){
        //Use Format specifiers
        System.out.printf("%d %d",12,10);
        System.out.printf("\n%s %s","Java"," Programming");
        System.out.printf("\n%f %b",78.4f,true);
    }
}

Output

12 10
Java  Programming
78.400002 true

Command Line arguments

Command line argument are provided by console when running java program. That is initial input to our program. We can use those argument values in this ways.

//Command line arguments in java
public class Test{

    public static void main(String[] args){

        if(args.length!=0){
            //Print all passing argument values
            for(int i=0; i<args.length;i++){

                System.out.println(args[i]);
            }
        }else{
            //When not pass any argument
            System.out.println("No argument is supplied");
        }
        
    }
}
Pass Command line Argument

Output

Java 
Red 
v2 
development

Normally command line argument are used to set up the initial specified configuration by when launching app.

Java memory management

Basically there are two types of memory allocation are supported in java programing. static memory allocation on stack area and dynamic memory allocation in heap area. Both stack and heap are internaly use RAM memory.

But their behaviour is different. Stack area memory are used in function calling execution and allocate memory to local variables. And heap memory are allocated by new operators. This memory are normally used to hold the properties of class instance variable. Array is one other example which are use heap memory.

// Example of Dynamic memory allocation in java
public class DynamicMemory
{
	public static void main(String[] args)
	{
		// Create a dynamic array of given element
		// ref is holds the reference of this created array
		// This array are contain 5 elements
		String[] ref = new String[]
		{
			"C" , "C++" , "Java" , "C#" , "Swift"
		};
        // Execute the loop through by size of array
		for (int index = 0; index < ref.length; index++)
		{
			// Display containing array element which are stored in heap area
			System.out.println("index : [" + index + "] value : " + ref[index]);
		}
	}
}
stack and heap Memory Area

Output

index : [0] value : C
index : [1] value : C++
index : [2] value : Java
index : [3] value : C#
index : [4] value : Swift

Java is excellent memory management, That are provide automatic memory management. Note that we can manually instruct to free the allocated memory on heap area. Because heap area are provide an instance reference which is hold by any particular variables (Object). Set null reference to this variable. If no other variable are hold this reference then java garbage collector are deallocate the memory.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment