Search This Blog

Sunday, 7 December 2014

How to Add and Retrive Data from the Map in Java


import java.util.Iterator;
import java.util.Map;
import java.util.Set;

class SimpleMapProgram{
    public static void main(String[] args){
 //creating the map object of key as integer and value as String type
 
 Map<Integer,String> map=new HashMap<Integer,String>();
//adding the element into map there is put() method in java for adding the element into the Map
map.put(1,"Core Java");//1 st element added into the HashMap
map.put(2,"Adv Java");//2 st element added into the HashMap
map.put(3,"Spring");//3 st element added into the HashMap
map.put(4,""Hibernate);//4 st element added into the HashMap
map.put(5,"Struts");//5 st element added into the HashMap

//retriving the element from the Hashmap
//first the give set type object for getting the all keys of the HashMap       
       Set keyset=map.keySet();
       //here getting the Iterator object for retriving the hashmap object
 Iterator<Integer> it=keyset.iterator();

      System.out.println("HashMap Element Are As Follow");
        while(it.hasNext()){
        //getting the key
            int key=it.next();
         //getting the value of the element by passing the key as argument to the get method
            String value=map.get(key);
         //printing the key and value of the object
            System.out.println(key+"-----------"+value);
        } //while loop closed
      }//main method close
}//class close




Output:


HashMap Element Are As Follow
                       1---------Core Java
                       2---------Adv Java
                       3---------Spring
                       4---------Hibernate
                       5---------Struts

Thread life cycle in java


Thread life cycle:

The Thread life cycle means the thread is going through which state that is the Thread life.
The Thread is mainly going into the five state.
The state are as below.
  • New/born

  • Runnable

  • Running

  • Blocked/Waiting state

  • Dead state

    1)New: 

    This is the start point of the Thread.When we call the Thread class constructor that time this state is performed.

    2) Runnable:

    This state means the Thread is ready for execution when we call start method then Thread is going to Runnable state.

    3) Running:

    This is the actual state where the Thread is execution.In this state the code executed  concurrently mode.For coming in this state Thread perform number of number here check that the Thread priority and all other conditions.

    4)Blocked:

    If any reason Thread is going to blocked state.If we perform the IO operation if the Thread is waiting for the file then Thread is going to waiting state.and if we call the join,sleep method then the Thread is going to Blocked state.From the blocked state Thread always go to Runnable State not in Running state.

    4)Dead/Complete:

    After complete the Thread execution the Thread is going to Dead state.it is the last state of the Thread life cycle.If  The once Thread goes into Dead state the Then never come into Running state.

        

Monday, 1 December 2014

What is the Collection in Java


Collection:

Collection is the class in the java which can stored the Homogenous,Hetrogenous object.That object we can send throgh out the network.

Sunday, 30 November 2014

Difference between Vector and ArrayList


Vector:

  • Vector is legacy class means that is introduced in Java 1.0.
  • Vector class are Thread Safe.
  • All method in the Vector class are Syncronized.
  • Relatively Speed is less than the ArrayList.


ArrayList:

  • ArrayList is new  class means that is introduced in Java 2.0.
  • ArrayList class are not Thread Safe.
  • No any method in the Vector class is Syncronized.
  • Relatively Speed is faster than the Vector.

Difference between Enumeration and Iterator

Enumeration:

  • Enumeration is the legacy class which is used on legacy Interface.
  • Enumeration is Fail Safe means we can modify the object in the retrieving time.
  • Enumeration is Introduced in Java 1.0
  • Enumeration has no any method for removing the element.
  • Enumeration method name are Big.


Iterator:

  • Iterator is not legacy class it is used for any type of collection.
  • Iterator is Fail Fast  means we can not modify the object at the retrieving time.
  • Iterator is Introduced in  Java 1.0.
  • Iterator has remove() method for removing the element.
  • Iterator method name are smaller than the Enumeration.

Tuesday, 25 November 2014

Difference between Exception and Error



Exception :


Exception is occured due to logical mistake of our program that's why Exception are occured at runtime.
Programmer can handale the Exception.

Example :FileNotFoundException
ArithmaticException



Error :


Error is occured due to System failure problem.not programmer.
Error can not handale by the programmer.

Example :StackoverflowErrror
lackOfResourceError



Difference between Checked Exception and Unchecked Exception



Checked Exception :


The Exception which are checked by compiler whether programmer handaling or not such type of Exception are callad checked Exception.

Example :FileNotFoundException



UnChecked Exception :


The Exception which are not checked by compiler whether programmer handaling or not such type of Exception are callad Unchecked Exception.

Example :ArthmaticException



Note :


Exception always occur at runtime means the checked or unchecked exception also occured at the Runtime.

Difference between StringBuffer and StringBuilder in Java



StringBuffer :


StringBuffer Introduced In Java 1.0 version.
StringBuffer Is Thread Safe Class.
All method in the StringBuffer are Syncronized.
At speed StringBuffer is slow than the StringBuilder because of syncronization.

StringBuilder :


StringBulider Introduced In Java 5.0 version.
StringBuffer Is Non-Thread Safe Class.
All method are Non- Syncronized.
At speed StingBuilder is faster than the StringBuffer .

Java Interivew Question

Exaplain about final,finally,finaliz in Java


final :

final is modifier in java which is used for variable,method and class.
1 :if declare variable as final that variable becomes a constant means we can not change the value of that variable.
2 :if declare method as final that means we can not override that method in subclass i.e we can not provide the method body into the subclass.
3 :if we declare class as final means we can extends that class.i.e.we can not inherit that class into subclass


finally :

finally is block which is associated with try - catch for resource deallocation.finally is block which 100% excute if any Exception raise or not.if any resource allocated that deallocation code we write into the finaaly block.




finalize :

finalize is a method of object class is also used for deallocation of object.that method is automatically callad by object.if any object going to Garbadge Collection before object going to Garbadge Collection the fialize method is callad for resource deallocation of object.i.e means database connection any network connection allocated that resource are deallocated into the finalize method.

Wednesday, 6 August 2014

                              OOPS Conecpt Of Java


Introduction:

     opps stand for "Object Oriented Programming System".OPPs is technique not technology,that means it does not provide any syntax or any predefined API for programing,it only provides the suggestion to design and develop objects in programming language.

Why OOPs?

      oops is methodology introduced to represents real world objects using a program for automating real world business by achiving security because business nedd security.
 All living and non-living things are callad object.so the real worls objects such as Person,Annimal,Bike,Computer etc.

Definition of OPPs:

OOP is methodology that provide a way od modulirizing program by creating partitioned memory area for both data and methods that can be used as template for creating copies od such module(Ojects on Demand)  

What is class?

  • A class is a specification or blue print or template of an object that defines what goes to make up particular sort of objects.
  • Thus a class is a logical construct,an object is physical reality.
  • A class defines the structure,state and behaviour that will be shares by a set of objects.Hence each object of a given class contains the structure,state and behaviour defined by the class.
  • When we create a class ,we'll specify the data and code that constitude that class.Collectively these elements are callad memebers of that class.specifically,the data defined by that class are referred to as member variables or instance variables or attributes.the code that operates on that data is reffered to as member methods. 
 

What is Object?

  • Object is the physical reality of a class.
  • Technically object can be defined as "it is an encapsulated form of all non-static variables and non-static methods of particular class".
  • An instance of a class is the other technical term of an object.
  • The process of creating objects out of a class is callad instantiontion.
  • Two objects can be communicated by passing message(argument)
     above diagram shows the meanning of the logicla construct and physically reality.

Tuesday, 5 August 2014

Why java is used today?
-->Java Programming language mainly used designed to develop internet based application by providing platform independency.C,C++ Programming language supports developing only stand alone application i.e. that application are excutedd only currunt machine,can not excuted from remote machine via network call.

Java Feature:
1>Simple: Because all the difficult conecpt of c,c++ have been omotted in java,for example pointer which is very difficult for both learners and also programmer has been compltly removed from elimented from java.
2>Secure: Java is secure because java not use pointer and another is java is run on the virtual machine that's why java is secure.
3>Robust: Robust means very strong.java applies the strong managment system and also java use automatic gerbadge collection and exception handling is used for type cheking purpose.
3>Portable: Java is Portable i.e we can .class file anywhere and run that .class file any machine.
4>Object Oriented:
Object-oriented means we organize our software as a combination of different types of objects that incorporates both data and behaviour.Object-oriented programming(OOPs) is a methodology that simplify software development and maintenance by providing some rules.Basic concepts of OOPs are:
  1. Object
  2. Class
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation
5>Distributed:
        Information is distributed on various computers on network.Using java,we can write programms which capture information and distribute it to the client.This is possible because java can handla the protocols like TCP/IP and UDP.

6>Robust:
    Robust means strong java programms are strong java and they don,t crash easily like c or c++ program.there are two reason for this firstly,java has got excellent inbuild exception handling facility.If an exception occures,the program terminates abruptlygiving rise to problems like loss of data.Overcme such problem is callad exception handling.this means even though an exception occurs in a Java Program ,no harm will happen.