Saturday 12 October 2013

What are the new features added to Servlets 2.5?

Following are the changes introduced in Servlet 2.5:
A new dependency on J2SE 5.0
Support for annotations
Loading the class
Several web.xml conveniences
A handful of removed restrictions
Some edge case clarifications

Thursday 21 February 2013

How to write program Map interface in collection ?

import java.util.*;
public class Where
{
    public static void main(String args[])
    {
   HashMap h=new HashMap();
      h.put(111, "ram");
           h.put(112, "neha");
    h.put(113, "hem");
     h.put(114, "sallu");
      Set s=h.entrySet();
      Iterator it=s.iterator();
      while(it.hasNext())
      {
     Map.Entry me=(Map.Entry)it.next();
        System.out.println("me.getKey():" +me.getKey());
        System.out.println("me.getValue():" +me.getValue());
       
        }   

   System.out.println();
}   
}
   

output:-

for compile-
E:\sallu>javac Where.java
for run-
E:\sallu>java Where
me.getKey():114
me.getValue():sallu
me.getKey():113
me.getValue():hem
me.getKey():112
me.getValue():neha
me.getKey():111
me.getValue():ram


E:\sallu>

Saturday 2 February 2013

Difference between StringBuffer, String and StringBuilder in Java



1) String is immutable while StringBuffer and StringBuilder is mutable object.
2) StringBuffer is synchronized while StringBuilder is not which makes StringBuilder faster than StringBuffer.
3) Concatenation operator "+" is internal implemented using either StringBuffer or StringBuilder.
4) Use String if you require immutability, use Stringbuffer in java if you need mutable + thread-safety and use StringBuilder in Java if you require mutable + without thread-safety.

Wednesday 30 January 2013

Can we execute without main () method Program in java

// * execute without main method Program *//

class Maind
{
static
{
System.out.println("Hello execute without main method.");
System.exit(0);
}
}

Compile :     javac Maind.java
       Run :    java Maind
Output :-
               Hello execute without main method.
______________________________________________


// *....... execute without main method Program........... /


class Maind
{
static
{
System.out.println("Hello execute without main method.");
}
}

Compile : javac Maind.java
Run: java Maind

Output :-
               Hello execute without main method.
               Exception in thread "main" java.lang.NoSuchMethodError: main

Thursday 10 January 2013

how to Remove Duplicate Charatcers From String (i.e. AAA BBB---AB)

import java.util.*;

//class RemoveDuplicateCharatcersFromString = Main9
class Main9
{
public static String removeDuplicates(String s) {
StringBuilder build = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
String st = s.substring(i, i + 1);
if (build.indexOf(st) == -1) {
build.append(st);
}
}
return build.toString();
}
public static void main(String[] args)
{
String str="AAA BBB ";
String newString=removeDuplicates(str);
System.out.println(newString);
}
}
out put:-
                 A B

Tuesday 1 January 2013

How to use method for calculating Fibonacci series?

public class MainClass {
   public static long fibonacci(long number) {
      if ((number == 0) || (number == 1))
         return number;
      else
         return fibonacci(number - 1) + fibonacci(number - 2);
   }
   public static void main(String[] args) {
      for (int counter = 0; counter <= 10; counter++){
         System.out.printf("Fibonacci of %d is: %d\n",
         counter, fibonacci(counter));
      }
   }
}
output:
 
 
Fibonacci of 0 is: 0
Fibonacci of 1 is: 1
Fibonacci of 2 is: 1
Fibonacci of 3 is: 2
Fibonacci of 4 is: 3
Fibonacci of 5 is: 5
Fibonacci of 6 is: 8
Fibonacci of 7 is: 13
Fibonacci of 8 is: 21
Fibonacci of 9 is: 34
Fibonacci of 10 is: 55

How to find the minimum and the maximum element in an array

import java.util.Arrays;
import java.util.Collections;

public class Main3 {
   public static void main(String[] args) {
      Integer[] numbers = { 8, 2, 7, 15, 4, 9, 5,99};
      int min = (int) Collections.min(Arrays.asList(numbers));
      int max = (int) Collections.max(Arrays.asList(numbers));
      System.out.println("Min number: " + min);
      System.out.println("Max number: " + max);
   }
}

// *.....How to reverse array........*//


import java.util.ArrayList;
import java.util.Collections;

public class Main {
   public static void main(String[] args) {
      ArrayList arrayList = new ArrayList();
      arrayList.add("A");
      arrayList.add("B");
      arrayList.add("C");
      arrayList.add("D");
      arrayList.add("E");
      System.out.println("Before Reverse Order: " + arrayList);
      Collections.reverse(arrayList);
      System.out.println("After Reverse Order: " + arrayList);
   }
}

o/p:

Before Reverse :- [A, B, C, D, E]
After  Reverse :- [E, D, C, B, A]

What is Map Collection?

A map collection refers to a set of maps that are compiled and organized for a specific purpose, such as research, education, or preservatio...