Friday, January 22, 2016

9 difference between Array vs ArrayList in Java

Both array and ArrayList are two important data structure in Java and frequently used in Java programs. Even though ArrayList is internally backed by an array, knowing the difference between an array and an ArrayList in Java is critical for becoming a good Java developer. If you know the similarity and differences, you can judiciously decide when to use an array over an AraryList or vice-versa. In this article, I'll help you understand the difference between ArrayList and array in Java. If you are coming from C or C++ background then you already know that array is one of the most useful data structure in the programming world. It offers O(1) performance for index-based search and one of the fundamental way to store data.

Wednesday, January 20, 2016

How to declare and initialize a List (ArrayList and LinkedList) with values in Java

Initializing a list while declaring is very convenient for quick use, but unfortunately, Java doesn't provide any programming constructs e.g. collection literals, but there is a trick which allows you to declare and initialize a List at the same time. This trick is also known as initializing List with values. If you have been using Java programming language for quite some time then you must be familiar with syntax of array in Java and how to initialize an array in the same line while declaring it as shown below:

String[] oldValues = new String[] {"list" , "set" , "map"};

or even shorter :

String[] values = {"abc","bcd", "def"};

Similarly, we can also create List  and initialize it at the same line, popularly known as initializing List in one line example. Arrays.asList() is used for that purpose which returns a fixed size List backed by Array. By the way don’t confuse between Immutable or read only List which doesn’t allow any modification operation including set(index) which is permitted in fixed length List.Here is an example of creating and initializing List in one line:

Saturday, January 16, 2016

Solving java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Java Spring MVC Application

The  java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet error comes when you deploy a Spring MVC application into Tomcat or Jetty and Servlet container not able to found this class, which usually found in the spring-webmvc.jar file. The DispatcherServlet is the core of Spring MVC framework, it implements the FrontController pattern and intercept all HTTP requests sent to your Web application and subsequently route it to correct controller based upon URL mapping. You need to declare the DispatcherServlet in your web.xml and specify the URL pattern such that Servlet container like Tomcat should send all HTTP request to DispatcherServlet. We also set load-on-startup tag as 1 for this Servlet, so that it should be loaded at deployment time. This Servlet is the link between Servlet Container and Spring MVC framework.

Thursday, January 14, 2016

Why JPA Entity or Hibernate Persistence Class Should Not be Final?

One of the interesting hibernate interview questions is, why you should not make a Hibernate persistent class final? I'll try to answer this question in this short blog post. Use of proxies is the core feature of Hibernate (one of the most popular ORM framework for Java Projects) for implementing key performance features e.g. lazy loading and lazy associations fetching. In order to use a proxy in place of real class, your hibernate persistence class must be either non-final or the implementation of an interface that declares all of the public methods. Why? because you cannot extend a final class in Java, and to stand up as a proxy, proxy class must satisfy the IS-A relation, which comes either by extending a class using "extends", or implementing an interface using "implements".

Tuesday, January 5, 2016

How to check if String is not null and empty in Java?

In Java, since null and empty are two different concept, it's a little bit tricky for beginners to check if a String is both not null and not empty. A String reference variable points to null if it has not been initialized and an empty String is a String without any character or a String of zero length. Remember, a String with just whitespace may not be considered as empty String by one program but considered as empty String by others, so, depending upon your situation, you can include the logic to check for that as well. A String with just white space is also referred as blank String in java. In this tutorial, I will teach you a couple of right ways to test if a String is not null and not empty in Java.

Monday, January 4, 2016

How to become a better Java developer?

First of all wish you very happy new year guys. It's that time of year when we start afresh, make plans, set goals and make resolutions for the new year. Being a Java developer and author of a Java blog, I frequently receive a request from Java programmers from all over the world about how they can improve themselves? How they can become a better Java developer etc. In the past, I have shared my 10 tips to become a better programmer and every tip on that article still holds true, but given it's a new year. I have decided to jot down 5 new resolutions for Java programmers to improve themselves, enhance their knowledge on Java Programming and increase their value for future career growth. So without wasting anymore time, let's see what are 5 resolutions for Java Programmers in the new year, 2016.

Saturday, January 2, 2016

SQL EXISTS Example - Customers Who Never Order LeetCode Solution

Good morning Guys, Today we will learn how to use the EXISTS clause in SQL by picking up a nice SQL exercise from LeetCode. Suppose that a website contains two tables, the Customers table and the Orders table. Write an SQL query to find all customers who have never ordered anything. This problem can be solved from different way but it is also a very good example of how you can use the SQL EXISTS clause. If any customer has ordered anything then their CustomerId will exist in Order table, we will take advantage of this knowledge to solve this problem. This question is not as popular as the second largest salary query, but it is also one of the frequently asked SQL queries from programming interview, you can see the full list here.

Friday, January 1, 2016

java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer

You might have seen the java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer error while working with Java NIO based server which is using SocketChannel instead of InputStream for reading data from the network. In general, this can come at both client and server end of a client-server Java application which is using TCP/IP to connect each other. Though, this exception usually comes at the server end and client was complaining that they are not able to connect. From the error message, it's quite clear that before client or server could read the data from SocketChannel, another party has disconnected the session. Let's see the root cause of the problem and how to solve java.net.SocketException: Failed to read from SocketChannel: Connection reset by a peer in Java application.