Java

ArrayList NullPointerException

Java's ArrayList is a great Collection that frees us from IndexOutOfBound, one type limitation, and NullPointerException issues while providing several value-added methods. Naturally, you would be shocked if you encounter a NullPointerException on an ArrayList. The only time, you would encounter this problem is when you fail to initialize the ArrayList.

Converting primitive wrapper objects to String

The following example shows how to convert objects of primitive wrapper classes to String.

public class PrimitiveWrapperToString {

   public static void main(String[] args) {
     Integer i = new Integer(1);
     Double d = new Double(1.1);
     Float f = new Float(2.2);
     Long l = new Long(13);

     String si = Integer.toString(i);
     String sd = Double.toString(d);
     String sf = Float.toString(f);
     String sl = Long.toString(l);

     System.out.println(si + " " + sd + " " + sf + " " + sl);
  }
}

Java stack

Stack is a LIFO ADT (Last in first out abstract data type) and a linear data structure. In a jar the items on top (i.e. items that went in last) come our first. In a LIFO ADT, items that go in last come our first.

Stacks are a very useful data structure. Although you have the possibility of creating your own stack data structure from Java code, it is a better idea to simply use the stack provided by Java.

Code Following code shows how to use Java's stack:

Java file I/O

Java provides a number of classes and methods that allow to read and write files. In Java all files are byte-oriented. Two of the most often-used stream classes are FileInputStream and FileOutputStream, which create byte streams linked to files.

  1. To open a file, simple create an object of one of the classes specifying the name of the file as an argument to the constructor.
  2. Use close() to close the file

Syntax

Pages