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

Bubble sort Implementation

Bubble sort  : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). // Bubble Sort class BubbleSort { public static void sort ( int [] array) { int n = array. length ; while ( true ) { boolean swapped = false; for ( int i = 0 ; i < n - 1 ; i++) { if (array[i + 1 ] < array[i]) { swap (array , i , i + 1 ) ; swapped = true; } } if (!swapped) break; } } private static void swap ( int [] array , int i , int j) { int temp = array[i] ;...

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics ...

Overview of time and space complexity for sorting algorithm

Bubble sort : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). Selection sort : In selection sort we will divide the Array in 2 parts , sorted and unsorted and select the min element from unsorted and will copy to sorted array.      Time Complexity for selection sort also is O(n^2).      space complexity for bubble sort will be O(1). Insertion sort :  In insertion sort, Same process like selection sort , we will divide the array in sorted and unsorted array and we will select element from unsorted array and will insert it into sorted array at its proper...