Friday, 3 June 2016

Java Program to Reverse an Integer Number without using String - Example

How ot revrse a integer number in Java

When I was asked "Can you write a program to reverse a number in Java" long long years back, I did a trick. I converted the number to String by just concatentating with empty String e.g. "" + 123 and then converted that Stirng to StringBuilder can caled the reverse() method.

That solved the problem but Interviewer was not satisfied. He said Ok, but then asked again, can you reverse a number without using anything String or StringBuilder and by building your logic with basic programming elements. I was confused, what do you mean by basic programming elements? He said, I can use loop, if block, operator, and other basic stuff which makes a programming language.

Saturday, 28 May 2016

How to Check if Given Number is Prime Number in Java

Before writing a progarm to check if a number is prime or not, let's first revise what is a prime number? In Maths, a number is said to be prime if it is not divisible by any number other than itself. You can now convert this definition to code and create your Java program to check if given number is prime or not.

Friday, 27 May 2016

How to Calculate Factorial in Java using Recursion and Loop

Java Program to calculate factorial in Java using Recursion
You can calculate factorial of a given number in Java program either by using recursion or loop. The factorial of a number is equal to number*fact(number-1), which means its a recursive algorithm and that's why it is often used to teach recursion to programmers in computer programming classes.

But, recursion is not always preffered in production code and that's why you also need to learn how to convert a recursive algorithm to an iterative algorithm i.e. algorithm which make use of various loop construct in Java programming language.

Here in this Java beginners tutorial, I'll teach you both ways to calcualte factorial in Java i.e. with and without recursion. Since recursive algorithm is very simple, we'll fisrt look at that and later we'll check out how to calculate factorial without recursion.

Thursday, 26 May 2016

How to print Pyramid Pattern of Numbers in Java

Write a Java Program to print the following Pyramid pattern of numbers:


You can easily print above pattern using two loops and with the help of print() and println() method of PrintStream. You can access them using System.out object as System.out.print() and System.out.println().

But, in this program, I'll show you can print this patter by using just one loop instead of two.

Friday, 20 May 2016

10 Java Books Every Senior Developer Should Read

Apart form programming, one of my passion is to read books. I have hundreads of e-books on my smartphone and PC and tens of book on my self, especially realated to Java. Since I have benefitted a lot from the book, I am going to share my collection of Java related books, which I think every senior developer should read. By saying senior Java developer, I mean anyone doing programming in Java for more than 3 to 4 years. You don't need to buy these books, you can download PDF from Internet for some of them, or you can borrow it from your friends or library, but your must read these books. Sometime, I end up purchasing the book but never get chance to read it, that's what I hate. If you have book which looks like new even after one year of purchase, then you have not read it. This is why I also like to borrow books from friends and library. This put me some pressure to read atleast couple of chapters before returning it :-)

Thursday, 31 December 2015

7 Difference between REST and SOAP WebService in Java Interview

Difference between REST and SOAP in Java
What is the difference between REST and SOAP Web-service is a very popular question from Java J2EE interviews. I have myself asked this question a number of times. Here I am listing out some of the key differences between REST and SOAP purely from interview perspective i.e. I am only mentioning key points, not going into details. Unless you are doing exclusive work in REST or SOAP Web service, interviewer will not go into deep and if you have done the work already, you probably don't need this list. Anyway, let's see some key difference between REST and SOAP web service in Java.

6 Difference between wait() and sleep() method in Java

Though both wait() and sleep() method are used to pause the running thread, here are some important difference between them:

1) The wait() method must be called from the synchronized context (can be a synchronized block or method) but sleep() method can be called from within our with-out synchronized block.