Java Installation :
Java installation procedure differs from one Operating system to other.
We can download Java Developer Kid in the following link - Click Here
We can download Java Developer Kid in the following link - Click Here
JDK, JRE can be installed on the many platforms:
1. Microsoft Windows
2. Linux
3. MacOS
And for Installation process please refer the below links
Windows :
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 extecute the program as java programName
Java HelloWorld
5. The Output appears after running the program will be
Hello World
Explanation :
Before explaning 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.
So 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.
Ans : Since all other methods inside the main method should be accessible directly,
And these arguments should be of String format.
Note : Hi i am new for blogging, if you think my blog is useful, kindly SUBSCRIBE in the website and also share this to your friends. And if anything is wrong in the tutorial please correct me and kindly forgive me. Thanks everyone.
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.
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.
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.
byte | case | catch | char |
class | const | continue | default |
do | double | else | enum |
extends | final | finally | float |
for | goto | if | implements |
import | instanceof | int | interface |
long | native | new | package |
private | protected | public | return |
short | static | strictfp | super |
switch | synchronized | this | throw |
throws | transient | try | void |
volatile | while | abstract | assert |
boolean | break |
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.
2. class declared as public
3. main method (String[] args) type
4. main method (String []args)
5. main method (String args[])
6. main method (String...args)
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){
}
}
in Next tutorial we will see about class, objects, variables, data-types and package.
Note : Hi i am new for blogging, if you think my blog is useful, kindly SUBSCRIBE in the website and also share this to your friends. And if anything is wrong in the tutorial please correct me and kindly forgive me. Thanks everyone.
No comments:
Post a Comment