Showing posts with label Best core java training in Bangalore Best core java training in Bangalore best java training institute bangalore Marathahalli. Show all posts
Showing posts with label Best core java training in Bangalore Best core java training in Bangalore best java training institute bangalore Marathahalli. Show all posts

Friday, 12 January 2018

Understanding exception hierarchy in Java And Thread Life Cycle, Thread States in Java



Understanding exception hierarchy in Java And Thread Life Cycle, Thread States in Java


It starts with showing the Java class structure for handling errors and exceptions. We will then look at three major types of exceptions in Java which are direct sub-classes of java.lang.Exception, java.lang.RuntimeException and java.lang.Error respectively, along with examples for each type.

Important classes of Java Exception Hierarchy

Above class diagram depicts the 4 fundamental classes which are at the heart of exception handling in Java –
  • java.lang.Throwable is at the root of Java’s exception hierarchy. All types of exception are descendants of Throwable.
  • java.lang.Exception is direct sub-class of Throwable. Exception is the root class for all checked exceptions in Java.
  • java.lang.RuntimeException is direct sub-class of Exception. RuntimeException is the root class for all unchecked exceptions in Java.
  • java.lang.Error is direct sub-class of Throwable and represents unrecoverable JVM errors.

Three main types of exceptions in Java

From a programming perspective you will rarely work with Throwable class. Rather, it is considered a good programming practice to avoid using Throwable directly in your code. The remaining three classes – Exception, RuntimeException and Error represent the three important types of exceptions in Java.
Let us take a look at the 3 main types of Exceptions in Java-
  • Exceptions – Also known as checked exceptions, these are the sub-classes of java.lang.Exception class. Whenever a piece of code is likely to throw a checked exception, then either it must be caught using a try-catch block or the signature of the method containing such code should specify this exception in the throws clause.
    ExamplesFileNotFoundException, IOException.
  • Runtime Exceptions – Also known as unchecked exceptions, these are the sub-classes of java.lang.RuntimeException. These exceptions are specified for scenarios which neither need to be specifically caught using try-catch block nor do the methods which can throw them need to include these exceptions in the throws clause.
    ExamplesArrayIndexOutOfBoundsException, NullPointerException.
  • Errors – Errors are sub-classes of java.lang.Error class. These exceptions are thrown by JVM when it encounters severe issues such as if it runs out of memory and similar serious error conditions. These errors should not be caught and should be dealt with on priority as they may lead to significant glitches in deployed code.
    ExamplesOutOfMemoryError, StackOverflowError.

Summary

In this article we first looked at the exception hierarchy in Java described via its class diagram.This was followed by brief explanation of the important classes in the diagram. Finally, we looked the three main types of exceptions in Java – Exceptions, Runtime Exceptions and Errors and understood their individual purpose.
Java Thread Life Cycle
              Let us start by getting a high level understanding of the 6 thread states in Java with the diagram shown next –
  • New – A newly created thread object instance on which the start() method has not yet been invoked is in the new state. To learn how to instantiate threads in the proper way check out this
  • Runnable – A thread in new state enters the runnable state when the Thread.start() method is invoked on it. There are 2 important points to note regarding the runnable state –
    1. Although the thread enters the runnable state immediately on invoking the start() method, but it is not necessary that the thread immediately starts executing. A thread runs when the logic it holds in its run() method can be executed by the processor. In case the thread logic needs any resource which is not available then the thread waits for the resource to become available.
    2. Secondly, a thread in runnable state may run for some time and then get blocked for a monitor lock, or enter the waiting/timed_waiting states as it waits for the opportunity/time to enter runnable state again.
  • Blocked – A running thread may enter the blocked state as it waits for a monitor lock to be freed. It may also be blocked as it waits to reenter a monitor lock after being asked to wait using the Thread.wait() method.
  • Waiting – A thread enters the waiting state when it is made to wait for a go-ahead signal to proceed. The go-ahead in this case is given by another thread and can be given in the following 3 scenarios –
    1. Thread waiting due to Thread.wait() method being called on it: The other thread can use Thread.notify() or Thread.notifyAll() to give the go-ahead to the waiting thread.
    2. Thread waiting as it itself has asked for joining another thread using Thread.join(): The waiting thread gets a go-ahead when the thread its waiting for ends.
    3. Thread waiting due to LockSupport.park()method being invoked on it: The waiting thread resumes when LockSupport.unPark() is called with the parked thread object as the parameter.
  • Timed_Waiting – A thread which is waiting as it has been specifically ‘instructed’ to wait for a specified waiting time is in a timed_waiting state. A thread can be made to wait for a pre-determined amount of time in the following ways –
    1. Thread made to wait using Thread.sleep() method.
    2. Threads being asked to wait for a permit for a specified amount of time using LockSuport.parkNanos() and LockSupport.parkUntil() methods.
    3. Threads being made to wait for a fixed amount of time using Thread.wait(long millis) or Thread.join(long millis, int nanos).
  • Terminated – A thread enters its ‘final resting’ state or terminated state when it has finished executing the logic specified in its run() method.



Thursday, 21 September 2017

Java OOPs Concepts



Java is a programming information which is more well known as a result of appropriated nature over web. We can compose answer for any continuous circumstance.
                 Web advancement is simple in Java. One can basically compose Java projects to robotize continuous business. It is more secure as well.  Today we live in DATA world. To make a little web application in phone to an expansive undertaking application that keep running on web, we utilize Java/J2EE programming dialect.
                               Java writing computer programs is straightforward, protest situated, strong , secure, design nonpartisan and compact, superior, deciphered, strung and dynamic.  Protest Oriented Programming is a worldview that gives numerous ideas, for example, legacy, information authoritative, polymorphism and so forth.  Simula is considered as the primary protest arranged programming dialect. best java training institute bangalore Marathahalli.

                 The programming worldview where everything is spoken to as a question, is known as really protest situated programming dialect.  Smalltalk is considered as the principal genuinely question arranged programming dialect.  Uh oh (Object Oriented Programming System)  Protest implies a genuine word element, for example, pen, seat, table and so on. Protest Oriented Programming is a philosophy or worldview to plan a program utilizing classes and questions. It rearranges the product advancement and upkeep by giving a few ideas:
      Object
      Class
      Inheritance
      Polymorphism
      Abstraction
      Encapsulation
                           Question  Any substance that has state and conduct is known as a protest. For instance: seat, pen, table, console, bicycle and so forth. It can be physical and coherent.  Class  Gathering of articles is called class. It is a legitimate substance.  Legacy  When one protest procures every one of the properties and practices of parent question i.e. known as legacy. It gives code reusability. It is utilized to accomplish runtime polymorphism.  Polymorphism  When one assignment is performed by various ways i.e. known as polymorphism. For instance: to persuade the client in an unexpected way, to draw something e.g. shape or rectangle and so forth.
                          In java, we utilize technique over-burdening and strategy abrogating to accomplish polymorphism.  Another illustration can be to talk something e.g. feline talks meaw, pooch barks woof and so forth.  Deliberation  Stowing away interior points of interest and demonstrating usefulness is known as deliberation. For instance: telephone call, we don't have the foggiest idea about the inward handling.
                              In java, we utilize unique class and interface to accomplish deliberation.  Epitome  Official (or wrapping) code and information together into a solitary unit is known as embodiment. For instance: container, it is wrapped with various solutions.  A java class is the case of epitome. Java bean is the completely embodied class since every one of the information individuals are private here.  Preferred standpoint of OOPs over Procedure-arranged programming dialect  1)OOPs makes improvement and support less demanding where as in Procedure-situated programming dialect it is difficult to oversee if code develops as venture measure develops.  Java Training in Bangalore

  2)OOPs gives information stowing away though in Procedure-arranged programming dialect a worldwide information can be gotten to from anyplace.
  3)OOPs gives capacity to recreate true occasion significantly more adequately. We can give the arrangement of genuine word issue in the event that we are utilizing the Object-Oriented Programming dialect.  What is distinction between question situated programming dialect and protest based programming dialect? 
                   Protest based programming dialect takes after every one of the highlights of OOPs aside from Inheritance. JavaScript and VBScript are cases of protest based programming dialects.  Java Naming traditions  Java naming tradition is a run to take after as you choose what to name your identifiers, for example, class, bundle, variable, consistent, technique and so forth.  In any case, it is not compelled to take after. Along these lines, it is known as tradition not run the show.  Every one of the classes, interfaces, bundles, techniques and fields of java programming dialect are offered by java naming tradition.  Favorable position of naming traditions in java  By utilizing standard Java naming traditions, you make your code less demanding to peruse for yourself and for different software engineers.
                          Coherence of Java program is vital. It shows that less time is spent to make sense of what the code does.  Name       Convention  class name   should begin with capitalized letter and be a thing e.g. String, Color, Button, System, Thread and so forth. interface name should begin with capitalized letter and be a descriptive word e.g. Runnable, Remote, ActionListener and so on.  technique name   should begin with lowercase letter and be a verb e.g. actionPerformed(), principle(), print(), println() and so on.
 variable name    should begin with lowercase letter e.g. firstName, orderNumber and so forth.  bundle name    should be in lowercase letter e.g. java, lang, sql, util and so forth.  constants name   should be in capitalized letter. e.g. RED, YELLOW, MAX_PRIORITY and so forth.  Question and Class in Java  In this page, we will find out about java questions and classes. In protest situated programming procedure, we outline a program utilizing items and classes.  Question is the physical and in addition consistent substance though class is the intelligent element as it were.  Question in Java  A substance that has state and conduct is known as a protest e.g. seat, bicycle, marker, pen, table, auto and so on. It can be physical or sensible (unmistakable and elusive).
                         The case of elusive protest is saving money framework.  A protest has three qualities:
      state: speaks to information (esteem) of a protest.
      behavior: speaks to the conduct (usefulness) of a protest, for example, store, pull back and so forth.
      identity: Object personality is ordinarily executed through a remarkable ID.
                                The estimation of the ID is not obvious to the outer client. Be that as it may, it is utilized inside by the JVM to recognize each question interestingly.  For Example: Pen is a protest. Its name is Reynolds, shading is white and so forth known as its state. It is utilized to compose, so composing is its conduct.  Question is an occasion of a class. Class is a format or diagram from which objects are made. So protest is the instance(result) of a class.