How to format JSON String in Java - Example Tutorial

You can format JSON String in Java using Jackson API's pretty print feature. As you might have noticed in the my previous JSON tutorials that the output of the programs is not properly formatted, which makes them hard to read, especially in large log files where there are so many other text, both JSON and normal text is there. That's why its advised to print JSON String properly formatted because then it will stand out in log file or console. Whenever we print JSON String from Java Programs by using method writeValueAsString(), it usually comes in one line, as shown in following example :

{"name":"Virat","sport":"cricket","age":25,"id":121,"lastScores":[77,72,23,57,54,36,74,17]}

This is not very readable as you cannot see how many attributes are there, what is their name and value, compare it to following formatted output which is printed using Jackson's pretty print feature:

{
  "name" : "Virat",
 "sport" : "cricket",
  "age" : 25,
  "id" : 121,
  "lastScores" : [ 77, 72, 23, 57, 54, 36, 74, 17 ]
}

Its way better than earlier output, its much more readable. You can easily identify which one is just simple name value pair, which one is JSON array and much more. Wondering, how to nicely print JSON String, just checkout the example shown in next section.

Top 10 Java Multithreading and Concurrency Best Practices

Writing concurrent code is hard and and testing correctness with concurrency is even harder. Though Java programming language provides lots of synchronization and concurrency support from language to API level, it's eventually comes to individual's diligent and expertise to write bug free Java concurrency code. These Java concurrency and multi-threading best practices are collection of some well known tips, which helps you to write better concurrency code in Java. Some of you, may be familiar with these tips, it's often worth to revise them in couple of years. These Java multi-threading and concurrency tips are from my own learning and usage, and also inspired by reading books like Effective Java and Java Concurrency in Practice in particular. I suggest reading Java Concurrency Practice two times to every Java developer, yes, you heard it correctly, TWO times. Concurrency is confusing and difficult to comprehend, much like Recursion to few programmers; and in one reading, you might not get all of it.

Sieve of Eratosthenes Algorithm to generate Prime numbers upto 100 in Java

There are many occasions when you need to generate all prime numbers upto a specified integer and one algorithm which is most often used to generate prime numbers is Sieve of Eratosthenes. Its an ancient greek algorithm to find all prime numbers upto a given number and named after the famous greek mathematician Eratosthenes. He was the first man to calculate the circumference of the earth and also known for working on calendars with leap years. A prime number is a whole number which is either divisible by 1 or itself e.g. 2, 3 and 5. You can see they are not divisible to any positive whole integer. In other words, prime number doesn't have a factor other than 1 or itself. You can use this algorithm to generate prime numbers from 1 to 100 or up-to any maximum value. In this tutorial, we will not only learn how Sieve of Eratosthenes algorithm works but also we will generate prime numbers using this algorithm and verify whether all number generated are actually prime or not.

Fixing Unsupported major.minor version 52.0 Error in Java

Unsupported major.minor version 52.0 comes when you are trying to run a class compiled using Java 1.8 compiler into a lower JRE version e.g. JRE 1.7 or JRE 1.6. Simplest way to fix this error is install the latest Java release i.e. Java 8 and run your program. If upgrading to Java 8 is not an option then make sure you use Java compiler's cross compilation feature and compile source code for lower Java version by using -target option of javac command. If you have multiple JRE installed make sure that the latest one comes first in PATH environment variable. You can get "java.lang.unsupportedclassversionerror unsupported major.minor version 52.0" while running a Java program explicitly using java command or running a Java Applet in browser or running a Java program from command line but compiled in Eclipse or any other build tool like Maven or ANT. Main reason of this error is that during compilation, you have used a higher version of JDK but during deployment, you have deployed into a lower version of JDK or JRE. Let's understand the cause and solution of Unsupported major.minor version 52.0 Error in little more detail.

TL:DR; upgrade to Java 8 or compile for lower JRE version using java -target 1.6 option.

How to differentiate between an Average and a Good Programmer?

When it comes to interview, goal is to find the most suitable developer for job to get done but its very difficult to judge someone's caliber, experience and expertise in short duration of Interview. There is always a process starting from phone interview, written tests to face-to-face interview, but its still difficult to hire the right programmer. Process can help you to filter candidates but eventually it will come down to your experience and gut feeling. As you take more and more interviews, you will know what to ask and what not and like many other interviewers in the world, you will develop some of your own tips. Similarly, I have developed couple of tips from my experience which has helped me to differentiate an average programmer with a good programmer in past. Today I would like to share one of such tips with you guys, to see if you agree with my observation and hopefully I will get couple of more tips to find right programmers. One of my most used tip is finding gaps in requirement. I have found and learnt it over time that good developers has knack of breaking requirements and finding gaps, which is very important to produce quality software and products. Though this skill comes with experience, a good developer even with less experience has this ability. In this article, I will share my hypothetical interview with two programmers and see what they produced.