Wednesday 17 May 2017

Right way to check if String is empty in Java

Right way to check if String is empty in Java


What do you most of us do while using String in Java? checking whether String is null or empty right? I am sure you know a couple of ways to test whether String is empty or not, but do you know the right way to do it? When we talk about Strings in Java, we can imagine them as arrays of characters, and they are, but in Java, they also object. An empty Java String is considered as the not null String that contains zero characters, meaning its length is 0. However, a Java String that might only contain the white-space character is not considered as empty, it is considered to contain one character and its length is equal to 1. One of the most popular way of checking whether String is empty or not is String class' isEmpty() method, this looks perfect right, it's readable and returns boolean if String is empty otherwise returns false, but the problem is you can not call this method without checking whether String is null or not. In another word, this is not null safe and it will throw NullPointerException if String is null.

Tuesday 16 May 2017

How to calculate Sum of Digits using Recursion in Java

How to calculate Sum of Digits using Recursion in Java



This is the second part of our article to solve this coding interview question,  how to find the sum of digits of an integer number in Java. In the first part, we have solved this problem without using recursion i.e. by using a while loop and in this part, we will solve it by using recursion. It's good to know different approaches to solving the same problem, this will help you to do well on coding interviews. While finding a recursive algorithm, always search for a base case, which requires special handling. Once you find the base case, you can easily code the method by delegating rest of processing to the method itself, i.e. by using recursion.  In this problem, the base case is when the number becomes zero, at that time our program is complete and we return the sum of digits of given number. Another property of a recursive algorithm is that with each passing steps your program approaches to result and problems become shorter.

Sunday 14 May 2017

Right way to Compare String in Java

Right way to Compare String in Java


The String is a special class in Java, so is String comparison. When I say comparing String variables, it can be either to compare two String object to check if they are same, i.e. contains same characters or compare them alphabetically to check which comes first or second. In this article, we are going to talk about the right way of comparing String variables, but what is the wrong way? The wrong way is to compare String using == operator. It is one area in which almost every Java programmer  has made mistakes sometimes by comparing two String variable using == operator. Many Java developers are exposed to string comparison very early in their Java journey,  It's often required in their first few programming assignments e.g. write a program to print hello if the user enters "John".  When you first start with String in Java, you create an object using String literal syntax e.g. name = "John" and then compare using == operator, you will get the right answer, but if you take same String as user input, you will not get the correct answer.  Why? because equality operator compares references i.e. if two reference variable points to the same object in the heap then it returns true, otherwise, it returns false.

Friday 12 May 2017

What is fail safe and fail fast Iterator in Java

What is fail safe and fail fast Iterator in Java?



Java Collections supports two types of Iterator, fail safe and fail fast. The main distinction between a fail-fast and fail-safe Iterator is whether or not the underlying collection can be modified while its begin iterated. If you have used Collection like ArrayList then you know that when you iterate over them, no other thread should modify the collection. If Iterator detects any structural change after iteration has begun e.g adding or removing a new element then it throws ConcurrentModificationException,  this is known as fail-fast behavior and these iterators are called fail-fast

Thursday 11 May 2017

HashSet vs TreeSet in Java ? Similarities and Differences

HashSet vs TreeSet in Java? Similarities and Differences


HashSet and TreeSet both implement same interface i.e  java.util.Set interface and they possess the quality of Set interface means duplicate elements are not allowed. Both HashSet and TreeSet are used for to store unique elements, but HashSet doesn't care about any order and TreeSet keeps a thing in order. Ordering or sorting on TreeSet can be customized by using Comparator interface, by default TreeSet uses elements natural order for sorting, which is defined by compareTo() method of java.lang.Comparable interface. What is the difference between HashSet and TreeSet is is also one the frequently asked Java interview question, So you should know about similarities and difference between them? It also helps you to understand when to use both TreeSet and HashSet and what are the scenario when we should use this sets. Let's go through the similarities and difference between HashSet and TreeSet in Java.

Wednesday 10 May 2017

Difference between Public, Private and Protected modifier in Java

Difference between Public, Private and Protected modifier in Java?


In Java, you have got something called access modifier, which specifies accessibility of class, methods and variables. There is four access modifier in Java namely public, private, protected and the default access modifier, also known as package level modifier. The difference between these access modifier comes in their ability to restrict access to a class, method or variables, public is the least restrictive access modifier while private is the most restrictive access modifier, package and protected lies in between. Another key difference between public, protected, package and private modifier come from the point where you can apply them, for example, you cannot use private or protected modifier with a top level class but you can use public modifier there.

Tuesday 9 May 2017

Top 10 Tricky Java interview questions and Answers

Top 10 Tricky Java interview questions and Answer


What is a tricky question? Well, tricky Java interview questions are those questions which have some surprise element on it. If you try to answer a tricky question with common sense, you will most likely fail because they require some specific knowledge. Most of the tricky Java questions comes from confusing concepts like function overloading and overriding, Multi-threading which is really tricky to master, character encoding, checked vs unchecked exceptions and subtle Java programming details like Integer overflow. Most important thing to answer a tricky Java question is attitude and analytical thinking, which helps even if you don't know the answer. Anyway in this Java article we will see 10 Java questions which are real tricky and requires more than average knowledge of Java programming language to answer them correctly. As per my experience, there is always one or two tricky or tough Java interview question on any core Java or J2EE interviews, so it's good to prepare tricky questions from Java in advance.

Sunday 7 May 2017

2 Ways to find duplicate elements in an Array - Java

2 Ways to find duplicate elements in an Array - Java
Problem: You have given an array of objects, which could be an array of integers and or array of Strings or any object which implements the Comparable interface. How would you find duplicate elements from an array? Can you solve this problem in O(n) complexity? This is actually one of the frequently asked coding problems from Java interviews. There are multiple ways to solve this problem and you will learn two popular ways here, first the brute force way, which involves comparing each element with every other element and other which uses a hash table like data structure to reduce the time complexity of problem from quadratic to linear, of course by trading off some space complexity. This also shows that how by using a suitable data structure you can come up with a better algorithm to solve a problem. If you are preparing for programming job interviews, then I also suggest you take a look at Cracking the Coding Interview book, which contains 150 programming questions and solutions, good enough to do well on any programming job interviews e.g. Java, C++, Python or Ruby.

Friday 5 May 2017

Java Program to reverse an array in place? Fastest Example

Java Program to reverse an array in place? Fastest Example


It's easy to reverse an array if you have the luxury to use another array, but how would you reverse an array if a temporary buffer is not allowed? This is one of the testing array interview questions, which often proved tricky for Java programmers. Well, you can also reverse an array in place without using an additional buffer. If you know how to access array elements and how to loop over an array in Java using traditional for loop, you can easily solve this problem without using additional space. All you need to do is loop over the array fhttps://www.blogger.com/blogger.g?blogID=8114691391974361909#editor/target=post;postID=8599089079040113425rom start to the middle element and swap first element to the last, second element to the second last etc. Once you reach the middle element, your array is already sorted and that too without using any additional space. You can even use this algorithm to reverse a String in Java as well. After all, a String is backed by character array.

Wednesday 3 May 2017

Top 5 Java Main method Interview Questions with Answers

Top 5 Java Main method Interview Questions with Answers
The main() method in Java is starting point of any standalone core Java application. JVM starts executing Java program from main method and the thread which executes main is called main thread in Java. The main method is also an important topic in Java interviews for 2 to 3 years experienced developer. In this Java article, we will a couple of questions related to the main method in Java. Apart from Why main is static in Java, I see following questions keep coming related to the main method:
1.     Can we overload the main method in Java? Which main method JVM will call?
2.     Can we override the main method in Java?
3.     Can we make main final in Java?
4.     Can we make main synchronized in Java?
5.     How to call a nonstatic method from main in Java?

Monday 1 May 2017

What is Polymorphism in Java? Overriding or Overloading

What is Polymorphism in Java? Overriding or Overloading


Polymorphism vs Overloading vs Overriding
Someone asked me What are the difference between Polymorphism and Overriding in Java and the similar difference between Polymorphism and Overloading. Well, they are not two different things, Polymorphism is an object oriented or OOPS concept like Abstraction, Encapsulation or Inheritance which facilitate the use of the interface and allows Java program to take advantage of dynamic binding in Java. Polymorphism is also a way through which a Type can behave differently than expected based upon which kind of Object it is pointing. Overloading and overriding are two forms of Polymorphism available in Java.