Stopping java threads correctly

1.wait causes the current thread to wait and give away the lock it holds. That's why wait is always called in a synchronized block on the locking object of this block. When some other thread using the same locking object calls notifyAll, all other threads waiting on this locking object need to be notified. So…

Read More

Comparator vs Comparable

  Parameter Comparable Comparator Sorting logic Sorting logic must be in same class whose objects are being sorted. Hence this is called natural ordering of objects Sorting logic is in separate class. Hence we can write different sorting based on different attributes of objects to be sorted. E.g. Sorting using id,name etc. Implementation Class whose…

Read More

Comparisons using NaN

Noncompliant Code Example This noncompliant code example attempts a direct comparison with NaN. In accordance with the semantics of NaN, all comparisons with NaN yield false (with the exception of the != operator, which returns true). Consequently, this comparison always return false, and the "result is NaN" message is never printed. public class NaNComparison {…

Read More

Forward references in Java

This is specifically a compilation error. And its all about ordering of class variable declarations. Let's use some code for illustrative purposes: public class ForwardReference { public ForwardReference() { super(); } public ForwardReference echoReference() { return this; } public void testLegalForwardReference() { // Illustration: Legal this.x = 5; } private int x = 0; //…

Read More

Swaminr Note on SCJP

This is a old posting from geocities which I like to preserver:   These notes were collected while I was preparing for the SCJP2 exam. The collected information is from various online Java tutorials, mock exams, certification books and the online discussion groups. If someone has any comments or any corrections that you want to…

Read More

Übersicht Exceptions

Unchecked  ArrayIndexOutOfBoundsException ClassCastException  IllegalArgumentException  IllegalStateException  NullPointerException  NumberFormatException  AssertionError  ExceptionInInitializerError  StackOverflowError  NoClassDefFoundError  Checked  Exception  IOException  FileNotFoundException  ParseException  ClassNotFoundException  CloneNotSupportedException  InstantiationException  InterruptedException NoSuchMethodException NoSuchFieldException

Read More