JAVA – Installation , Writing HELLO-WORLD Program.


Java Installation :

Java installation procedure differs from one Operating system to other.

We can download Java Developer Kid (JDK) in the following link – Click Here

JDK, JRE can be installed on the many platforms:

1. Windows Operating system

2. Linux Operating system

3. Mac Operating system

And for Installation process please refer the below links

Windows :

To view the steps please Click Here

Linux :

To view the steps please Click Here

MacOs :

To view the steps please Click Here

Writing our First Java Program


Once after installing java we can write a new HelloWorld program

1. Open a file in notepad / vim editor

This is the sample helloworld Program.

class HelloWorld{ 
public static void main(String args[]) 
{ 
System.out.println("Hello World"); 
} 
}

2. Then Save the file with .java extension.

3. Compile the program using javac programName.java

javac HelloWorld.java

This will create a .class file after JRE/JVM process done.

4. Finally execute the program as java programName

Java HelloWorld

5. The Output appears after running the program will be

Hello World

Explanation :

Before explaining the HelloWorld Program, we have to know some of the basic things combined to form this HelloWorld Program.

Java program is combination of reserved in-build java keywords and variables defined inside the programs.

What is a Java Keyword ?

It is a word which have predefined meaning in Java Programming language.

What is the limitations  for Java Keyword ?

It cannot be used as identifiers for naming Variables, Classes, Methods or other entities.

List of Java Keywords used in HelloWorld Program is

class – is a java keyword used to declare the class, Always the class keyword followed by ClassName should be mentioned.

public – is a java keyword which is an access specifier which defines the visibility of the program, since its public it is visible to all.

static – is a java keyword defines when a Java program is called it creates the space in Memory automatically , so we can call this method of the class directly without creating any object.

void – is a java keyword which defines the return type, and void keyword which doesn’t return any value.

main() – is a java keyword defines a method / function name denotes the main method or main function name.

String args[] – defines for the main method the arguments of variables can be passed in args[] of array format and which should be of String format.

System – is a java keyword defines the predefined build-in class in Java libraries.

out – is a java keyword defines the INPUT / OUTPUT operations in predefined build-in class in Java libraries.

println() – is java keyword defines the method of PrintStream which is predefined build-in method in Java.

so as said earlier, Java program is combination of reserved in-build java keywords and variables defined inside the programs.

How the process Works ?

1. For example if we write a program which contains only class declaration.

And if we compile the program it is compiled successfully,

The compiler don’t check for ClassName / MethodName , it simply checks the syntax.

Why java compiler is not checking for ClassNames and MethodNames ?

Java Compiler don’t have definitions to check ClassName or MethodNames.

When we run the program, it throws the error “NoSuchMethodError:main”

Why is the JVM throws such error ?

Because JVM checks for main method inside the Java program and throws error.

1. JVM is also a program which have some codes checks for main method.And if it doesn’t have main method it throws error.

2. So if we write the main method inside the code is executed successfully.

class HelloWorld{
public static void main(String args[])
{
    System.out.println("Hello World");
}
}

So main method is declared as “public static void main (String args[] )”

Why main method should be declared as public ?

Ans : Java program written will be stored somewhere in the memory location.

And that memory location should be accessible from anywhere so its defined as “public”.

Why is main method should be declared as static ?

Ans : Since all other methods inside the main method should be accessible directly,

Its stored in memory location and it should not be accessed by creating objects inside the main method.

Why is main method should be declared as void ?

Ans : Since its is the main method and it should not return any values void is used.

Why is main method have command-line arguments ?

Ans : Any input to the main method can be processed by passing command-line arguments.

And these arguments should be of String format.

Other notations for String arguments is also supported by Java language.

1. String[] args

2. String  []args

3. String args[]

4. String…args    “Only three dots allowed”


Is there any rules says arguments should be mentioned as args ?

Ans : No , this can be anything with any name as user defined

public static void main (String[] some-name)

public static void main (String  []some-name)

public static void main (String  some-name[])

public static void main (String…some-name)

JVM doesn’t check these argument names as its not in its protocol.

List of Java Keywords :


We are not supposed to use these java reserved keywords for user defined variables or methods or anything.

thiscasecatchchar
classconstcontinuedefault
dodoubleelseenum
extendsfinalfinallyfloat
forgotoifimplements
importinstanceofintinterface
longnativenewpackage
privateprotectedpublicreturn
shortstaticstrictfpsuper
switchsynchronizedthisthrow
throwstransienttryvoid
volatilewhileabstractassert
booleanbreak

Implementation Techniques :

Java class can be declared by many types

1. class declared with access-modifier, by default if the access-modifier is not specified java will consider as public.

class className{
      public static void main(String args[]){
     }
}

2. class declared as public 

public class className{
      public static void main(String args[]){
     }

}

3. main method (String[] args) type 

public class className{
      public static void main(String[] args){
     }

}

4. main method (String []args)

public class className{
      public static void main(String []args){
     }

}

5. main method  (String args[])

public class className{
      public static void main(String args[]){
     }

}

6. main method (String…args)

public class className{
      public static void main(String...args){
     }

}