Sunday, April 29, 2012

OutOfMemoryError vs StackOverflowError

OutOfMemoryError:
Thrown when Java is running out of memory and cannot allocate any more memory in the heap for the object

StackOverflowError:
The stack space lives at the top-end of the address space and the heap lives at the bottom-end of the address space. As stack grows, it grows downwards. As heap grows, it grows upwards. So there is a potential that the two can collide and when that happens, you get a StackOverflowError thrown. Typically happens when you call some methods recursively (that keeps going-and-going).


Monday, April 16, 2012

Command Line Arguments


package org.training.fundementals;

public class Args {

      /**
       * @param args
       */
      public static void main(String[] args) {
     
            System.out.println(args[0]);
            System.out.println(args[1]);
            System.out.println(args[2]);
            System.out.println(args[3]);
System.out.println(args[3]);
System.out.println(args[3]);

           

      }

}

C:\> java Args This is a simple Java program
Output:
This
is
a
simple
Java
Program


Friday, December 9, 2011

NetBeans v/s Eclipse


NetBeans is like the long lost love that has aged with grace and dignity, retaining the characteristics that made you love her in the first place, whereas Eclipse is like the aging, overeating trophy wife who thinks she’s still “all that” and lets you know about it, frequently.

Sunday, November 27, 2011

NullPointerException


package org.training.exceptions;

public class NullPointer {

      public static void main(String[] args) {
           
            new NullPointer();
      }
     
      public NullPointer(){
     
            Object[] object = {12,25,68,45};
           
            try {
                    for (Object o : object ) {
                          System.out.println(o);
                    }
                    // explicitly dereferencing object array
                    object = null;
                   
                    for (Object o : object ) {
                          System.out.println(o);
                    }
                   
            }catch(NullPointerException nfe) {
                  nfe.printStackTrace();
            }
    }

}

Output:

12
25
68
45
java.lang.NullPointerException
      at org.training.exceptions.NullPointer.<init>(NullPointer.java:21)
      at org.training.exceptions.NullPointer.main(NullPointer.java:7)

ClassNotFoundException


ClassNotFoundException is one of the most commonly occurring exceptions. This exception is thrown when the executing program is unable to find the defined class. This could be because of various reasons, wrong classpath, older version of jar file etc...

package org.training.exceptions;

public class ClassNotFound {

      public static void main(String[] args) {
           
            new ClassNotFound();
      }
     
      public ClassNotFound(){
     
            try {
                  // load the java.lang.Strung class,
                  // this would thrown an ClassNotFoundException
 because there is no class called “Strung” defined in the java.lang package  
             Class c = Class.forName("java.lang.Strung");
            }catch(ClassNotFoundException cnfe) {
                  cnfe.printStackTrace();
            }
    }

}

Output:

java.lang.ClassNotFoundException: java.lang.Strung
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Unknown Source)
      at org.training.exceptions.ClassNotFound.<init>(ClassNotFound.java:14)
      at org.training.exceptions.ClassNotFound.main(ClassNotFound.java:7)

NoSuchMethodException


package org.training.exceptions;

import java.lang.reflect.Method;

public class NoSuchMethod {

      public static void main(String[] args) {
           
            new NoSuchMethod();
      }
     
      public NoSuchMethod(){
     
            try {
                  // load the java.lang.String class, if not found throws            ClassNotFoundException
                  Class c = Class.forName("java.lang.String");
           
                  try {
                  Class[] arguments = new Class[2];

                  // search for method called "someMethod" with two parameters
                  // since there is no method called "someMethod(arg1, arg2)", in the java.lang.String class, this throws an NoSuchMethodException
                  Method m = c.getDeclaredMethod("someMethod", arguments);

                }catch(NoSuchMethodException nsme) {
                      nsme.printStackTrace();
                }
          }catch(ClassNotFoundException cnfe) {
                  cnfe.printStackTrace();
            }
    }

}

Output:

java.lang.NoSuchMethodException: java.lang.String.someMethod(null, null)
      at java.lang.Class.getDeclaredMethod(Unknown Source)
      at org.training.exceptions.NoSuchMethod.<init>(NoSuchMethod.java:19)
      at org.training.exceptions.NoSuchMethod.main(NoSuchMethod.java:9)

Friday, November 25, 2011

NumberFormatException


package org.training.exceptions;

public class NumberFormat {

      public static void main(String[] args) {
           
            String string = "hello";
           
            try {
                  // this parsing doesn't work as hello is not a number
                  Integer integer = Integer.parseInt(string);
                  System.out.println(integer++);
            }catch(NumberFormatException nfe) {
                  System.out.println("Number Format Exception caught, check what you are parsing");
                  nfe.printStackTrace();
            }
}


Output:

Number Format Exception caught, check what you are parsing
java.lang.NumberFormatException: For input string: "hello"
      at java.lang.NumberFormatException.forInputString(Unknown Source)
      at java.lang.Integer.parseInt(Unknown Source)
      at java.lang.Integer.parseInt(Unknown Source)
      at org.training.exceptions.NumberFormat.main(NumberFormat.java:11)