Article Library

The marketingsource.com article library contains thousands of articles on topics of interest to business owners, marketing professionals, and much more. New articles are added daily.

Search:

 


NEW!  FREE Do-It-Yourself Press Release Kit     Get It Now (Adobe PDF)
 
Close This Window

Free Do-It-Yourself Press Release Kit


Fill out the information below to receive our free e-book. We'll automatically redirect you to the download area.
First Name: *
Last Name: *
Company:
Email: *
Phone #:

Are you interested in any of the following?

Press Release Writing Services
Press Release Distribution Services

Notify me of special offers and promotions
Subscribe me to the Getting Results newsletter


Close This Window

An alternative procedure to returning null values in java Java training

by: fresherlab on Date: Sat, 31 Jul 2010 Time: 9:26 AM


Java training courses are on everything from the basics to advanced techniques for development with Java, Enterprise JavaBeans (EJB), Java EE, and JavaServer Faces (JSF). Every Java training engagement is customized to ensure that the training content and materials align with your business, technology, and organizational needs.
Java programming is that it seems common practice to test returned objects for null. Receiving null is confusing. When you make a function call and the result is null what does it mean? Was the data not found? Was there an exception in the function that was silently swallowed resulting in the return of a null object? You're not really sure if null is an expected return value. If no exception is thrown then you just kind of assume that null is a valid return value. However, you can't really do anything with the returned null object so what do you do now?
Here are a couple of alternatives to returning null:
Use the Empty Collection Methods
If you are working with returning collection objects from your function you should return an empty collection rather than a null object. This is very easy to do with the Collections class. There are three static methods available:
• Collections.emptyList()
• Collections.emptySet()
• Collections.emptyMap()
An empty List is easily instantiated for return as shown here:
List myList = Collections.emptyList();
Returning an empty List eliminates the required check for null by the function caller. Now instead of having code which looks like this:
List names = getNames();
if(names != null) {
for(String name : names) {
System.out.println(name);
}
}
You can simply write the following without worrying about a NullPointerException:
List names = getNames();
for(String name : names) {
System.out.println(name);
}
This second example is much cleaner, easier to read and understand. Returning empty Collections is much safer as well since it avoids the potential run time exception: NullPointerException.
An unchecked exception represent defects in the program (bugs) - often invalid arguments passed to a non-private method. To quote from The Java Programming Language, by Gosling, Arnold, and Holmes : "Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time."[1]
Certainly an empty collection is not a bug! So lets just return an empty collection rather than a null reference. In addition, testing for an empty collection is a lot more intuitive than testing for null.
List names = getNames();
if(names.isEmpty()) {
//throw an Exception
//or do something else
//or do nothing at all
}
Throw an Exception
It's perfectly acceptable to throw a custom checked exception alerting the calling client that the requested object cannot be populated or retrieved. This approach is much more revealing and forces the calling client to handle the potential problem. It also eliminates the need to check the returned object for null.
A checked exception is guaranteed to be handled so you gain more control over your program. A RuntimeException may or may not be handled by the caller and will bubble up until either it is caught or reaches the top of the caller chain.
Conclusion
Nulls are useful under certain conditions when null values are expected. However, for the above conditions with collections and data retrieval it has been abused. Use the empty collection and avoid returning null objects whenever possible. It will make code cleaner, easier to read and debug.


About the Author

Fresherlab.com is a young organization, based at India’s IT hub, Bangalore.
It's a dynamic and competitive world with full of ups and downs in IT sector and therefore; the fresh engineers require just more than theoretical knowledge to get themselves ready for the industry. Academic institutions across the world provide the basic and conceptual fundamentals covering multiple areas in computer science. With increasing number of graduating engineers, but with constant number of companies, it becomes difficult for fresh engineers to compete with their unpolished skills because they need more effective and specialized quality training.




Rating: Not yet rated
Login to vote | Not Registered? Create an account

Comments

No comments posted.

Add Comment

You do not have permission to comment. If you log in, you may be able to comment.


 

      Phone:  1-877-732-6101

      E-Mail:   Send Us A Message

 

News + Promotions

Enjoy exclusive promo offers, including an immediate 10% discount. Plus catch valuable marketing advice and trends with our weekly newsletter.

Offers and Discounts
Weekly Newsletter

Did You Know?

You can increase direct mail response rates dramatically by integrating your traditional direct mail campaign with an e-marketing campaign.

Tip #1
Mailing to prospects you've had contact with before? E-mail them a few days before your mailer arrives, and they'll be more likely to respond.

Need help integrating your direct mail with e-marketing?
We have extensive experience fusing traditional and internet marketing. Contact Us Today