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]

Wednesday, 31 October 2012

What are different types of inner classes? Nested -level classes, Member classes, Local classes, Anonymous classes. Explain them




There are 4 types of inner classes - Member inner class, Local inner class, Static inner class and Anonymous inner class
1. Member inner class – A member of a class(enclosing class).
2. Local inner class – An inner class that is defined within a block.
3. Static inner class – Like static members, this class itself is static.
4. Anonymous inner class – A class without a name and implements exactly only one interface or exactly extends one abstract class.

The enclosing class can not have the accessibility to the inner class. To do so, the outer class must create an object of its nested class except for static inner class. The member classes can access all of its enclosing class’s members, because inner class is like a member of a class.
Anonymous inner class is used when an object creation, one time single method invocation to be done and releasing the object at once. This is particular in event driven programming.
The nested classes are implemented in handling AWT, Swing and Applet programming. An anonymous inner class is invoked when an action event is to be performed.

What is a “dirty read”?


In typical database transactions, one transaction reads changes the value while the other reads the value before committing or rolling back by the first transaction. This reading process is called as ‘dirty read’. Because there is always a chance that the first transaction might rollback the change which causes the second transaction reads an invalid value. 

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...