10 Java Exception and Error Interview Questions Answers

You will always see some interview questions from Exception and Error handling in core Java Interviews. Exception handling is an important aspect of Java application development and its key to writing robust, stable Java programs, which makes it natural favorites on interviews. Questions from Error and Exception in Java mostly based on the concept of Exception and Error in Java, How to handle Exception , best practices to follow during Exception handling etc. Though multithreading, garbage collection, JVM concepts and questions from object oriented design rules these interviews, you should always expect and prepare some questions on effective error handling.

5 books to learn Object Oriented Programming and Design patterns - Best of lot

Knowledge of Object oriented design principle and the various design pattern is a must for any Java developer. As I said earlier on 10 OOPS and SOLID design principles, coding without knowing these principles are like trying to learn a language without knowing alphabets. By the way, there are several books written on Object-oriented design principles, design patterns, and best practices, but only a few of them provides what they claim. There are two things here, design principles and design patterns, one is basic and other is best practice built on that basic. One should first learn Object-oriented principles and then learn design patterns to see how those principles are used to solve day to day problems. 

How to Add Two Integer Numbers without using Plus + or ++ Arithmetic Operator in Java - Recursion example

In this article, we will take a look at another interview question about adding two numbers, but without using + or ++ operator. Interview starts with a simple statement, Can you write a function to add two numbers (integers) without using + or plus arithmetic operator in Java? If you are good at maths, it wouldn’t take more than a second to say that, we can use subtraction or - operator to add two numbers because a-(-b)== a+b. Well, that’s correct, but real question starts when interviewer quickly points out that, you can not use any arithmetic operator including +,-,*,/++ or --. Programming and Coding questions are an integral part of any Java interview. You should always expect a couple of questions like this, e.g. swapping two numbers without using a temp variable. If you have been giving interviews, then you know that it will eventually come downs to the bitwise operator in Java. 

How to Generate MD5 checksum for Files in Java

MD5 checksums are good to verify the integrity of files and It's easy to generate MD5 checksum in Java. Java provides a couple of ways to generate the MD5 checksum for any file, you can either use java.security.MessageDigest or any open source library like Apache commons codec or Spring. All 3 ways we have seen in our earlier article about generating the MD5 hash for String is also applicable to generate the MD5  checksum for any file. Since most of the md5() or md5Hex() method takes byte[], you can simply read bytes from InputStream or pass to these md5 methods. Apache commons codec from version 1.4 also provides an overloaded method for accepting InputStream, which makes generating checksum very easy in Java. For those who are not familiar with checksum, it's a fixed-size datum generated from a block of data to detect any accidental change in data. 

How to get current URL, parameters and Hash tag using JQuery and JavaScript

While dealing with current URL, many time you want to know what is the current URL path, What are the parameters, and what is the hashtag on URL. The hash tag is pretty important if you are implementing tab structure using HTML and JQuery. To avoid confusion, let's take an example of URL: http://javarevisited.blogspot.com/2013/01/top-5-java-programming-books-best-good.html#ixzz2PGmDFlPd , in this example ixzz2PGmDFlPd, is a hash tag. Now, both JavaScript and JQuery provides a convenient way to retrieve current URL in the form of window.location object. You can use various properties of the window.location JavaScript object e.g. window.location.href to get complete URL, window.location.pathname to get the current path, and window.location.hash to get a hash tag from current URL. If you like to use JQuery then you can get a window.location as JQuery object and retrieve relevant properties using attr() function. 

5 Reasons to Use Composition over Inheritance in Java and OOP

Favor composition over inheritance is a one of the popular object-oriented design principles, which helps to create flexible and maintainable code in Java and other object-oriented languages. Many times I have seen people suggesting use composition instead of inheritance, in fact, my favorite books like Head first Design Patterns also advocates this design principle. Head first books have its own way of explaining, why composition is better than inheritance and though its long its quite interesting and informative. It was the first chapter of this book, which helped me a lot on understanding this key OOPS concept. In this Java and OOPS tutorial, I have tried to summarize my experience around composition and inheritance in two main category, first, difference between composition and inheritance and second, when to use Composition over inheritance in Java. 

How to find CPU and Memory used by Java process in Solaris – prstat command example

We often need to find CPU and memory utilization of a particular Java process in order to get some performance stats, capacity planning or just to understand the current load on the system. Performance monitoring and testing is an integral part of any Java application development, especially if you are working in high-frequency trading space. In order to fix a performance problem, you have to find them first and knowledge of right tools and commands helps there. One of the primary cause of poor application performance is it Java or any other process is exhaustion of important resources like CPU and memory. Monitoring CPU and memory utilization of your Java process give you some useful insight like what is the normal memory and CPU utilization, when are you getting spikes in CPU and memory consumption. If  heap memory is constantly increasing over time than it’s an indication that your Java application may have a memory leak. 

In this Java performance tutorial, we will learn about two most common Solaris commands prstat and pmap to find CPU and memory utilization of Java process. prstat is a process monitoring tool easily available in any Solaris systems including SPARC and x86 boxes, which provides useful statics of CPU and memory utilization of a process. 

For other systems e.g. windows you can use windows task manager for CPU monitoring and Linux you can use top command for same purpose. By the way, you can also check out Java performance by Charlie Hunt and Binu John, it is immensely helpful for Java performance monitoring and tuning. This is the book, which gives you right kind of tools and knowledge required to find performance stats for Java application.