Question

I'm very confused about a task that I'm required to do (Not asking for the answer, just a point in the right direction)

I basically need to create an add method for the following main method. I'm confused though how to pass the arguments due to the fact they are mix a primitive and wrapper. Any help would be good.

   public static void main(String[] args) {
   Double answer1 = add(2, 7);
   Number answer2 = add(new Integer(4), new Double(5.2));
   double answer3 = add(8, 1.3);
   System.out.println(answer1 + " " + answer2 + " " + answer3);
Was it helpful?

Solution

If I define a single method, for all your example then too it compiles and produce result as expected without any errors

public static double add(double i , double j)
{
   return i + j;
}

This phenomena of Boxing Unboxing is neatly handled by Java compiler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top