In Java or other object oriented programming language, we often use Object and instance word interchangeably, but sometimes it confuses beginners like hell. I have been often asked several times, whether object and instance are the same thing or different? Why we sometimes use object and sometimes instance if they are same thing etc? This gives me the idea to write a little bit about it. I will mostly talk about Java conventions perspective. Just like we use word function in C or C++ for a block of code, which can be called by its same, but in Java, we refer them as methods. In Java functions are known as methods, similarly, objects are known as instances in Java. You have a class, which represent a blueprint of a real world thing e.g. Car, and object represents a real world car e.g. your car, my car, a red car or a blue car. They are also known as instances of the car.
Java has the very good support of handling Error and Exception, It has a well-defined Exception hierarchy and language level support to throw and catch Exception and deal with them. Java Programmers often deals with built-in exceptions from java.lang package and several others which are already defined in JDK API e.g. NullPointerException. If you have read Effective Java, you may remember the advice of Joshua Bloch regarding Exception. According to him, you should try to reuse the Exception classes provided in the JDK, e.g., IndexOutOfBoundException, ArithmeticException, IOException, and java.lang.ArrayIndexOutOfBoundsException , instead of creating new ones for a similar purpose. But there are cases when a user-defined, the custom exception provides a better opportunity to deal with special cases.
One of the common coding question from Java interviews is how to test if an Array contains a certain value or not? This is a simple question but sometimes interview pressure makes candidates nervous. Since array in Java doesn't have any inbuilt method for search, interviewer prefers to ask this question, to see how a candidate deals with such situation. If you have good knowledge of Java API then you will immediately come to know that there are alternatives available e.g. binary search of Arrays class or taking advantage of ArrayList contains method by first converting your array to ArrayList. If you come up with those solutions, Interviewer will surely ask you to write down a method to search an element in an array without using any library method. You can easily solve this question if you know linear search or binary search algorithm.
We often need to convert a floating point number into integral number e.g. a double or float value 234.50d to long value 234L or 235L. There are a couple of ways to convert a double value to long value in Java e.g. you can simply cast a double value to long or you can wrap a double value into a Double object and call it's longValue() method, or using Math.round() method to round floating point value to a nearest integer. Te right way to convert a double value to a long in Java really depends on upon what you want to do with the floating point value. If you just want to truncate the double value to remove zero and take an integer value, you can simply cast double to long. If you have Double object instead of the double primitive type then you can also Double.longValue() method, this doesn't do anything but just cast the double primitive wrapped inside the Double object to long.
Modulo Operator is one of the fundamental operators in Java. It's a binary operator i.e. it requires two operands. In a division operation, the remainder is returned by using modulo operator. It is denoted by % (percentage) sign. For example 5%2 will return 1 because if you divide 5 with 2, the remainder will be 1. For a programmer it's very important to know how to use this operator, they are very important to build logic. For example, in many cases like reversing a number or checking if a number is a palindrome, you can use modulus operator with 10 to get the last digit, for example, 101%10 will return 1 or 1234%10 will return 4, the last digit. It is one of a rather less used arithmetic operators in comparison of +, -, * and /. One of the important points about the remainder operator which is not known by many Java programmer is that it can also be used with floating point numbers.