Friday 30 September 2016

3 Ways to Solve "No JVM installation found. Please install a 64-bit JDK" - Android Studio

You are an enthusiastic Java programmer who just learned about developing Android apps in Java. To start with you have installed Android Studio, which is the official Integrated Development Environment (IDE) for Android app development, based on IntelliJ IDEA, but as soon as you click on the Android Studio Desktop Icon to start Android Studio you are getting "No JVM installation found. Please install a 64-bit JDK" error? How do you solve this problem? Well, even though the error message is same, every problem is different depending upon your machine, the Java version, whether your desktop or laptop has 32-bit or 64-bit OS e.g. Windows 8.1 or Windows 10. Whether you have installed 32-bit Java or 64-bit JDK e.g. JDK 1.8.0 and what is the value of JAVA_HOME environment variable or whether you have defined it or not.
Read more »

Thursday 29 September 2016

How to Serialize Object in Java - Serialization Example

Serialization is one of the important but confusing concept in Java. Even experienced Java developer struggle to implement Serialization correctly. The Serialiation mechamism is provided by Java to save and restore state of an object programatically. Java provides two classes Serializable and Externalizable in java.io package to facilitate this process, both are marker interface i.e. an interface without any methods. Serializing an Object in Java means converting into a wire format so that you can either persists its state in a file locally or transfer it to another client via the network, hence it become an extrememly important concept in distributed applications running across several JVMs. There are other features in Java e.g. Remote Method Invocation (RMI) or HttpSession in Servlet API which mandates the participating object should impelment Serializable interface because they may be transffered and saved across the network.
Read more »

Wednesday 28 September 2016

Iterative QuickSort Example in Java - without Recursion

The quicksort algorithm is one of the important sorting algorithms. Similar to merge sort, quicksort also uses divide-and-conquer hence it's easy to implement quicksort algorithm using recursion in Java, but it's slightly more difficult to write an iterative version of quicksort. That's why Interviewers are now asking to implement QuickSort without using recursion. The interview will start with something like writing a program to sort an array using quicksort algorithm in Java and most likely you will come up with a recursive implementation of quicksort as shown here. As a  follow-up, Interviewer will now ask you to code the same algorithm using Iteration.
Read more »

Tuesday 27 September 2016

Top 5 JSON Library Java JEE Developers Should Know

The JSON format is one of the most popular formats to transfer and exchange data in web. Almost all RESTful web services take JSON input and provide JSON output but unfortunately JDK doesn't have built-in support for one of the most common web standard like JSON. As a Java developer if you want to develop RESTful web service and produce JSON data or if you are developing a client to an existing RESTFul web services and want to consume JSON response, you don't need to be disappointed. Fortunately, there are so many open source libraries and API available for creating, parsing and processing JSON response in Java e.g. Jackson, Google GSon, json-simple etc.
Read more »

Monday 26 September 2016

How to get just DATE or TIME from GETDATE() in SQL Sever

The GETDATE is one of the most popular built-in methods of  Microsoft SQL Server, but unlike its name suggest, it doesn't return just date, instead it returns date with time information e.g. 2015-07-31 15:42:54.470 , quite similar to our own java.util.Date from Java world. If you want just date like 2015-07-31, or just time like 15:42:54.470 then you need to either CAST or CONVERT output of GETDATE function into DATE or TIME data type. From SQL Server 2008 onward, apart from DATETIME, which is used to store both date and time, You also have a DATE data type to store date without time e.g. 2015-07-31, and a TIME data type to store time without any date information e.g. 15:42:54.470. Since GETDATE() function return a DATETIME value, You have to use either CAST or CONVERT method to convert a DATETIME value to DATE or TIME in SQL Server.
Read more »

Sunday 25 September 2016

10 basic differences between Java and Groovy Programming

If you are working in Java, you might have heard about Scala, Groovy, and Closure. Out of these three Groovy seems to be gaining a place in Java projects more rapidly than others. Groovy is a Scripting language but runs on Java virtual machine. Every Java program can run on Groovy platform. As per official Groovy website, "Apache Groovy is a powerful, optionally typed and dynamic language, with static typing and static compilation capabilities, for the Java platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax". I think, they have highlighted the key capabilities of Groovy very well in that sentence. It basically further reduce the burden from Java developer with respect to coding. Now, you can put more focus on business logic and get your work done quickly without wasting time on writing more code.
Read more »

Saturday 24 September 2016

How to Remove CTRL-M characters From a File in UNIX and Linux

I wanted to transfer some files from Windows to Unix using FileZilla, but the problem arises when these files are transferred (Ascii or Binary mode both) and opened using VI we get ^M characters, also known as CTRL-M characters. I searched about this but the solutions were to remove these ^M characters when files are transferred using utilities. Is there any way that these ^M characters do not appear in the first place? Well, my search continues but I will share the solution which worked for me for removing control M characters i.e. CTRL-M or ^M characters. There are several UNIX commands e.g. dos2unix which can be used to convert a Windows or DOS generated files to UNIX one. You can also use sed command (stream editor) to remove CTRL-M characters without opening the file, very useful if you are removing CTRL-M characters from a large file. Alternatively, you can use VI command to open the file and replace ^M characters with nothing.
Read more »

Friday 23 September 2016

Top 21 Java Final Modifier (Keyword) Interview Questions and Answers

The final modifier is one of the important keywords in Java. You can use this with a class, method, and variable as well. A good knowledge of final keyword is not only essential for writing good, performant and secure Java programs but also to clear any  Java interview. Interviewer pays special attention to final keyword and expects candidates knows how and when to use the final modifier with variables, methods, and classes. In this article, I have collected Java interview questions which are based upon the concept of final modifier. You might have seen many of these questions on both telephonic and face-to-face round. You can use this collection not only to check your knowledge about final keyword but also to prepare for final modifier from Java interview's perspective.  If you have come across any interesting final modifier question which is not in this list, please share with us as a comment and I'll add into this list.
Read more »

Thursday 22 September 2016

3 Maven Eclipse Tips for Java Developers

If you are using Maven inside Eclipse IDE via M2Eclipse plugin then following tips can help you a lot.

1) Setup Dependency as Java Project in Eclipse
If your POM dependencies is another project in Eclipse then your project will automatically get updated whenever you build the dependent project. For example, let's say you have a project, ABC, which is dependent on two core modules e.g. framework.jar and persistence.jar, if you have checked out these project and has set up them as Maven Eclipse project, M2Eclipse can directly add them as dependency, instead of loading their JAR files from repository, be it local or remote.
Read more »

Wednesday 21 September 2016

How to Print all Leaf Nodes of Binary tree in Java - Recursion and Stack

Binary tree based questions are very common in Java or any other Programming job interviews. One of the frequently asked binary tree questions is "write a program to print all leaf nodes of a binary tree". In order to solve this problem, you must know what is a leaf node? A leaf node in a binary tree is a node whose left and right child is null. They are actually the last nodes of any binary tree. In a typical programming interview, you would be given a binary tree and asked to write a program to print all leaf nodes. Usually, all binary tree related questions can be solved easily using recursion because a tree is a recursive data structure, but you should also know how to solve them without recursion.
Read more »

Sunday 11 September 2016

2 Reasons of org.springframework.beans.factory.BeanCreationException: Error creating bean with name [Solution]

The Spring framework is one of the most popular frameworks for developing Java application. Apart from many goodies, it also provides a DI and IOC container which initialize objects and their dependencies and assemble them together. The Java classes created and maintained by Spring are called Spring bean. At the startup, when Spring framework initializes system by creating objects and their dependencies depending upon @Autowired annotation or spring configuration XML file, it throws "org.springframework.beans.factory.BeanCreationException: Error creating a bean with name X" error if it is not able to instantiate a particular Spring bean. There could be numerous reasons why Spring could not able to create a bean with name X, but clue always lies on the detailed stack trace. This error always has some underlying cause e.g. a ClassNotFoundException or a NoClassDefFoundError, which potentially signal a missing JAR file in the classpath.
Read more »

Friday 9 September 2016

Best Book to Learn Java for C and C++ Programmer?

In the last 2 decade many Java programmers started programming with C and C++ but the situation is changed now, you have more choices e.g. you can choose Python or you can even start with Java. It seems Academia is preferring Python in USA and Java in India, but there are still many programmers who know C and C++ and wants to learn Java for one or other reasons. I often receive emails from my readers about book recommendation and recently a couple of them asked me to suggest best Java books for C and C++ programmer. I can relate those beginners to myself because I have also gone through the same phase but those days the university text books are our only source to learn new thing. We didn't have broadband and unlimited access to Internet and eBooks was not popular at that time but things have changed. The good thing is now I know a couple of really good books which can help to utilize most of his C and C++ knowledge to learn Java e.g. Core Java Volume 1 - Fundamentals by Cay S. Horstmann which provides comparative analysis with C++ time to time.
Read more »

Thursday 8 September 2016

Why use Spaces over Tabs for Indentation in Code Editors - Eclipse

When I started coding in Eclipse, I was not aware that by default Eclipse uses tabs for indentation and tabs can have varied with e.g. 1 tab can be equal to 2 spaces or 4 spaces or even 8 spaces. Sometimes, It's all up to you how you configure tabs in your code editor and other times just the mercy of the tool you don't know how to configure e.g. VI in UNIX. I only realize the problem when I found too many difference in a file while check-in into SVN. Apparently, other people was using different indentation (spaces) and that's why the file was showing so many differences when I reformatted them in Eclipse. This happens to many programmers, some pay attention, some didn't and go ahead with the check-in the code, only to revert it back later. There is no clear guideline upon which one is better and whether a programmer should use tabs or spaces, even the Clean Code doesn't  help here.
Read more »

Tuesday 6 September 2016

Java Comparable Interface and compareTo() Example

Hello, guys, today I am going to talk about one of the fundamental concepts in Java, defining the natural ordering of the objects e.g. lexicographical order for String and numerical order for Number classes e.g. Integer, Double, Float etc. The compareTo() method is defined in the java.lang.Comparable interface. It is used to define the natural ordering of an object e.g. String class override compareTo() to define the lexicographical order for String objects. Integer class override compareTo() to define the numeric ordering of integer objects. Similarly, you can override compareTo() method by implementing Comparable interface to define the natural order on which you want to order your object. For example, an Order should be ordered by order id hence compareTo() must provide logic for that.
Read more »

Saturday 3 September 2016

How to solve java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test

The error "java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test" occurs when you try to connect MySQL database running on your localhost, listening on port 3306 port from Java program but either you don't have MySQL JDBC driver in your classpath or driver is not registered before calling the getConnection() method. Since JDBC API is part of JDK itself, when you write a Java program to connect any database like MySQL, SQL Server or Oracle, everything compiles fine, as you only use classes from JDK but at runtime, when the JDBC driver which is required to connect to database is not available, JDBC API either throws this error or "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver".
Read more »