Tuesday, January 31, 2017

Where and How to download Spring Framework JAR file (Spring 4.0 or Spring 3.2) without Maven, Gradle

One of the easiest and oldest ways to run a Java program which depends on an external library or framework is to download dependency JAR files, put it on the classpath and then run the program by creating a Main class with the main() method. This is simple but not as easy as you think, there are many challenges down the road e.g. you need to find the right version of JAR files and their dependencies e.g. Spring might have a dependency on other third party libraries like Log4j. So, when the build tool like Maven and Gradle comes, everybody stopped downloading the JAR file manually. All they do is specify the Maven dependency and Maven will download the JAR file of specified version along with their dependencies. This greatly simplified the development and testing of Java application which uses a framework like Spring and Hibernate.

Sunday, January 29, 2017

Best Books to Learn Servlet and JSP

Unfortunately, there are not many good books to learn Servlet and JSP for Java web developers. Despite being the most popular Java Web technology, there are very few good books to learn Servlet and JSP. The one book, which everybody will suggest you is the Head First Servlet and JSP, which is indeed one of the best books but it's outdated now. It doesn't cover the latest development in Servlet API, particularly Servlet 3.0 and also not been updated for last 7 to 8 years, but considering others, it is still a good book to learn Servlet and JSP. The main advantage of this book is Head first style which is great for beginners. With lots of repetition, bullet points, fireside chats, Head First book make sure you understand things and not just read them. Since many readers ask me to suggest good books learn Servlet and JSP, I'll share a couple of title in this book, which I think is relevant and help you to learn Servlet and JSP better.

Saturday, January 28, 2017

Difference between for loop and Enhanced for loop in Java

Though you can use both for loop and enhanced for loop to iterate over arrays and collections like a list, set, or map. There are some key differences between them. In general, enhanced for loop is much more easy to use and less error prone than for loop, where you need to manage the steps manually. At the same time, for loop is much more powerful because you get the opportunity to control over looping process. All the difference, you will learn in this article, stems from this very fact that traditional for loop gives more control that enhanced for loop but on the other hand enhanced or advanced for loop gives more convenience.

Thursday, January 26, 2017

How to prepare for Java Interviews? Topics, Books, and Questions

Java Interviews are a little bit different than traditional programming interviews on tech giants and product based companies like Google, Amazon, Microsoft, or Facebook. First, even though it has questions from Data Structure and Algorithms e.g. String or Array, you can still manage to clear Java interviews if you are not an expert on them. The questions are a little bit easier and more practical than those companies. Another very important thing about Java interviews are questions based upon Java programming language and JDK API. Since Java is also an Object oriented programming language, you will find lots of OOP questions there. Since Java is primarily used as application programming language, the focus is aligned accordingly with questions mostly focusing on API, Java concepts, and design patterns.

Wednesday, January 25, 2017

How to create LocalDateTime in Java 8 - Example

The LocalDateTime is a new class introduced in Java 8 new Date and Time API. This class is in java.time package and it represents both date and time information without timezone. In this example, you will learn different ways to create an instance of LocalDateTime class in Java 8 e.g. by using the static factory method, or by combining LocalDate and LocalTime instances together, which are subsequently used to denote date without time and time without the date in Java 8. As their name suggests they are local, so they don't contain timezone information.  They are always bound to local timezone i.e. the timezone of the machine on which your Java program is running. The class which contains the date, time and timezone information is known as ZonedDateTime in Java 8.

Monday, January 23, 2017

How to Split String based on delimiter in Java? Example Tutorial

You can use the split() method of String class from JDK to split a String based on a delimiter e.g. splitting a comma separated String on a comma, breaking a pipe delimited String on a pipe or splitting a pipe delimited String on a pipe. It's very similar to earlier examples where you have learned how to split String in Java. The only point which is important to remember is little bit knowledge of regular expression, especially when the delimiter is also a special character in regular expression e.g. pipe (|) or dot (.), as seen in how to split String by dot in Java. In those cases, you need to escape these characters e.g. instead of |, you need to pass \\| to the split method.

Sunday, January 22, 2017

Top 10 Excuses Programmers Gives to Avoid Unit Testing

Though everyone loves unit tests and everyone agree with benefits they bring in, when the time comes to write them, you will see a lot of excuses, even from some of the more experienced and senior developers. At the heart of the problem of not writing unit tests or enough unit tests they are two things, first is time pressure i.e. you don't have enough time to complete coding forget about writing unit tests. This problem comes due to erroneous estimation i.e. only estimating time for coding and not including unit testing as part of development. This is also our first excuse, so we'll see it in a short while. The second reason is laziness and ignorance i.e. you don't know to write unit tests but don't put enough effort to learn the tips and tricks.  In this article, I have shared some of the most common excuses given by programmers for not writing unit tests.

Saturday, January 21, 2017

How many characters is allowed on VARCHAR(n) columns in SQL Server? How much memory VARCHAR variable takes in disk?

One of the frequently ask SQL questions in any programming interviews is what is the difference between VARCHAR and CHAR data type in SQL? particularly when your project is using Microsoft SQL Server. You might have seen this couple of times, but given its popularity, nowadays people are asking this question differently e.g. they will ask you how much space a column of VARCHAR(2) data type will take? How many characters can it take? How do you minimize the space? Is it better to use a CHAR variable instead of VARCHAR(2) and why? These are some of the really interesting question and more meaningful than classical SQL questions like the difference between char vs varchar data type question.

Thursday, January 19, 2017

Can we make an Array or ArrayList volatile in Java?

This is one of the many interesting multi-threading questions I have shared in my post 50 multi-threading interview questions. Yes, you can make an array volatile in Java, there is no problem with that, neither compiler will flag any error not JVM will throw any exception but the tricky part is why you want to make an array volatile and what is the effect of making an array volatile in Java? In order to answer this question you must be familiar with both volatile modifier and Java memory model, otherwise, it would be difficult to answer, and that's why it's also one of the trick questions from Java interviews. Before answering this question in detail, let's first revise what is a volatile keyword in Java and what kind of guarantee it provides in the context of multithreading and concurrency.

Wednesday, January 18, 2017

String replaceAll() example - How to replace all characters and substring from String

You can replace all occurrence of a single character, or a substring of a given String in Java using the replaceAll() method of java.lang.String class. This method also allows you to specify the target substring using the regular expression, which means you can use this to remove all white space from String. The replaceAll() function is very useful, versatile and powerful method and as a Java developer, you must know about it. Even though Java provides separate methods for replacing characters and replacing substring, you can do all that just by using this single method. The replaceAll() method replaces each substring of this string (the String on which it is called) that matches the given regular expression with the given replacement. It internally uses classes like Pattern and Matcher from java.util.regex package for searching and replacing matching characters or substring.

Tuesday, January 17, 2017

Top 10 UNIX and Linux System Programming Books- Best of Lot, Must Read

I have been sharing programming books in this blog from a long time, starting with core Java, I moved to other related technology e.g. Spring, Design patterns, Data Structure and Algorithms etc. Today, I am going to share some of the best UNIX and Linux programming books. The list contains some of the must read UNIX books from great writers like Sir Richard. Stevens, Sir Brain Kernighan, who has contributed so much to the development of UNIX operating system. Most of the books in this list are timeless,  classics and don't age with time, much like UNIX, which is around from last 40 years and I am sure will be around for next 40 years, or maybe forever.

Sunday, January 15, 2017

5 Free OCEJWCD 6 Mock Exam 1Z0-899 Practice Test

The OCEJWCD is the Oracle's version of SCWCD (Sun Certified Web Component Developer) exam, which tests candidate's knowledge on Servlet, JSP, and other web technology. The OCEJWCD stands for Oracle Certified Enterprise Java Web Component Developer. Like other Java certifications e.g. OCAJP or OCPJP, the key to success is selecting a good book, practicing code daily, and then solving as many mock questions as possible. In the last article, I have shared some of the best OCEJWCD books and in this article, I am going to share of the best free OCEJWCD 6 mock tests you can online. These mock exams are good for self-assessment and practicing before real exams.

Saturday, January 14, 2017

How public static final variable works in Java?

How do public static final variable works in Java? what is the difference between a public static final variable and a public final (non-static) variable in Java? Does both are same? It is one of the simple, yet tricky questions which may Interviewers like to ask candidates, and would you believe that almost 50% Java developers miss the key point to mention here, which we'll see in this article. In short, No, they are not same. Even though both are final variables and you cannot change their value once assigned there is a very subtle difference between them. A public static final variable is a compile time constant, but a public final is just a final variable, i.e. you cannot reassign value to it but it's not a compile-time constant.

How to convert double to int in Java?

Suppose you have a double primitive variable 4.444 and you want to convert it to the integer value 4, how do you that in Java? Since double is bigger data type than int, you can simply downcast double to int in Java. double is 64-bit primitive value and when you cast it to 32-bit integer, anything after the decimal point is lost. Btw, type casting doesn't do any rounding or flooring, which means if you have 9.999999 and while casting to int you are expecting 10 then you would be disappointed, casting will give you just 9. If you need 10 then you need to use Math.round() method to first round the double value to the nearest integer and then truncate decimals.

Thursday, January 12, 2017

Difference between OCPJP 8 Upgrade Exams 1Z0-813 and 1Z0-810? Which one to take?

Many Java developers who holds a Java certification either from Sun or Oracle and who is looking to upgrade to latest Java version i.e. Java SE 8 certification has a common doubt, should they go for 1Z0-810 or 1Z0-813 upgrade exams? What are the different between these two exams? Well, even though they are both upgrade to Java SE 8 exam, they are separate exams and have a different exam code, the difference is also obvious when you look at their title. The 1Z0-810 exam is known as "Upgrade Java SE 7 to Java SE 8 OCP Programme" while the 1Z0-813 exam is known as "Upgrade to Java SE 8 OCP ( Java SE 6 and all prior versions)".

A Better way to write SQL queries

Writing code is art and same applies to SQL queries. The way you structure your query, the way you write it goes a long way to communicate your intent to the fellow developer. When I see SQL queries on emails from multiple developers, I can see the stark difference in their writing style. Some developers write it so neatly and indent their query properly, which makes it easy to spot the key details e.g. which columns you are extracting from which table and what are conditions. Since in real life projects, SQL queries are hardly one-liner, learning the right way to write SQL query makes a lot of difference when you read it yourself later or you share that query to someone for review or use.

Tuesday, January 10, 2017

12 Essential Apache Web Server Interview Questions Answers - Java & Linux

The Apache HTTP Server, also known as httpd, has been the most popular web server on the Internet since April of 1996. It is both free and robust and that's the reason that almost half of the world's websites  runs on Apache HTTP web server, ranging from small hobby websites to huge e-commerce giants and big banks. There are also many companies which use Apache and Tomcat to host Java web application. In a typical setup, Apache web server receives the HTTP request and depending upon the URL it either route to Tomcat or serve the static files directly from the file system. These questions are based mostly on that experience. Though Apache is not just famous in Java world it is even more popular with other web technologies e.g. Perl, PHP, and MySQL. It's one of the powerful and secure web servers so anyone can use it to handle HTTP traffic. It supports both HTTP and HTTPS. It's worth noting that Apache is also an HTTP/1.1 compliant web server.

Monday, January 9, 2017

Bucket Sort in Java with Example

In recent years, one of the question I have increasingly seen in programming job interviews is about constant time sorting algorithms e.g.  do you know any O(n) sorting algorithm? When I first encountered this question, I had no idea whether we can sort in constant time because even some of the fastest sorting algorithms e.g. QuickSort or MergeSort takes O(N log N) time for sorting. After some research, I come to know that there are some constant time algorithms e.g. bucket sort, counting sort and radix sort, which can sort an array in O(n) time.  The idea of bucket sort is quite simple, you distributes the elements of an array into a number of buckets and then sort those individual bucket by a differnet sorting algorithm or by recursively applying the bucket sorting algorithm.

Sunday, January 8, 2017

3 Ways to Convert Java 8 Stream to an Array - Lambda Expression and Constructor Reference Example

One of the frequently asked Java 8 questions is, how you do convert a Java 8 Stream to an array? For example, you have a Stream of Strings and you want an array of String so that you can pass this to a legacy method which expects an array, how do you do that? Well, the obvious place to search is the Javadoc of java.util.stream.Stream class and there you will find a toArray() method. Though this method will convert the Stream to an array it has a problem, it returns an Object array. What will you do, if you need a String array? Well, you can use the overloaded version of toArray(IntFunction generator), which expect a generator function to create an array of specified type. You can pass a lambda expression or constructor reference to this method to specify the type of array you want. This will return you an array of T i.e. if String contains String then it will return String array.

Saturday, January 7, 2017

Difference between extends and implements keywords in Java

Though both extends and implements keyword in Java is used to implement Inheritance concept of Object-Oriented programming, there is a subtle difference between them. The extends keyword is mainly used to extend a class i.e. to create a subclass in Java, while implements keyword is used to implement an interface in Java. The extends keyword can also be used by an interface for extending another interface. In order to better understand the difference between extends and implements, you also need to learn and understand the difference between class and interface in Java. Though both are an integral part of application development using object oriented methodology, an interface is more abstract than class hence it is used to define API or contract.

Thursday, January 5, 2017

Running SQL Queries on Production/live Databases - 5 Things to Remember

Did you ever face a situation where some of your innocuous looking action has cause production issue and that too big time? Well, I hope you have not because it's certainly, not a pleasant experience. One of such innocuous looking action is running SQL queries on production databases. I had that in past very early in my career where I removed some config as duplicates only to find after a week that it stopped publishing messages to one of the downstream. When you work in complex systems which has so many components, millions of lines of code, thousands of configuration and many databases with hundreds of tables, you have to be really careful with anything you do. Often there is on the real way to perform a production-like testing, hence the best bet is to keep your change as much isolated and limited as possible.

Wednesday, January 4, 2017

"The system cannot find the path specified." Error in Command Prompt

Recently I was trying to run the Apache Tomcat server from the command prompt to test my Java web application on localhost, only to find the "The system cannot find the path specified" error in command prompt. I haven't seen this error before so I was wondering whether tomcat is throwing or something is wrong with my system's PATH environment variable.  In order to start the tomcat server, I was running the catalina.bat file as tomcat/bin/catalina.bat start and it wasn't starting the Tomcat at all, instead, it was keep throwing "The system cannot find the path specified." error as shown below:

Tuesday, January 3, 2017

How to check for NULL values in SQL Query? The Right way Example

One of the most common SQL Interview question on Programming interview is to select some rows from a table which also contains null values. Since many SQL developers are used of using = and != operator on WHERE clause, they often tend to forget the fact that column allows NULL or not. Using = or != is perfectly fine if your column has NOT NULL constraint and you know for sure that there are no NULL values in that column, but if does contains NULLs then your query will return the incorrect result at times. This is one of the most common mistakes but at the same time hard to find SQL bugs if it managed to get into the real environment. In this article, you will learn the right way to check NULL values in SQL query using IS NULL and IS NOT NULL predicates.

Sunday, January 1, 2017

What is the real use of Method Overloading in Java or Programming?

Many programmers, including Java and C++, knows about overloading e.g. method overloading or function overloading, but if you ask them why you should overload a method? Many of them become clueless. This is a common problem of half learning i.e. you know the concept but you don't know the application. If you neither know what problem it solves nor what benefit it provides, then just knowing the concept is not good enough. You won't be able to reap all benefit if you just know the concept and never use it in practice. The most important benefit overloading provides is a cleaner and intuitive API.