HTML

How to use Callable Statement in Java to call Stored Procedure? JDBC Example

The CallableStatement of JDBC API is used to call a stored procedure from Java Program. Calling a stored procedure follows the same pattern as creating PreparedStatment and than executing it. You first need to create a database connection by supplying all the relevant details e.g. database URL, which comprise JDBC protocol and hostname, username, and password. Make sure your JDBC URL is acceptable by JDBC driver you are using to connect to the database. Every database vendor uses different JDBC URL and they have different driver JAR which must be in your classpath before you can run the stored procedure from Java Program.

How to convert int[] to ArrayList in Java 8? Example Tutorial

I once asked this question to one of the Java developers during Interview and like many other, he answered that Arrays.asList() can convert an array of primitive integer values to ArrayList<Integer> in Java, which was actually wrong. Even though, Arrays.asList() is the go-to method to convert an array to ArrayList in Java when it comes to converting a primitive array to ArrayList, this method is not useful. The Arrays.asList() method does not deal with boxing and it will just create a List<int[]> which is not what you want. In fact, there was no shortcut to convert an int[] to ArrayList<Integer> or long[] to ArrayList<Long> till Java 8.

How to copy elements of one array to another array in Java - Arrays.copyOf and Arrays.copyOfRange Example

There are multiple ways to copy elements from one array in Java e.g. you can manually copy elements by using a loop, create a clone of the array, use Arrays.copyOf() method or System.arrayCopy() to start copying elements from one array to another in Java. Even though both allow you to copy elements from source to destination array, the Arrays.copyOf() is much easier to use as it takes the just original array and the length of the new array. But, this means you cannot copy subarray using this method because you are not specifying to and from an index, but don't worry there is another method in the java.util.Arrays class to copy elements from one index to other in Java, the Arrays.copyOfRange() method. Both methods are overloaded to copy different types of array.

Java 8 Optional Example - isPresent(), OrElse() and get()

The Optional class in Java is one of many goodies we have got from the Java 8 release. If you use correctly, Optional can result in clean code and can also help you to avoid NullPointerException which has bothered Java developer from its inception. Even though many of us have used null to indicate the absence of something, the big problem is that if you call a method or access a field on the null object (except static fields), you will get a NullPointerException and your whole program may crash. The bigger problem is to find the faulty code or root cause because NullPointerException only indicates the line when you try to access field or variable from a null object but how does that null is get created on the code is unknown.

How to sort an Array in descending order in Java - Example

Sorting an array is one of the common tasks in Programming and you have many algorithms to sort an array like QuickSort, MergeSort which provides O(NLogN) time performance and Bucket Sort, Counting Sort and Radix Sort algorithms which can even sort some array in O(N) time. But, you hardly need to code these algorithms by hand when it comes to writing real code. The Programming language you will use already have tried and tested implementation for those algorithms and that's what you will learn in this article. In Java Programming language, it's easy to sort an array, you just need to call the Arrays.sort() method with a Comparator which can sort the array in the order you want but it highly depends upon which type of object is stored in the array.

Insertion Sort Algorithm in Java with Example

Insertion sort is another simple sorting algorithm like Bubble Sort. You may not have realized but you must have used Insertion sort in a lot of places in your life. One of the best examples of Insertion sort in real-world is, how you sort your hand in playing cards. You pick one card from the deck, you assume it's sorted, and then we insert subsequent card in their proper position. For example, if your first card is Jack, and the next card is Queen then you put the queen after Jack. Now if the next card is King, we put it after the queen, and if we get 9, we put it before jack. So if you look closely, Insertion sort is a perfect sorting algorithm to insert a new value into an already sorted array. That's why the best-case complexity of insertion sort is O(n), in which case you can just insert a new number in the already sorted list of integers.

7 Examples of HttpURLConnection in Java - Sending GET and POST Request

The HttpURLConnection is an important class in java.net package which allows you to send an HTTP request from Java program. By using this you can send any kind of HTTP request e.g. GET, POST, PUT, DELETE, HEAD etc. It also provides several methods to configure your HTTP requests e.g. it allows you to add headers, add request parameters, attaching cookie, configuring timeout, handling redirects and so on. A good knowledge of this class is important for Java developers working on both core Java and Java web application.

21 String Programming and Coding Interview Questions With Solutions

In this article, I am going to share 21 of the most common String based Programming and Coding interview questions from Java developer interviews. These questions require you to write code to solve the problem and they are different from traditional Java String questions like how substring method works in Java or when to use the intern() method of String in Java? Since coding and problem solving is an important part of any programming job interview, it's imperative that you know how to solve them in time and on pressure situation, which comes only after doing practice with a right set of questions. Since these questions are already tried and tested and appeared in many interviews, they will provide you the experience you need to crack your coding interview.