An example of method overloading. The program calls a function to get the square root of a number. The appropriate function (integer or double version) is executed.

package squareroots;
public class SquareRoots
{
public static void main(String args[])
{
double x = 3, result = 0;
int y = 5;
result = getRoot(x);
System.out.println("\n\tSquare root of " + x + " = " + result + "\n");
System.out.println("\n\tSquare of result = " + result*result + "\n");
result = getRoot(y);
System.out.println("\n\tSquare root of " + y + " = " + result + "\n");
System.out.println("\n\tSquare of result = " + result*result + "\n");
}
private static double getRoot(int inum)
{
System.out.println("\nIn the integer version");
return Math.sqrt(inum);
}
private static double getRoot(double dnum)
{
System.out.println("\nIn the double version");
return Math.sqrt(dnum);
}
}

While pursuing a Computer Science degree, I founded DaniWeb.com, an online community for developers and IT professionals. I coded the backend platform from the ground up and I also do all of the advertising sales and SEO. I'm a super-geeky programmer with a passion for Internet marketing.

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.