Monday, December 22, 2014

How to print Floyd's Triangle in Java with Example

There are lots of programming exercise in Java, which involves printing a particular pattern in console, one of them is printing Floyd triangle in console. In Floyd triangle there are n integers in the nth row and a total of (n(n+1))/2 integers in n rows. This is one of the most simple pattern to print but helpful in learning how to create other more complex patterns. Key to develop pattern is using nested loops and methods like System.out.print() and println() appropriately. Actually pattern based programming task are originally designed to master loops in programming. A good programmer should be able to look a pattern and break into nested loops. A more formal definition of Floyd's triangle : "It's a right angled triangle of array of natural numbers, which is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, stating with 1 in the top left corner".

It looks like following pattern :
2  3
4  5  6
7  8  9  10
11  12  13  14  15

Your task is to print this table using nested loops in Java. Main point is to build the logic by yourself, this will help you a lot in the long run.

Wednesday, December 10, 2014

9 Things about Null in Java

Java and null are uniquely bonded. There is hardly a Java programmer, who is not troubled by null pointer exception, it is the most infamous fact about. Even inventor of null concept has called it his billion dollar mistake, then why Java kept it? Null was there from long time and I believe Java designer knows that null creates more problem than it solves, but still they went with it. It surprise me even more because Java's design philosophy was to simplify things, that's why they didn't bothered with pointers, operator overloading and multiple inheritance of implementation, they why null. Well I really don't know the answer of that question, what I know is that, doesn't matter how much null is criticized by Java developers and open source community, we have to live with that. Instead of ruing about null it's better to learn more about it and make sure we use it correct. Why you should learn about null in Java? because If you don't pay attention to null, Java will make sure that you will suffer from dreaded java.lang.NullPointerException and you will learn your lesson hard way. Robust programming is an art and your team, customer and user will appreciate that. In my experience, one of the main reasons of NullPointerException are not enough knowledge about null in Java. Many of you already familiar with null but for those, who are not, can learn some old and new things about null keyword. Let's revisit or learn some important things about null in Java.

Friday, December 5, 2014

How to do static import in Eclipse - Java

Do you know what is shortcut of doing static import in Eclipse? Well I didn't know before, but today I come to know that shortcut Ctrl+Shift+M (Source > Add Import) can not only be used to add missing imports but It can also help with static import in Java program.  Suppose you are using lots of static variable from a utility class e.g. TimeUnit by referring them with class name, just like we refer static variable. In Eclipse IDE, you can select the whole reference variable and press Ctrl+Shift+M and it will automatically import that static element using static import in Java.
For example, if you have following code in your class, you can select TimeUnit.SECONDS and then use shortcut Ctrl+Shift+M to statically import SECONDS variable in your program, as shown in first and second screenshot.

Wednesday, December 3, 2014

Where is Java used in Real World?

If you are a beginner and just started learning Java, you might be thinking where exactly Java is used? You don't see many games written in Java except Minecraft, desktop tools like Adobe Acrobat, Microsoft Office are not written in Java, neither is your operating systems like Linux or Windows, so where exactly people use Java? Does it have any real-world application or not? Well, you are not alone, many programmers ask this question before starting with Java, or after picking Java is one of the programming language of choice at the graduate level. By the way, you can get a clue of where Java is used by installing Java at your desktop, Oracle says more than 3 billion devices run Java, that's a huge number, isn't it? Most major companies use Java in one way or other. Many server-side applications are written in Java to process tens of millions of requests per day, high-frequency trading applications are also written in Java e.g. LMAX trading applications, which is built over their path-breaking inter-thread communication library, Disruptor. In this article, we will see more precisely, what kind of projects are done in Java, which domain or sector Java is dominating and where exactly Java is used in real-world?

Monday, December 1, 2014

How to Read JSON String in Java - Json-Simple Example

JSON is a text format is a widely used as data-interchange language because its parsing and its generation is easy for programs. It is slowly replacing XML as most powerful data interchange format, as it is lightweight, consumes less bandwidth and also platform independent.  Though Java doesn't have built in support for parsing JSON files and objects, there are lot of good open source JSON libraries are available which can help you to read and write JSON object to file and URL. Two of the most popular JSON parsing libraries are Jackson and Gson. They are matured, rich and stable. Though there are couple of more libraries there like JSON simple, which we are going to use in this example. Between Jackson and Gson, later does very nice job in mapping JSON object and serialization. JSON is also used in request and response between client server communication. In this tutorial we are going to see how to read and write JSON to file using JSON.Simple library, and you will notice yourself how simple working with JSON is.

Since we don't have JSON support in JDK, we need to download this open source library. If you are using maven to download JAR and managing dependency, if not then you should, then you can just include following dependencies in your pom.xml file :

    <artifactid> json-simple</artifactid>

Otherwise, you have to add the newest version of json-simple-1.1.1.jar in CLASSPATH of your Java program. Also, Java 9 is coming up with built in JSON support in JDK, which will make it easier to deal with JSON format, but that will not replace existing Jackson and GSON library, which seems to be very rich with functionality.