Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Jun 25, 2001

Testing New Operations For Null


Beginning Java programmers sometimes test the results of a new operation for a null. The code for this test looks like this:
 
    Integer i = new Integer (400);
    if (i == null)
        throw new NullPointerException();

This test is not wrong, but it is unnecessary. The two lines making up the if and the throw are wasted. They serve only to make the program fatter and slower.

C/C++ programmers often do this initially because testing the results of malloc() in C is necessary, and failing to do so creates a bug. In Java, however, new is not permitted to return null. If it does, the JVM is most likely crashing and the test isn't going to help.
Navneet Gupta
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap