Skip to main content

Java Overview : Basic Java Overview

 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. 

  1.  Abstraction 
  2.  Polymorphism 
  3.  Inheritance 
  4.  Encapsulation 

  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.


  1. BASIC OVERVIEW OF JAVA (SESSION 1)
  2. PRIMITIVE TYPES AND WRAPPER CLASS (SESSION 2)
  3. JAVA OPERATORS (SESSION 3)
  4. LOOPS IN JAVA (SESSION 4)
  5. NAMING CONVENTION WITH FACTORIAL PROGRAM (SESSION 5)


Comments

Popular posts from this blog

Java Program : Writing First Java Factorial Program with explanation

 NAMING CONVENTION IN JAVA : Java is an object oriented programming language , we can relate it to real life object like i mapped Java with human in my previous post JAVA OVERVIEW (SESSION 1)  and represent human properties like body parts as properties in Java and Human can dance , drive , walk , run these can be mapped as Behaviour in java.    Now To represent properties and behaviour in java , there are some standard naming conventions we should follow. Class name should always starts with Uppercase letter like class Student { //Code to be executed } Properties or any kind of variables should starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ; //Code to be executed } Methods name should also starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ;

OOPS Concept in Java : ENCAPSULATION

OOPS Concept in Java : ENCAPSULATION   This OOPS concept can be used to make things simpler in case of software development . Main purpose of this concept is to hide the properties of any class and give access to fetch and modified based on business criteria.  A simple example can be a POJO ( Plain Old Java Object) in which all the properties of a class can be private and through getter and setter method of properties we can fetch and update the properties of Object. So instead of having direct access to properties we have created 2 methods to make the CLASS things encapsulated in single unit while access to it is via 2 public methods.   Just consider we have requirement that once the object is created its value should not be changed then simplest way to achieve this  can be done by just removing setter method and we will keep only getter methods to access Object properties . In this case after Object creation whatever the value of Object properties has been initialised it will b

OOPS Concept in Java : POLYMORPHISM

 POLYMORPHISM IN JAVA :  Polymorphism means mutiple forms of Single reference. And to understand in simple way just take an example from my previous post of OOPS CONCEPT IN JAVA : INHERITANCE  , I request you guys to go through this link before proceding here. So in this post I have created a method called sum() in PARENT class which has been used by CHILD class without writing same sum() method in CHILD class. This was possible becuase of INHERITANCE concept in java.  But what if CHILD class is not satisfied with PARENT sum() and CHILD class wants to improve it like by adding some message before the calculation. To do this we have another OOPS CONCEPT IN JAVA i.e POLYMORPHISM and by applying this logic we can make same sum() method behvae differently based on Object. As I mentioned earlier POLYMORPHISM means different forms and same has been achieved here by calling same sum() method but the output is different based on Object on which it has been called. If Object is of PARENT