Learning Today,
Leading Tomorrow

What is Java

  • Java is one of the programming language / technology used by the industry programmers for development of distributed applications.
  • Java is a programming language first released by Sun Microsystems by a person called James Gosling and others.

Class

  • Process of binding data members and associated methods in a single unit is known as class.
  • Class is collection of data members and methods.
  • When we create object for class, memory space will be allocated for the data members and methods.
  • Class always have logical existance and object will have physical existance.

e.g class Upsada{ ----
----
}

Object

  • Instance of class is known as object which allocating sufficient memory space for data member and method.
  • There are multiple ways to create object for java but most used way is using new keyword.

e.g objectName = new Upsada upsadaObject= new Upsada();

Constructor

  • Special kind of method that determines how an object is initialized when created.

Access Modifier

  • public: Accessed any where.
  • private : Accessed within class only.
  • protected : Accessed by class in same package and sub-class in other package.
  • default : Accessed only in same same package.

Garbage Collection

  • When object is no longer referred to any variable, java automatically reclaims memory used by that object.
  • System.gc() method may be used to call it explicitly.

Immutable Object

  • When object is no longer referred to any variable, java automatically reclaims memory used by that object.
  • System.gc() method may be used to call it explicitly.

Object class

  • Present at top in hierarchy.
  • Each and every class internally extends Object class.
  • The Object class defines basic state and behavior of that object must have.

Object class Methods

  • clone() - method to create a copy from an existing object.
  • getClass() - method to get information about the class, such as its name.
  • equals() - method compares two objects for equality and returns true if they are equal.
  • hashCode() - method to get hashcode of object which is an integer value generated by a hashing algorithm.
  • toString() - method to display a text representation of an object.
  • finalize() - method invoked on an object just before an object is destroyed and can be just prior to garbage collector. The finalize() method may be called automatically by the system.

Abstract class

  • If any concrete class of java contains purely null body methods. Then such class of java classes is recommended to make as abstract class, because calling null body methods with respect to concrete base class object is of new use.
  • Null body methods always exist in abstract classes and they are always supposed to be overridden in derived class.
  • An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class.
  • Null body methods always exist in abstract classes and they are always supposed to be overridden in derived class.

Abstarct class :
public abstract class InputStream extends Object implements Closeable
public abstract class OutputStream extends Object implements Closeable, Flushable

Interface

  • An interface is a collection of public static final XXX data members and public abstract XXX() methods.
  • Interface always participate in multiple inheritance.
  • Interfaces are purely containing abstract methods and highly recommended to override in subclass.
  • Interface definition should not be final because all the interface are used for inheritance.

Interface definition should not contain constructor because
1.Data member of an interface are already initialize during their declation in the interface
2.Constructor are not permitted to write in the context of interface.

Exception

  • Runtime error of java application is known as exception. Whenever an exception occurred in a java program, program execution is abnormally terminated, control comes out of the program and by default JVM generate system error messages.
  • The process of converting system error message in to user defined error message is known as exception handling.

Exception are classified in to :
1.Asynchronous-These exception always deals with H/W problem or external problem.
2.Synchronous - These are always deal with programmatic run time exception.
a.Checked Exception
b.Uncheed Exception

Checked Exception

  • If any kind of checking process becomes fail while searching in secondary memory then throws exception are known as checked Exception
  • These are the subclass of java.lang.Exception class

java.lang.Exception :
--->FileNotFoundException
--->IOException
--->ClassNotFoundException

Unchecked Exception

  • Unchecked are the exceptions that are not checked at compiled time. Which occurs due to defect in logic.
  • These are the sub class of java.lang.RuntimeException.
  • java.lang.RuntimeException is super class for all Synchronous unchecked exception.
  • java.lang. Throwable is one of the predefined super class for all error and exception in java.

java.lang.RuntimeException :
--->ArithmeticException - When we try to divide a number with by zero
--->NumberFormatException - When we try to convert invalid string to numeric value
--->ArrayIndexOutOfBoundsException - When we try to access item from array which is not present.

Exception Flow

  • When Exception occurs in a program, JVM internally contact to JRE for getting exception sub class.
  • JRE internally contact with with java.lang.Throwable class for getting what type of exception occurs in java program.
  • java.lang.Throwable decide what type of exception occurred in java program and same message is passed to JRE.
  • Further JRE internally contact with to java exception API for obtaining exception sub-class.
  • Java exception API gives an appropriate exception sub-class to JVM
  • JVM creates an object for exception sub-class and return exception.

static keyword

static variable :

  • static variables belongs to class and not to object.
  • static variables initialized only ones. These variables initialized first before initialization of any other instance variable.
  • A single copy to be shared by all instances of class.
  • Static variables can be accessed directly by class name and does not any object.

static method :

  • static Method belongs to class and not to the object.
  • Static Method can access only static data. It cannot not access non-static data.
  • A static method can call other static method and cannot call non static method from it.
  • A static Method can accessed directly by class name does not require any object.

static block :

  • Static Block is a block of statement inside a java class that will be execuated when class is first time loaded in JVM.