How to Add Elements of two Arrays in Java - Example

One of the common programming exercise on various Java course is addition and multiplication of two arrays. How do you add two integer arrays in Java? Can you add two String array? how about other data types etc? These are some of the interesting questions because Java doesn't support operator overloading. You cannot use the plus operator to add two arrays in Java e.g. if you have two int arrays  a1 and a2, doing a3 = a1 + a2 will give compile time error. The only way to add two arrays in Java is to iterate over them and add individual elements and store them into a new array. This is also not easy because the array can be of different length, so you need to make some rules and apply them to your method e.g. you can throw IllegalArgumentException if you get two arrays which are not of the same type and their length is different.

Difference between Daemon Thread vs User Thread in Java?

A thread is used to perform parallel execution  in Jaa e.g. while rendering screen your program is also downloading the data from the internet in the background. There are two types of threads in Java, user thread and daemon thread, both of which can use to implement parallel processing in Java depending upon priority and importance of the task. The main difference between a user thread and a daemon thread is that your Java program will not finish execution until one of the user thread is live. JVM will wait for all active user threads to finish their execution before it shutdown itself. On the other hand, a daemon thread doesn't get that preference, JVM will exit and close the Java program even if there is a daemon thread running in the background. They are treated as low priority threads in Java, that's why they are more suitable for non-critical background jobs. In this article, we will learn some key difference between user and daemon thread from Java multithreading and interview perspective.

How to reverse String in place in Java - Example

One of the common Java coding interview questions is to write a program to reverse a String in place in Java, without using additional memory. You cannot use any library method e.g. StringBuilder to solve this problem. This restriction is placed because StringBuilder and StringBuffer class defines a reverse() method which can easily reverse the given String. Since the main objective of this question is to test the programming logic of candidate, there is no point giving him the option to use the library method which can make this question trivial. Now, how do you solve this problem? Since String is backed by a character array, You can use the same in place algorithm we have used to reverse an array in place. That technique uses the two pointer approach where one pointer starts from the beginning and other pointer starts from the end. You swap elements until they meet. At that point of time, your String is already reversed.