JAVA BASIC OVERVIEW :
Java is high-level, class-based, robust, platform independent and object oriented programming language.
Here each keyword has specific meaning which we need to understand.
How java internally works and execute our logic ?
We will need to write a file with extension .java, in which we will be writing our logics to be executed and this should be inside a class because java starts from main method of any class which is public inside the file named same as class name.
Example :
public class FirstJavaProgram {public static void main(String[] args) {
//our logics
}
}
This code should be saved in a file named as FirstJavaProgram.java
Whenever we are writing a java class, it's basically a blue print of what class has and it can do, it means characteristics and behaviour , just like a simple real life object like human, a human has many characteristics and behaviours , so in java we can map characteristics as class variable and behaviours as class's methods.
Example :
public class FirstJavaProgram {int firstNumber=10;
int secondNumber=20;
public int sum(int fNum, int sNujm){
return fNum+sNum;
}
public static void main(String[] args) {
//our logics
}
Here red lines are variables with their values and green lines indicates a method to add 2 number which nothing but the behaviour of class. There are mainly 4 object oriented concept as below which every java developer should know.
Will explain each OOPS concept in details in our next topic.
Once we will write our class we need to compile this with command javac fileName.java. it will generate a fileName.class file which is in machine level language , human can't understand, so this bytecode can be read and execute by JVM. Once our file is compiled and .class file is generated then this .class file can be executed on any platform which has java with command java fileName.java. That's why java is platform independent .
In above example we have just written a main method which will be searched by JVM at the time of program start. So its mandatory to have a main method with same signature as mentioned in above example. i.e public static void main(String[] args)
Here each keyword has meaning as below.
1) public : Its access specifier , we have total 4 types of access specifier public, private , protected and default, basic use of these specifier to put restriction on variable, methods and class, the most restricted access specifier is private and the most unrestricted specifier is public. if no specifier is mentioned then it will be default access specifier.
Protected access specifier. i will be explaining in next topic as there is one most asked interview question is why default is more restricted than protected.
2) static : static keyword is used to represent anything without object and its belongs to class not object and it will load before class is getting loaded into memory as there is specific memory for static variables, methods and classes.
3) void : void is return type and its means method is not going to return anything , if any type is going to return from method than that type should be mentioned instead of void like boolean, int, float or String etc.
4) main : its method name , in this it can't be changed as we must have one main method for JVM to get it identified and run our program, but in case of our custom method name can be anything.
5) String[] args : its input parameter for main method, a method can have multiple input parameter or none its all depends on our requirement , here in case of main method it will be always an array of String.
This is explanation of main method signature .In this topic we have learned what is java and how we should write classes and how internally it execute our logics.
- BASIC OVERVIEW OF JAVA (SESSION 1)
- PRIMITIVE TYPES AND WRAPPER CLASS (SESSION 2)
- JAVA OPERATORS (SESSION 3)
- LOOPS IN JAVA (SESSION 4)
- NAMING CONVENTION WITH FACTORIAL PROGRAM (SESSION 5)
Comments
Post a Comment