Skip navigation
2011

1. Novice level

   

Code block:

 

public class Bear {

 

    private String name;

 

    public boolean isMatch(String mName) {

          if (name.eqauls(mName)) {       

              return true;

          }

         

          return false;

    }

 

    public void setBearName(String name) {

          this.name = name;

    }

}

 

public class Tester {

 

    public static void main(String[] args) {

          Bear bear = new Bear();

          bear.isMatch("Foolish Bear");

    }

}

 

How about the result ? You will smile immediately, because it's too easy. Object filed in java will not assign value by JVM initially.

 

2. Intermediate level

 

Code block:

 

public class Bear {

    

    private String name;

   

    public Bear(String name) {

          this.name = name;

    }

     ... ...   

 

}

 

public class BearFactory {

 

    private LinkedBlockingQueue<Integer> bearPool;

 

    private Bear[] bearArray;

 

    public BearFactory() {

          bearPool = new LinkedBlockingQueue<Integer>();

          bearArray = new Bear[10];

          init();

    }

 

    public void init() {

          for (int i=0; i<10; ++i) {

               bearPool.push(new Bear());

          }

    }

   

    public int sizeOfBearPool() {

         return bearPool.size();

    }

   

    public int availableBearIndex() {

     

         return bearPool.poll();

    }

 

    public Bear getBear(int index) {

        return bearArray[index];

    }

 

}

 

public class Tester {

 

    public static void main(String[] args) {

         

         BearFactory factory = new BearFactory();

        

         if (factory.sizeOfBearPool() > 0) {

              factory.getBear(factory.availableBearIndex()).isMatch("this is trap bear");

         }

    }

}

 

How about this result ?

Actually here has two issues:

1. Box and unbox for primitive type. Because bearPool.poll() will return null for Integer type, but that method need return int type. That casting will report nullpointer exeception.

2. Use wrong collection fetch mode. Do you remember this mode?

if (collection.size() > 0 )

   do something with that collection.

 

Effective java already give comments on that part.

 

 

 

3. Practical level

 

Code block:

 

public class Bear {

 

    private String name;

   

    private Logger logger = Logger.getLogger(Bear.class);

 

    public Bear(String name) {

          this.name = name;

    }

 

    public boolean isMatch(String mName) {

          if (name.eqauls(mName)) {       

              logger.log("This is trial comments");

              return true;

          }

         

          return false;

    }

 

    public void setBearName(String name) {

          this.name = name;

    }

}

 

public class Tester {

 

    public static void main(String[] args) {

          Bear bear = new Bear();

          bear.isMatch("Foolish Bear");

    }

}

 

How about this result ? ... ... Do you have answer ? ... ... Do you need hints? ... ... Ok, if you don't have any tip about that, I recommend you read java memory model. Actually the NullPointer exception will happend in  "logger.log("This is trial comments");  ". Why was that line ? Because in java memory model it will not guarantee the object instance filed constructed completely, you can call java constructor atomic issue. But in which situation you will get exception, the answer is in multi-threading exectuion senarios. How to resolve that ? try add "final static" for the logger variable construction.

Filter Blog

By date:
By tag: