Thursday, June 27, 2013

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 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. Some Interviewer also  test debugging skill of programmers, as resolving Exceptions quickly is another trait of solid Java programming knowledge. If programmer is familiar with infamous and dodgy ClassNotFoundException or OutOfMemoryError, there is a good chance that he has some good practical experience under his belt. In this article we will see some Java Error and Exception interview questions asked to fresher, experienced and senior Java developers in Java J2EE interviews.

Tuesday, June 25, 2013

5 must read books to learn Object Oriented and Java Design patterns - Best of lot

Knowledge of Object oriented design principle and various design pattern is 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 language without knowing alphabets. By the way there are several books written on Object oriented design principles, design patterns and best practices, but only 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 pattern to see how those principles are used to solve day to day problems. Keeping this in mind, I have collected following list of 5 books to learn Object oriented basics and design patterns in Java. These books are ordered from a beginners to advanced perspective. J2EE design patterns are little different than core Java design patterns but equally important for a Java developer, core J2EE patterns books is a good choice to learn that.

Thursday, June 20, 2013

Introduction of How Android Works for Java Programmers

Android development is current buzz in Java programming world. It's the Android, which keeps Java in the fore front from last couple of years. Now, How important is to understand or learn Android for Java programmers? Well it depends, if you like application development and want to reach mass, Android offers that opportunity to you. Millions of Android phones are available and they are keep increasing, with pace, much higher than IPhone or iOS. What all these means is, it does make a lot of sense to learn Android programming for Java programmer, and this article is about that, but this is also a one of the  good reason to learn Java programming. This tutorial will give you basic idea of How Android works? not detailed but a good overview. One distinct advantage Java programmers has over others is that Android API is much like Java API, though Android doesn't support all classes available in J2SE SDK, it supports critical ones. Another advantage is that, you can use same tools e.g. IDE like Eclipse to develop Android applications, Google provides Eclipse plug-in for Android development. On opposite, if you want to go for  iOS development, A steep learning curve with Objective C and iOS SDK waits you. I think it make more sense for a C++ programmer to do Objective C and iOS, than a Java Programmer. So classic battle of Java vs C++ still continues with Smartphone application development. Any way, let's come to the topic of How Android works internally. . If you are looking for some good books to start with Android, you can also check out Professional Android 4 Application Development and Ian F. Darwin’s Android Cookbook.

Tuesday, June 18, 2013

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

In this article, we will take a look on 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 in 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 integral part of any Java interview. You should always expect couple of questions like this, e.g. swapping two numbers without using temp variable. If you have been giving interviews, then you know that, it will eventually comes downs to bitwise operator in Java. Yes, we can add two numbers by using bitwise and bitshift operators, which is not arithmetic. Interviewer, will be happy by hearing bitwise operator, but he would like to see the code. Well, if you know binary arithmetic or how to add numbers in binary format, you may be familiar with fact than sum of two numbers can be obtained by using XOR operation and carry, by using AND operation. This is the fact, you must remember to solve this question or add two integers without using any arithmetic operator e.g. plus, minus etc. Sometime interviewer, may ask you to write both iterative and recursive solution of same question, Since recursion is another confusing programming technique, it's favored more during interviews. In this Java tutorial, we will see both recursive and iterative version of our add method, which calculate sum of two numbers without using arithmetic operator, but using bitshift and bitwise operators in Java.

Thursday, June 13, 2013

Spring HelloWorld Example in Java using Dependency Injection

In this Spring framework tutorial, we will learn how to write hello world example. This should be the first tutorial to start learning Spring framework, as it gets ball rolling and while coding and running this example, you learn a lot about Spring framework, Spring XSD files, required JAR file and more importantly how spring works.This Helloworld program in Spring framework is an example of classical Java hello world program, written using dependency Injection design pattern by using Spring Framework's IOC container. Spring is so far one of the most popular Java application framework, which promotes some best practices while writing Java application e.g. dependency Injection. Spring provides an IOC container to manage life-cycle of Spring beans and provides support to get beans any time from IOC container. Apart from Spring's IOC container, it also provide rich API to simply many of Java task e.g. JdbcTemplate help you to write JDBC code without taking care of boiler plate things like closing connection, statement, result-set etc. Similarly Spring framework also provides JmsTemplate to simply JMS related task e.g. sending and receiving messages in Java. I first learned about usefulness of Spring framework, while reading Expert Spring MVC and Web Flow, it first few chapters, which explains about how dependency injection provides improved testing, loose coupling and help in clean code. Those two chapters are so far my best read on Spring, and I also suggest to take a look. By the way in this Spring tutorial, we will  see one of the most simple example of Dependency Injection e.g. Hello Example. Message to Hello class is provided by Spring framework using Dependency Injection.

Tuesday, June 11, 2013

How to Generate MD5 checksum for Files in Java

MD5 checksums are good to verify integrity of files and It's easy to generate MD5 checksum in Java. Java provides couple of ways to generate 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 MD5 hash for String is also applicable to generate MD5  checksum for any file. Since most of 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 to accept 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. Which means once you create checksum for a file, which is based on contents of file, any change on file e.g. adding white space, deleting a character will result in different checksum. By comparing stored checksum with current checksum, you can detect any change on File. It's good practice to provide checksum of WAR or JAR files to support teams for production release. In this Java tutorial we will learn how to create MD5 checksum for any file in Java.

Thursday, June 6, 2013

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 hash tag on URL. 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 hash tag. Now, both JavaScript and JQuery provides convenient way to retrieve current URL in form of window.location object. You can use various properties of window.location JavaScript object e.g. window.location.href to get complete URL, window.location.pathname to get current path, and window.location.hash to get hash tag from current URL. If you like to use JQuery then you can get window.location as JQuery object and retrieve relevant properties using attr() function. If you are absolutely new in JQuery, and unaware of power of one of the most popular JavaScript framework, Head First JQuery is a good starting point. Being a fan of head first book, I always approach a new technology by an Head first title, it helped to learn a lot in short time, without spending time in trivial examples. By the way, In this web tutorial, we are going to retrieve current URL and hash tag using JavaScript and JQuery.

Tuesday, June 4, 2013

Why Favor Composition over Inheritance in Java and Object Oriented Programming

Favor composition over inheritance is a one of the popular object oriented design principle, 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, has 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. I have already mentioned about this design principle, in my list of 10 OOPS and SOLID design principles for Java programmers, here we will take a more closer look.

Monday, June 3, 2013

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 system. Performance monitoring and testing is 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 be it Java or any other process is exhaustion of important resources like CPU and memory. Monitoring CPU and memory utilization of your Java process gives you some useful insight like what is normal memory and CPU utilization, when are you getting spikes on 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 BinuJohn, 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.