3 Difference between Web Server vs Application vs Servlet Containers in Java JEE

In the Java EE, or J2EE or JEE world, there are a lot of confusion between terminology, which is quite evident that now we have three words (J2EE, Java EE, and JEE) to describe the same technology platform. You will see Java developers using the terms like Web Server, Application Server, and Web containers interchangeably but they are not exactly same. The main difference between Web server and application server is that web server is meant to serve static pages e.g. HTML and CSS, while Application Server is responsible for generating dynamic content by executing server side code e.g. JSP, Servlet or EJB. One of the most popular web servers is Apache HTTPD which is often used in conjunction with Tomcat to host many Java web application. Now, tomcat is not exactly an application server, it's more of a servlet engine or web container or also known as servlet containers because it provides the runtime environment for Servlet and JSP but doesn't provide the services like EJB and distributed transaction which are a key feature of the application server in Java JEE world.

How to set JAVA_HOME and PATH in Linux

The JAVA_HOME environment variable points to the JDK installation directory and used by many Java tools and applications like Eclipse, Maven, ANT, Tomcat etc. to figure out Java executables as they need Java for running. Unfortunately, When you install Java on Linux or Windows, this environment variable is not set automatically. You need to do it yourself. Java developers like me always set useful environment variables like JAVA_HOME, PATH, and CLASSPATH on the login scripts executed by the shell when you log in to Linux e.g. bash_profile.sh, but you can also set JAVA_HOME into /etc/profile for all users. I'll show you steps how to find the location of Java and how to setup the JAVA_HOME in Linux in this article and these steps should also work with any Linux or Java version e.g. Ubuntu, RedHat, CentOS, SUSE, ArchLinux, Debian, Fedora etc.

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.

How to count a number of words in given String in Java?

Can you write a method in Java which accepts a String argument and returns a number of words in it? A word is a sequence of one or more non-space character i.e. any character other than '' (empty String). This should be your method signature:

public int count(String word);

This method should return 1 if the input is "Java" and return 3 if the input is "Java, C++, Python". Similarly a call to wordCount("    ") should return 0.  This is one of the several String algorithmic questions you can expect in a programming job interview.  This is used to test the coding skills of the candidate and that's why it's very important to prepare for these questions anytime you go for an interview.

java.sql.SQLException: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [Solution]

This error comes when you are trying to connect to MySQL database from Java program using JDBC but either the JDBC driver for MySQL is not available in the classpath or it is not registered prior to calling the DriverManager.getConnection() method. In order to get the connection to the database, you must first register the driver using Class.forName() method. You should call this method with the correct the name of the JDBC driver "com.mysql.jdbc.Driver" and this will both load and register the driver with JDBC. The type 4 JDBC driver for MySQL is bundled into MySQL connector JAR e.g. mysql-connector-java-5.1.18-bin.jar depending upon which version of MySQL database you are connecting. Make sure this JAR is available in classpath before running your Java program, otherwise Class.forName() will not be able to find and load the class and throw java.lang.ClassNotFoundException: com.mysql.jdbc.Driver, another dreaded JDBC error, which we have seen in the earlier post.

Difference between final vs finally and finalize in Java? Answer

The final, finally, and finalize are one of the three confusing keywords, modifiers and methods in Java. They sounds similar but they are for different purpose. For example, final keyword is a modifier in Java. When you use final keyword with a class it becomes a final class and no one can extend this e.g. String is final in Java. When you use the final modifier with method than it cannot be overridden in subclass, and when you use the final keyword with variable, it become a constant i.e. its value cannot be changed once assigned. On the other hand, finally is a keyword related to exception handling in Java. It's often used with try block and it's part of try, catch and finally trio. A finally block can be used with or without catch block in Java and its guaranteed to be always executed, irrespective of what happens inside try block. This is the reason finally block is used to do cleanup and free resources.

Unsupported major.minor version 52.0 in Java + Eclipse + Linux [Solution]

The "unsupported major.minor version 52.0" error started to come after Java SE 8 release and the root cause of this error is trying to run a Java application compiled with JDK 8 into a JRE lower than Java SE 8 e.g. JRE 7 or JRE 6. This is very common because a developer has updated their compiler or IDE to Java SE 8 but many times their runtime is not upgraded to Java 8. If you remember, in Java you can run a class file compiled with a lower version say Java 6 to a higher version say JRE 8 because Java is backward compatible but vice-versa is not allowed. This make sense because Java SE 8 has features like lambda expressions, method reference, functional interface and new Date and Time API, which lower version JRE has no information. Depending upon where you getting this error e.g. Eclipse, NetBeans, IntelliJ IDEA or Android Studio, the solution could be different. All these IDEs has different settings for JRE but the bottom line is same, you need to configure these IDE to use JRE 8 to run the Java program compiled using Java 8.

5 Difference between BufferedReader and Scanner class in Java - Example

Even though both BufferedReader and Scanner can read a file or user input from command prompt in Java, there some significant differences between them. One of the main difference between BufferedReader and Scanner class is that former is meant to just read String while later is meant to both read and parse text data into Java primitive type e.g. int, short, float, double, and long. In other words, BufferedRedaer can only read String but Scanner can read both String and other data types like int, float, long, double, float etc. This functional difference drives several other differences on their usage. Another difference is Scanner is newer than BufferedReader, only introduced in Java 5, while BufferedReader is present in Java from JDK 1.1 version. This means, you have access to BufferedReader in almost all JDK version mainly Java 1.4 but Scanner is only available after Java 5.  This is also a popular core Java questions from interviews. Since many developer lack Java IO skill, questions like this test their knowledge about API and how to do some practical task.

12 Must Read Advance Java Books for Intermediate Programmers - Part 1

I often receive loads of email about Java book recommendations, something like, I have 2 years of experience, which Java book I should read to become an expert Java programmer, or I have 5 years of experience and want to become a Java expert, which books should I refer? It's interesting that most of the email I receive are not from beginners but from Java developers who has 2 to 3 years of experience. I call them Intermediate Java programmers, because they are in the state of their career where they know how to program in Java but they are not expert yet. They don't know how to write scalable, concurrent and robust code using Java concurrency features, they are learning design patterns but they are not yet using them in real code. They lack unit testing skills and they also don't have design skill a key for expert and senior Java developer. Keeping those things in mind, In this three part series of Java book recommendation article, I'll share 10 to 12 books to develop skills which both an intermediate and advanced Java developer should have.

4 ways of Session management in Servlet Java

Session tracking or Session management is an important feature of modern web applications which allows the server to remember it's clients. By keeping a session for each user, Server can serve the client better. It also helps in safety, security and personalization and must for certain kind of web applications e.g. e-commerce sites like Amazon or e-bay which stores item selected by the user for purchase in a shopping cart, even after the user is logged out. Since HTTP is a stateless protocol, there are no ways to know that two HTTP requests are related to each other i.e. they are coming from the same client or they are part of the same process. Session tracking is a mechanism that Servlets and Java Web application use to maintain state about a series of request from the same user across some period of time. By keeping a session, an e-commerce site can maintain add to card facility and also keep tracks of how you interact with the application. Since HTTP doesn't provide a default way to track Session, there are some non-standard ways to manage Sessions in Servlet JSP based application. Let's have a close look on them.

How to print Floyd's triangle in Java - Example Tutorial

In the last article, I have taught you how to print Pascal's triangle and in today's article I'll teach you how to print Floyd's triangle in Java program. Floyd's triangle is easier to print than Pascal's triangle because you don't need to take care of formatting the numbers as Floyd's triangle is a right angle triangle. It is named after American computer scientist Robert Floyd, who has also contributed Floyd–Warshall algorithm, which efficiently finds all shortest paths in a graph and Floyd's cycle-finding algorithm for detecting cycles in a sequence. If you remember, we use this algorithm to find the cycles in linked list. Coming back to Floyd's triangle, it is a right angle triangle which consists natural numbers starting from 1 in the first row. It then goes on with two numbers in second row, 3 numbers in 3rd row and so on. Along with other pattern based exercises and Pascal's triangle, Floyd's triangle is also a good programming exercise and often used in programming and training courses to teach how to program to beginners. It's one of the easier program but help you to build code sense and how to use basic programming constructs e.g. loop, operators and functions.

How to print Pascal Triangle in Java - Example Tutorial

Printing patterns with stars or numbers and triangles are some of the the common programming exercises. Earlier we have seen how to print pyramid pattern with stars and today you will learn how to print Pascal's triangle in Java. Sometime this problem is also asked as "write a program to print Pascal triangle without using array" or by just using for loop. Pascal’s triangle is a set of numbers arranged in the form of a triangle, similar to Floyd's triangle but their shape is different. Each number in the Pascal triangle row is the sum of the left number and right number of the previous row. If a number is missing in the above row,  it is assumed to be 0. The first row starts with number 1, that's why you will see that first two row of Pascal triangle just contain 1.