Sunday, 23 November 2014

Life cycle of a thread in java

Life cycle of a thread in java
Life cycle of a thread contains following stages :
New
Runnable
Running
Blocked or Non running
Terminated

 Release 5.0 introduced the Thread.getState method. When called on a thread, one of the following Thread.State values is returned:
  • NEW
  • RUNNABLE
  • BLOCKED
  • WAITING
  • TIMED_WAITING
  • TERMINATED

New : State of Thread before start() method .

Runnable : A thread is in running state after invocation of start() method but has not yet selected by scheduler .

Running : After Scheduler select the thread to run and run() method starts executing then that thread it comes in running state.

Blocked or NonRunning : when a thread goes into wait() or sleep state then its called to be in non running or blocked .It comes into runnable state only if notify or notifyAll() is called on that thread.

Terminated : after a thread is being stopped by stop () method ,it comes into terminated state and can never go back to runnable or running state .



Read More »

Friday, 21 November 2014

Java Programs for interview

Read More »

Program in Java To print Fibonacci series using recursive method

Program in Java To print Fibonacci series using recursive method
Hi all , Today I am going to write a post on popular interview program which is Write a program in java to print Fibonacci series  using recursive method.

This post is actually an extension of my previous post program in java to printFibonacci series  which you can find here .

But in most of interviews Fibonacci series is asked to print using recursive method .
So What is recursive method ????

Actually as the name signifies a recursive method is one which repeatedly calls itself .While writing a recursive method the most common mistake programmer do is not to properly define the end of that method mean on which condition this method will stop calling itself which can led to Stackoverflow error L

So Please take care of that mistake .So here is program in java to print Fibonacci series  using recursive method


import java.util.Scanner;


public class fibonacci {
    
     public static void main(String[] args) {
         
          Scanner in = new Scanner(System.in);
          System.out.println("please enter length of series in next line");
          int length=Integer.parseInt(in.nextLine());
         
          for (int i = 1; i <=length; i++) {
              System.out.print(" "+fibo(i));
             
          }
     }
    
     public static int fibo(int n)
     {
          //System.out.println(n+"n");
          if(n == 1)
          {
          return 0;
          }
          else if(n==2)
          {
              return 1;
              }
          else
          {
              return (fibo(n-1)+fibo(n-2));
          }
     }
    

}





So this was my post on To print Fibonacci series in java using recursive method.If you have any doubt or you know another efficient method please do write me .If you like this post please comment below  :)


you may like :
Java Interview Questions 
Difference between equals() and = =
Read More »

Program in Java To print Fibonacci series

Hi all , Today I am going to write a post on popular interview program which is Write a program in java to print Fibonacci series .
So what is Fibonacci series
In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence:
 1 1 2 3 5 8 13 21 34 55 89 144 ...
or (often, in modern usage):
0 1 1 2 3 5 8 13 21 34 55 89 144 ....

The Fibonacci spiral: an approximation of the golden spiral created by drawing circular arcs connecting the opposite corners of squares in the Fibonacci tiling  this one uses squares of sizes 1, 1, 2, 3, 5, 8, 13, 21, and 34.
By definition, the first two numbers in the Fibonacci sequence are 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.
So here is very simple method to print Fibonacci series of first 20 numbers :


public class fibonacci {
           
            public static void main(String[] args) {
                        int first=0;
                        int second =1;
                        int element =0;
                        System.out.print(first);
                        System.out.print(second);
                        for (int i = 0; i < 20; i++) {
                                    element=first+second;
                                    System.out.print(" "+element);
                                   
                                    first=second;
                                    second=element;
                        }
            }
           

}

Output is :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946

Now I am going to show how to print Fibonacci series  with user input . For example if user enters 3 then first three numbers should be printed . if users inputs 5 then first first 5 numbers of the Fibonacci series will be printed .
So The program below is print Fibonacci series  with user input in java :

import java.util.Scanner;


public class fibonacci {
           
            public static void main(String[] args) {
                       
                        Scanner in = new Scanner(System.in);
                        System.out.println("please enter length of series in next line");
                        int length=Integer.parseInt(in.nextLine());
                        int first=0;
                        int second =1;
                        int element =0;
                        System.out.print(first);
                        System.out.print(" "+second);
                        for (int i = 0; i < length; i++) {
                                    element=first+second;
                                    System.out.print(" "+element);
                                   
                                    first=second;
                                    second=element;
                        }
            }
           
Program in Java To print Fibonacci series

}


In the above program in.nextLine () will return String that’s why we need to convert it in Integer as we need an integer number in for loop.
One more way is there to print Fibonacci series in java is using recursive function which you can find here .


So this was my post on To print Fibonacci series in java.If you have any doubt or you know another efficient method please do write me .If you like this post please comment below  :)


You may like :
Custom Exception in Java -Tutorial
Read More »

Thursday, 20 November 2014

Java program to reverse a string

Java program to reverse a string
Hi all , Today I am going to write a post on very popular interview program which is How to Reverse a string in Java .
As String interview question have always been a favorite of almost all interviewers there’s always been a program on String manipulation in an interview .String programs vary from searching a particular word  in a sentence or a file ,reverse a string ,Number of times a particular word or character appears in a sentence or file and many more .

So today I am going to discus how to reverse a String in java .This program also helps to find that whether a String is Palindrome or not .

A palindrome String can be checked by comparing the original String with the reverse of that String with the help of  equals() method or “==” .If both are equal then that means the String is palindrome .But for that first you need to know about how to reverse a String .
The reverse of a String example I have done it in two ways.
  1. Reverse of String without using reverse method or Reverse a String using CharAt() method or Reverse a String without using String Buffer .
  2. Reverse of String using reverse method or Reverse a String without using String Buffer .
If you know both of the methods to reverse a String then you can impress the interviewer by writing both the methods J

So , Here is the Example of  Reverse of String without using reverse method  or Reverse a String using CharAt() method or Reverse a String without using String Buffer .

 
 
import java.util.*;
class ReverseString
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);
 
      System.out.println("Enter a string to reverse");
      original = in.nextLine();
 
      int length = original.length();
 
      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);
 
      System.out.println("Reverse of entered string is: "+reverse);
   }
}
 
 
 
Explanation:
1. In the above Example we are asking the user to enter a String to reverse.
2. After calculating the length of the String we are using a loop to get each character from end of String ( as we are starting the loop form end) and then appending the characters using + operator .

This is quite simple example .Hope you all understand.

Now Comes the second method of Revere a String.
Reverse of String using reverse method or Reverse a String without using String Buffer .

 
import java.util.*;
class InvertString
{
   public static void main(String args[])
   {
      StringBuffer a = new StringBuffer("Java programming is fun");
      System.out.println(a.reverse());
   }
}
 

In this example we are using StringBuffer and its reverse method to reverse a string .

If you want input from user in StringBuffer case then you can use the program below :

 
import java.util.*;
class ReverseString
{
   public static void main(String args[])
   {
      String original ;
      Scanner in = new Scanner(System.in);
 
      System.out.println("Enter a string to reverse");
      original = in.nextLine();
 
      
      StringBuffer a = new StringBuffer(original);
      System.out.println(a.reverse());
 
   }
}




So this was my post on How to Reverse a String in Java .If you have any doubt or you know another efficient method please do write me .If you like this post please comment below  :)

You might interested in :

Program to print Fibonacci series in Java 

Read More »

Difference between an Application server and a Web server

Difference between an Application server and a Web server
Hi all , Today I am going to write a post on very popular question which is what is the difference between an application server and a web server or you can say what is the difference between an app server and web server or sometimes in interviews it can asked as what is difference between tomcat and weblogic server .

So first I ll tell you the difference between an application server and a web server and then particularly tomcat and weblogic too. Since tomcat and weblogic are also examples of web server and application server but still I ll let you know the proper difference between tomcat and weblogic server.

If interviewer asks you jut one line difference between an application server and a web server then you can say that EJB applications (which ends with .ear) can be deployed on application server but not on web server but servlet and JSP application such as .war files can be deployed on web server .though an application server can also contain a web server then we can deploy .war files on that too.

Now lets come to detailed difference between an application server and a web server.

Application server
Web server
Application servers supports both EJB and transaction management

Web server supports only Servlets and JSPs
Application server exposes business logic to the client
Web server returns a HTML response to the client they can handle only http requests.
Application server can contain web server in them for e.g. JBOSS have JSP and servlet container in them

Application are heavy as compared to web server
Web server are also known as web container .
Example of Application servers are Weblogic ,JBOSS
Example of Web server is Tomcat.


Now I am going to tell you the detail difference between tomcat and weblogic server. Though its actually same as difference between an application server and a web server.

Weblogic
Tomcat
supports both EJB and transaction management

supports only Servlets and JSPs
exposes business logic to the client
returns a HTML response to the client they can handle only http requests.
Is owned by Oracle
Is owned by Apache Foundation
Supports both FTP and HTTP request.
Supports only HTTP requests.


That’s all from my side on difference between an application server and a web server.and difference between tomcat and weblogic server.
If you any more difference  between an application server and a web server or difference between tomcat and weblogic server then pleasee let me know I ll be more happy to include it .
If you like my post then please comment or give me + on google plus :)

Read More »

Tuesday, 18 November 2014

Difference between Arrays.sort() and collections.sort() in java


Collections.sort()

Arrays.sort()
Collections.sort operates on a List
Arrays.sort operates on an array

Use Collections.sort() if you're dealing with something that implements the Collection interface - example: ArrayList

Use Arrays.sort() if you're dealing with an Array
Collections.sort() has a input as List so it does a translation of List to array and vice versa which is an additional step while sorting.
So this should be used when you are trying to sort a list.

Arrays.sort is for arrays so the sorting is done directly on the array.
So clearly it should be used when you have a array available with you and you want to sort it.


But internally both are same as collections.sort() uses arrays.sort() only to sort the elements.
The algorithm used for this sorting is mergesort algorithm.
Read More »