Method Overloading with Overloaded Methods That Use Different Parameter Types, Including Booleans and Boolean Arrays in Java


Write a Java program to demonstrate method overloading with overloaded methods that use different parameter types, including booleans and boolean arrays

  • The BooleanManipulator class contains two convertBoolean methods: one that accepts a single boolean value and another that accepts an array of boolean values.
  • The first convertBoolean method converts a single boolean value to its string representation using String.valueOf and returns it.
  • The second convertBoolean method converts an array of boolean values to a string representation by iterating over each value, converting it to a string, and appending it to a StringBuilder with a space separator. It then trims the result to remove any trailing spaces and returns it.
  • In the Main class, an instance of the BooleanManipulator class is created.
  • The convertBoolean method is called twice, once with a single boolean value and once with an array of boolean values.
  • In each case, the appropriate convertBoolean method is invoked based on the argument types, and the result is printed.

Source Code

class BooleanManipulator
{
	String convertBoolean(boolean value)
	{
		return String.valueOf(value);
	}
 
	String convertBoolean(boolean[] values)
	{
		StringBuilder result = new StringBuilder();
		for (boolean value : values)
		{
			result.append(String.valueOf(value)).append(" ");
		}
		return result.toString().trim();
	}
}
 
public class Main
{
	public static void main(String[] args)
	{
		BooleanManipulator manipulator = new BooleanManipulator();
		System.out.println("Converted Boolean : " + manipulator.convertBoolean(true));
		boolean[] boolArray = {true, false, true};
		System.out.println("Converted Boolean Array : " + manipulator.convertBoolean(boolArray));
	}
}

Output

Converted Boolean : true
Converted Boolean Array : true false true

Example Programs