czwartek, 15 września 2011

IO

1. If directory in which the file should be created does not exist, method createNewFile() throws IOException.

        try {
            File f = new File("test", "test.txt");
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
----------------------------------------------------------------------------------------------------------

2. Method renameTo accepts only File objects not String!

Prototype of the method:
public boolean renameTo(File dest)
----------------------------------------------------------------------------------------------------------

3.  If there is no directory structure, PrintWriter throws FileNotFoundException.


    try {
        PrintWriter pw = new PrintWriter("test/test/test.xml");
    }catch(FileNotFoundException e) {
        e.printStackTrace();
    }


  when the directory structure exists, the physical file is created by PrintWriter


    try {
        PrintWriter pw = new PrintWriter("test.xml");
    }catch(FileNotFoundException e) {
        e.printStackTrace();
    }
----------------------------------------------------------------------------------------------------------
4. flush() method is only available in writers: FileWriter, BufferedFileWriter, PrintWriter.
    flush() method is not available in readers.

poniedziałek, 12 września 2011

INITIALIZATION BLOCKS

1. It is legal to create instance of outer class within both: static and instance initialization blocks.
    E.g.:

    class X {
         { X x = new X();}
          static { X x = new X();}
      }

2. It is legal to use this in initialization (non-static) block:

    class X {

        int a;
       {this.a = 4;}
    }

3.

   class X{
       {x = 4}
        int x;
     
        public X() {
           System.out.println(x);         //prints 4
         }
    }



     class X{
       {x = 4}
        int x = 1;
     
        public X() {
           System.out.println(x);         //prints 1
         }
    }



     class X{

        int x=1;
       {x = 4}

        public X() {
           System.out.println(x);         //prints 4
         }
    }

   exactly the same is in case of static initialization blocks

niedziela, 11 września 2011

COLLECTIONS

1. Collections methods such as: sort(), binarySerach() or reverse() accepts only Lists not Sets.
Notice also that sort() and binarySerach() methods accept only Lists containing Comparable elements of the same type. If you try to sort lists containing non-comparable elements compilation fails. If the elements are of different types ClassCastException is thrown at runtime (unless you use your own Comparator).
To reverse order you do need have list of Comparable objects.

sobota, 10 września 2011

INNER CLASSES

1. Inner non-static class must not have static fields or methods.
2. Inner static class may have static fields and methods
3. It is illegal to add/hide static methods in anonymous inner class

eg.

class X {
      public static void do() {}
}

class Y{

public void method() {
    X x = new X() {
           public static void do() {}             //compile error
   };
}

GENERICS

1.

It is incorrect:


-using wildcards in type variable declaration, eg.


public <?> void methods() {
....
}


public <? extends T> void methods() {
....
}


public <? super Comparable> void method() {
....
}




- using super:

public <T super Comparable> T get() {
....
}


It is correct:

public <T> T get() {
....
}

public <T extends Comparable> T get() {
....
}
------------------------------------------------------------------------------------------------------------

2. Return types

Notice also that the last example method can return only T - type object.

public <T extends Comparable> T get() {
    return (T) new String("");  //correct
}

public <T extends Comparable> T get() {
    return new String("");                    //incorrect
}


public <T extends Comparable> T get(T t) {
        return t;           //correct
}
------------------------------------------------------------------------------------------------------------

3.

It is correct, int[] is a Object!
List<?> l = new ArrayList<int[]>();

This one is incorrect:
List<?> l = new ArrayList<int>();
------------------------------------------------------------------------------------------------------------

4. Compiler warning  is thrown every time one tries to assign non-generic element to generic one.


       public static void main(String... s) {
List<Object> l = null;
                ArrayList l2 = null;
                 l = l2;    //warning
}


the exception is ? :


        public static void main(String... s) {
List<?> l = null;
                ArrayList l2 = null;
                 l = l2;    //no warning
       }

------------------------------------------------------------------------------------------------------------
5. Cannot create objects using wildcards, eg.:

new ArrayList<?>();
new ArrayList<? extends Number>();
new ArrayList<? super Number>();
new HashMap<String, ? extends Number>();
------------------------------------------------------------------------------------------------------------

6. One has to declare the type before return type and after method modifiers, e.g.:


    public static <E> void add(E e) {}
    public <E> static  void add(E e) {}   //incorrect
    public static  void <E> add(E e) {}    //incorrect




REGEX

1.

 [a-f] search for a, b, c, d, e or f
 [af] search for a or f
 \w search for [a-zA-Z_0-9]
 \s  search for space
 \d search for [0-9]
 . search for any character
\. search for dot

? - 0 or 1 time
* - 0 or more times
+ - 1 or more times
{n} - exactly n times
{n, } - minimum n times
{n, m} - minimum n times and maximum m times
------------------------------------------------------------------------------------------------------------

2. If one tries to use group/start/end method of Matcher class and the searched string is not found, the IllegalStateException is thrown, e.g.:


        Pattern p = Pattern.compile("test");
        Matcher m = p.matcher("my text");
     
        m.find();
        System.out.println(m.group() + " " + m.start() + " " + m.end());


Keep in mind that you need use find method first and then group/start/stop, eg.


        Pattern p = Pattern.compile("test");
        Matcher m = p.matcher("test");
     
        System.out.println(m.group() + " " + m.start() + " " + m.end());

In both cases IllegalStateException is trhown.


The proper useage may be as follows:


        Pattern p = Pattern.compile("test");
        Matcher m = p.matcher("my text");
     
        while(m.find()){
            System.out.println(m.group() + " " + m.start() + " " + m.end());
        }





EXCEPTIONS

1. When Exception is thrown, the finally block is executed always before the exception is propagated.

See the following example:

try {
   System.out.println("TRY-START");
   method_that_throws_runtime_exception();
   System.out.println("TRY-STOP");
} finally {
    System.out.println("FINALLY");
}

the result is: TRY-START   FINALLY and then the information about the runtime exception.
------------------------------------------------------------------------------------------------------------

2. One more thing regarding above scenario. Please, look at this example:


try {
   System.out.println("TRY-START");
   method_that_throws_runtime_exception();   /e.g. NullPointerException
   System.out.println("TRY-STOP");
} finally {
    System.out.println("FINALLY"); 
    throws new Exception();
}

The propagated exception is Exception not NullPointerException!
And the result is: TRY-START FINALLY information about Exception.



When the catch block handles exception:


try {
   System.out.println("TRY-START");
   method_that_throws_runtime_exception();   /e.g. NullPointerException
   System.out.println("TRY-STOP");
} catch (Exception e) {
    throws new Exception();
} finally {
    System.out.println("FINALLY");
}

The propagated exception is Exception not NullPointerException!
And the result is: TRY-START FINALLY information about Exception.

------------------------------------------------------------------------------------------------------------

3. It is correct:
 
   void method() throws FileNotFoundException {}
   
   but this one is incorrect:
 
   try {
   } catch (FileNotFoundexception e) {
   }