HTML

How to Reverse a 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 classes or methods like e.g. StringBuilder to solve this problem. This restriction is placed because StringBuilder and StringBuffer class define a reverse() method which can easily reverse the given String. Since the main objective of this question is to test the programming skill and coding 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? If you are familiar with array data structure then it would be easy for you. 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.

Difference between table scan, index scan, and index seek in SQL Server Database

Good understanding of Indexes is very important while working in a database and SQL and that's why you will find many questions based upon indexes on Programming Job interviews. One of such frequently asked SQL question is the real difference between table scan, index scan, an index seek? which one is faster and why? How does the database chooses which scan or seek to use? and How you can optimize the performance of your SQL SELECT queries by using this knowledge. In general, there are only two ways in which your query engine retrieves the data, using a table scan or by using an index. Which method is used for your particular query depends upon what indexes are available in that table, what columns you are requesting in your query, the kind of joins you are doing, and the size of your tables.

How to Format Date to String in Java 8 and Before - Example Tutorial

One of the common programming task in Java is to change the date format of a given Date or String. For example, you have something like "2017-01-18 20:10:00" and you want to convert it that date into "2017-01-18", or you want to convert from dd-MM-YY to MM-dd-YY or to any other format of your choice and need, but a valid date format as per Java specification. How will you do that? Well, it's not that difficult. It's just a two-step process. In the first step, you need to parse String to create an equivalent date using current format and then once you got the date, you need to again convert it back to String using the new format. The same process is repeated in both Java 8 and before, only corresponding API and classes changes.

Java 8 ConcurrentHashMap compute() and computeIfPresent() Example

The JDK 8 has added several useful methods in existing interfaces e.g. java.util.Map, java.util.Collection, and java.util.concurrent.ConcurrentMap. Thanks to default methods, the much-needed evolution of existing interfaces become possible. Out of many useful methods, one method which stands out to me is the compute() method, which allows you to update a value in ConcurrentHashMap atomically. As per Java documentation, The compute() function tries to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). The entire function is performed atomically.

Difference between ISNULL() and COALESCE() function in SQL

Even though both ISNULL() and COALESCE() function provides alternate values to NULL in T-SQL and Microsoft SQL Server e.g. replacing NULL values with empty String, there are some key differences between them, which is often the topic of SQL Server interview. In this article, you will not only learn the answer to this question but also learn how to use COALESCE and ISNULL function properly. One the main difference between them is that  COALESCE() is a standard SQL function but ISNULL() is a Microsoft SQL Server specific, which means it's not guaranteed to be supported by other database vendors like Oracle, MySQL or PostgreSQL.

How to Convert a List into Map in Java 8 - Example Tutorial

One of the common tasks in Java programming is to convert a list to map and I have written about this in the past and today we'll see how Java 8 makes this task easier. Btw, be it Java 7 or Java 8, you need to keep something in mind while converting a list to map because they are two different data structure. For example, the list allows duplicate elements but keys in a map must be unique, the value can be duplicated but a duplicate key may cause a problem in Java 8. Similarly, another concern is order. A list is an ordered collection but the map doesn't guarantee any order unless you decide to use LinkedHashMap, which keeps insertion order or TreeMap which keeps mapping in the sorted order of keys. This is one of the important details which many Java beginners forget and then spend hours to chase subtle bugs.

7 Examples to Sort An Array in Java

Sorting array is a day-to-day programming task for any software developer.  If you have come from Computer Science background then you have definitely learned fundamental sorting algorithms like the bubble sort, insertion sort, and quicksort but you don't really need to code those algorithms to sort an array in Java because Java has good support for sorting different types of array. You can use Arrays.sort() method to sort both primitive and object array in Java. But, before using this method, let's learn how the Arrays.sort() method works in Java. This will not only help you to get familiar with the API but also it's inner working.  This method sorts given array into ascending order, which is the numeric order for primitives and defined by compareTo() or compare() method for objects.

22 Array Concepts Interview Questions Answers in Java

An array is one of the fundamental data structure and most of other advanced data structures like list, hash tables are built using arrays.  Good knowledge of fundamental data structures like the array, linked list, a binary tree is not just essential for writing better code but also doing well on Programming Job interviews. Array-based questions are quite popular on Java interviews. There are two types of question you will find, first which are based upon array implementation in Java and second which are based upon how to use an array data structure to solve some coding problems. The first type of question is quite popular in the telephonic round of Java interview and second is usually asked on written test and face-to-face interviews.

Can you make an Abstract Class/Method Final in Java?

No, you cannot make an abstract class or method final in Java because the abstract and final are the mutual exclusive concept. An abstract class is incomplete and can only be instantiated by extending a concrete class and implementing all abstract methods, while a final class is considered as complete and cannot be extended further. This means when you make an abstract class final, it cannot be extended hence it cannot be used and that's why Java compiler throws a compile-time error when you try to make an abstract class final in Java. In short, an abstract class cannot be final in Java, using both abstract and final modifier with a class is illegal in Java.

Java - Convert String to Short Example

In last a couple of examples, I have taught you how to convert String to Integer, Long, Double, Float, Boolean, and Byte in Java and today I will show you how to convert String to Short in Java, but before that let's revise what is short data type in Java. The short is an integral data type similar to the int but it only takes 2 bytes to store data as compared to 4 bytes required by an int variable. Since it takes only 2 bytes or 16 bits to store data, the range of short is also shorter than int. It ranges from -32,768 to 32767 (inclusive) or -2^15 to 2^15 -1. You might be wondering why upper bound is 255 and lower bound is -256 but that's because we have included zero in between.

Linux Crontab Command Example - How to backup and load Cron Jobs from a File in UNIX

If you have been using Linux for some time then you might know about cron jobs. They are the scheduler which can be used to automatically start processes in a Linux box. I have worked on many projects which used cron jobs to start the Java process and environment daily or weekly basis. They are similar to Autosys when it comes to scheduling job but cron is a Linux command as opposed to Autosys which is separate application altogether. In Linux, crontab command is used for scheduling and automating jobs or process. You can also use it for loading cron jobs from a file, listing existing cron entries, and editing them. It manages the cron table that is used by the cron daemon in Linux to execute the cron jobs.

Top 20 Hibernate Interview Questions for Java J2EE Programmers

Hibernate is one of the most popular persistent frameworks in the Java world. Hibernate offers an object to relational (ORM) solution which frees Java developers from writing tedious, hard to read and cluttered JDBC code converting SQL columns into Object properties. Apart from freeing Java developer from writing JDBC and database interaction code, Hibernate also offers the out-of-box solution on caching, proxying and lazy loading which drastically improves the performance of your Java Web application. Given it's important in Java Web application development field Hibernate has become of one of the most sought-after skill and goes hand in hand with Spring framework. That's why Spring and Hibernate interview questions are also very popular in Java interviews, particularly for web development positions.

What is the use of DispatcherServlet in Spring MVC? Interview Question

Hello guys, In today's article we are going to discuss one interesting and important Spring MVC concepts, which is also a popular Spring Interview question. How DispatcherServlet works internally in Spring MVC or What is the role of DispatcherServlet in Spring MVC is some of the frequently asked Spring MVC Interview Questions. You might have seen them already during your previous Java web development interviews but if you have not, it's a good question to know. In this article, I'll answer these questions by explaining What is DispatcherServlet and its importance in Spring MVC. The DispatcherServlet is one of the important components of the Spring MVC web framework and acts as a Front Controller.

When and How to use the wait() and notify(), nofityAll() methods in Java? - Example Tutorial

When should you use the wait() and notify method in Java is one of the many popular questions about the wait and notify methods from Java multithreading interview questions. One of the reasons for its popularity is that still a lot of Java programmer struggle to explain and write code using wait-notify methods.  Many Java developer only knows some facts about the wait and notify methods like that wait() and notify() are defined in the java.lang.Object class or you cannot call wait() without synchronization, means without a synchronized block or synchronized method but doesn't really know when and how to use them.

Java 8 Stream findFirst() and filter() Example

Suppose, you have a list of prime numbers and you need to find the first prime number which is greater than a given number? How do you find it? Don't tell that you will loop through the list and check each element and return the first element which is greater than the given number. Well, that's right but that's not what you should do in Java 8. That's good for Java 7 or earlier version but Java 8 offers you many better alternatives and one of them is Stream. You can use the Stream class along with filter() and findFirst() methods to find out an element based upon a Predicate, a functional interface for defining a condition which returns a boolean.

Decorator Design Pattern in Java with Example

The Decorator design Pattern is one of the famous Gang of Four (GOF) structural design pattern, which provides a dynamic way of extending an object's functionality. It's different than the traditional way of adding new functionality into an object using Inheritance, instead, it uses Composition which makes it flexible and allows to add new functionalities at the run time, as opposite to Inheritance, which adds new functionality at compile time. Because of this flexibility, Decorator is one of the darling patterns for many Java developer. Btw, like many others object-oriented design patterns, the decorator design pattern is introduced by the famous Gang of Four design pattern book, almost 2 decades ago. This means it's a time-tested way of adding new functionalities into an object.

Top 35 Java String Interview Questions with Answers

The  String class and concept is a very important class in Java. There is not a single Java program out there which is not using String objects and that's why it's very important from interview point of view as well. In this article, I am going to share 35 String based questions from different Java interviews. It includes questions on java.lang.String class as well as a string as data structure e.g. some coding questions something like I have shared in my earlier article about 75 Coding questions to Crack any Programming Job interviews.  You can use this list to refresh your knowledge about String in Java as well as to prepare for your next Java interview. If you have any good String questions which are not on this list, feel free to drop a note and I'll include in this list.

10 Example of LocalDate, LocalTime, and LocalDateTime in Java 8

It's been almost 5 years since Java SE 8 was released on 17th March 2014 and Java 8 adoption has come a long way. Java programmers around the world have accepted with both ends, many companies have switched their development on Java 8 and several others are migrating to Java 8 platform. At this time, its extremely important for a Java developer to learn and share his Java 8 skill and that just not include lambdas, stream, method reference, functional interface, and other more glamorous stuff, you also need to learn the new Date and time API, which you would find using more regularly in your day to day life.

How to Convert Hostname to IP Address in Java - InetAddress Example

Hello guys, today, I am going to teach you about one interesting class from java.net package, the InetAddress. If you have never used this class before, let me tell you that it's an abstraction to represent an Internet address and it encapsulates both hostname and IP address. Since converting an IP address to hostname or hostname to IP address is a very common requirement, Java developer should know about this class, and, in general about java.net package, which contains much other useful class for networking and client-server applications. So, what is the best way to learn new APIs like java.net? Well, by writing small programs like this, which has a small focus and let you explore things. They are super useful and that's how I have learned the majority of Java API.

How to Find the Largest and Smallest of Three Numbers in Java

Hello guys, today, I am going to share with you another interesting coding problem for beginners, how to find the largest and smallest of three numbers. If you are learning how to program or looking to improve your problem-solving and coding skill then these kind of small problems are good to start with. They not only provide you an opportunity to think how to solve a problem using basic programming constructs like conditionals (if, else, if-else, switch), loops (for, while, do-while), operators like arithmetic, bitwise and logical operator as well as teach you how to write functions, class and create a program which you can run.

A Beginners Guide to Array in Java

Without any doubt, the array is one of the most used data structure in all programming languages, including Java. Pick up any programming language be it functional, object-oriented, imperative or even scripting languages like Python, Bash, and Perl, you will always find array. That's why it's important for any programmer to have a good understanding of the array data structure. Like any other data structure, the array also provides a way to organize and store objects, but the way it does makes all the difference. An array is used to store elements in the contiguous memory location and many C, C++ programmer can take advantage of a pointer to work with an array.

Why wait() and notify() method should be called inside a loop in Java?

If you have used wait() and notify() method in Java then you know that the standard idiom of calling wait() method uses a loop, but have you ever thought why? This is even advised by none other than Joshua Bloch, a Java guru and author of popular Effective Java book, must read for any Java programmer. When I first started using this method, I was puzzled why not just use the if block because ultimately we are testing for a condition and then either waiting or going for further processing. An if block is more readable for the testing condition than a while loop like for the classic producer-consumer problem, the waiting condition for producer thread could be written as :

How to Check if a Key Object Exists in HashMap Java? containsKey() Example

Hello guys, you might know that one of the common programming tasks while using HashMap in Java is to check if a given key exists in the map or not. This is supposed to be easy, right? Yes, it is easy if you know your API well, all you need to is call the containsKey() method, it returns true if given key exists in HashMap, otherwise false; but I have seen many programmers write code like below which is not correct, which inspired me to write this blog post.

if(map.get(key) !=  null){
    System.out.println("key exits in Map");
}

This code is fragile, it will not work if you have added null values into HashMap because HashMap does allow null values.

How to implement Radix Sort in Java - Algorithm Example

The Radix sort, like counting sort and bucket sort, is an integer based algorithm (I mean the values of the input array are assumed to be integers). Hence radix sort is among the fastest sorting algorithms around, in theory. It is also one of the few O(n) or linear time sorting algorithm along with the Bucket and Counting sort. The particular distinction for radix sort is that it creates a bucket for each cipher (i.e. digit); as such, similar to bucket sort, each bucket in radix sort must be a growable list that may admit different keys.

How to Convert/Print Array as String in Java with Example

Array and String are very closely related, not just because String is a character array in most of the programming language but also with popularity - they are two of the most important data structure for programmers. Many times we need to convert an array to String or create an array from String, but unfortunately, there is no direct way of doing this in Java. Though you can convert an array to String by simply calling their toString() method, you will not get any meaningful value.  If you convert an Integer array to String, you will get something like I@4fee225 due to the default implementation of toString() method from the java.lang.Object class. Here, I show the type of the array and content after @ is hash code value in hexadecimal.

How to convert Stream to List, Set, Map, and ConcurrentHashMap in Java 8 - Learn with Examples

In Java 8, Stream is one of the most important class as it allows a lot of useful functional operations e.g. filter, map, flatmap, etc on a collection of object. Hence, going forward converting a Collection to Stream, performing operations and then converting the result back to different Collection classes like List, Set, and Map will be a common task. The first problem of converting Stream to Collection is solved by Java designer by directly adding a stream() method on Collection interface, which means all other interfaces and classes which derived from Stream also have this method. Though the second problem of converting a Stream to List, Set, and Map requires some understanding of Stream API of JDK 8 and that's what you will learn in this article.