Friday, September 4, 2015

How to loop over two dimensional array in Java?

You can loop over two dimensional array in Java by using two for loops, also known as nested loop. Similarly to loop an n-dimensional array you need n loops nested into each other. Though its not common to see array of more than 3 dimension and 2D arrays is what you will see most of the places. It's one of the most useful data structure in programming world. You can use two dimensional array to make finite state machine (FSM) to solve state based problems, you can use 2D array to create board games like Chess, Sudoku and Tic-Tac-To and you can even use a two dimensional array to create 2D arcade games e.g. Tetris, Super Mario Bros and so on. Whatever you see on your screen is nothing but a 2D array which is populated using tiles. In order to make use of 2D array, you must know how to populate and iterate over it and that's what you will learn in this article. You can think about two dimensional array as matrix which has rows and columns, this help to visualize contents of array. In order to loop over 2D array, we first go through each row and then again we go through each column in every row. That's why we need two loops, nested in each other. Anytime, if you want to come out of nested loop, you can use break statement. If you are absolutely beginner and just started with programming, I recommend to read Head First Programming first. Once you gone through the book, most of the programming concept e.g. array, string, vector will make sense to you.

Wednesday, September 2, 2015

Difference between Java and C++ Constructor - Interview Question

If you are a C++ Programmer, now learning Java then you will find lot of similarity between two of the most popular object oriented programming languages e.g. both support Abstraction, Encapsulation, Class, Object and other OOP concepts. But, they are some subtle differences as well, e.g. both Java and C++  have constructor and they work the same way in Java as they do in C++, but the way they are called is different. For example, In Java, a constructor must be called by using new() operator, there is no other way to explicitly call the constructor while creating object, but in C++ you can do it without new operator. This is a good interview questions for programmers who knows both Java and C++. I know, switching context between C++ and Java is not easy, especially during interview but that's also perfect way to test how much experience candidate has. An experience C++ programmer which has worked in Java for couple of years should know the different on top of their head. BTW, if you are from C++ background and looking for a good book to learn Java then check out Core Java, Volume 1 by Cay S. Horstmann. Cay has taken care to put C++ perspective of things when introducing new concepts in Java. So, you will find comparison of Generics in Java with templates in C++, which helps to understand the concept quickly.

Monday, August 31, 2015

Difference between SOAP and RESTful Web Service in Java

Though both SOAP and RESTful web services allows a client to query server for some information, but the way they are implemented and used is quite different. Main difference between SOAP and REST is that former provides an standard of communication between client, server and other parties and has restricted set of rules and format, while REST leverages ubiquity of HTTP protocol, in both client and servers, to allow them to communicate with each other regardless of their implementation. In short, getting data from a RESTful web service requires less headache then getting data from a SOAP web service. Since everybody is familiar with HTTP requests like GET or POST, its easy to understand and correlated how RESTful webservice are working and which URL of REST web service provides what kind of information. In SOAP, you need to understand lengthy WSDL document to find out right methods and right way to call them.