1

In the below program am passing 2 types parameters uisng single method. I am getting "The operator + is undefined for the argument type(s) T, K" while adding 2 parameters.

import java.util.*;
class Multipletypeparameters<T,K>{
    T obj1;
    K obj2;
    public void setting(T obj1,K obj2){
        this.obj1=obj1;
        this.obj2=obj2;
    }
    public T add(){
        //Here While adding I am getting The operator + is undefined for the argument type(s) T, K error
        return obj1 + obj2;
    }
}
public class MultipleTypeParametersExample {

    public static void main(String[] args) {
        MultipleTypeParametersExample<String,String> g1=new MultipleTypeParametersExample <String,String>();
        g1.setting("A","B");
        System.out.println(g1.add());

        MultipleTypeParametersExample <Integer,Integer> g2=new MultipleTypeParametersExample <Integer,Integer>();
        g2.setting(10,10);
        System.out.println(g2.add());
    }

}
8
  • are you trying to add two objects together? Commented Oct 17, 2016 at 13:07
  • 2
    That's because the compiler doe not know how to use the + operator to "add" two instances of the generic types T and K. Imagine you pass a Car and a Boat instance for T and K - what do you expect the + to do?! A driving boat? A swimming car? And how would the compiler know that? Commented Oct 17, 2016 at 13:08
  • Well, it is true since the + method is not defined for Object. What should happen if you create a MultipleTypeParametersExample<List, Set>? Commented Oct 17, 2016 at 13:09
  • the + operator is defined for primitive number types and Strings only. anything else needs a method defined in an interface where the implementing class knows what to do. Commented Oct 17, 2016 at 13:09
  • then how to add , With strings I need to get output as AB and with int I need output 20 Commented Oct 17, 2016 at 13:12

4 Answers 4

2

Your problem is related to the fact that you use the operator + with objects while it is only understood by the compiler with primitive types and/or String.

One way to solve your problem is to propose an implementation of this operation.

Assuming that you use Java 8, your code could be something like this:

The class Multipletypeparameters

class Multipletypeparameters<T, K> {
    T obj1;
    K obj2;
    private final BiFunction<T, K, T> add;

    public Multipletypeparameters(BiFunction<T, K, T> add) {
        this.add = add;
    }
    public void setting(T obj1, K obj2) {
        this.obj1 = obj1;
        this.obj2 = obj2;
    }
    public T add(){
        return add.apply(obj1, obj2);
    }
}

Your test code:

Multipletypeparameters<String, String> g1 = new Multipletypeparameters<>((a, b) -> a + b);
g1.setting("A", "B");
System.out.println(g1.add());

Multipletypeparameters<Integer, Integer> g2 = new Multipletypeparameters<>(
    (a, b) -> a + b
);
g2.setting(10, 10);
System.out.println(g2.add());

Output:

AB
20

This approach applies the Strategy pattern which allows you to specify the implementation to use (the strategy to adopt) at Runtime but you could also make this method abstract and propose sub classes that will implement this method, as next:

abstract class Multipletypeparameters<T, K> {
    ...
    public abstract T add();
}

The implementations:

class MultipleStringparameters extends Multipletypeparameters<String, String> {

    @Override
    public String add() {
        return this.obj1 + this.obj2;
    }
}

class MultipleIntegerparameters extends Multipletypeparameters<Integer, Integer> {

    @Override
    public Integer add() {
        return this.obj1 + this.obj2;
    }
}

The test code:

Multipletypeparameters<String, String> g1 = new MultipleStringparameters();
g1.setting("A", "B");
System.out.println(g1.add());

Multipletypeparameters <Integer, Integer> g2 = new MultipleIntegerparameters();
g2.setting(10, 10);
System.out.println(g2.add());

Output:

AB
20
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for sample code. compiled in 1.8, but getting many errors regarding a and b
if you use java 8, it should compile, they are just lambda expressions
I'd say that BiFunction is the way to go here. The currently accepted answer using instanceof looks .... ehm... not so nice (not to say "horrible").
1

Your problem is that you are using language elements that you don't understand!

You see, you declared

public T add(){
  return obj1 + obj2;
}

Meaning: that method is supposed to return an object of type T. But your code tries to use the primitive-only operator + on a T and K object. That simply cant work. Because that operator only works for primitive types; and of course, even if it would work, who says that you can "adding" two arbitrary T and K objects would result in an object of type T?! The only thing that would work:

public String asStrings(){
  return obj1.toString() + obj2.toString();
}

And as you actually want to add numbers; you have to look into changing your generic types into something that extends Number! Then you you could be calling methods like intValue() or doubleValue() on them. And then using "+" becomes possible again.

Comments

1

You can change your add method to something like this. Note I've changed the class to a single generic type, otherwise you'd need to add more options to cater for Integer, String combos and I don't see why you would want to add different types. I find this not to be an elegant solution, and you'd be better off using a Strategy pattern where you can define what operation is performed when adding.

public class Example<T> {
    T obj1;
    T obj2;

    public void setting(T obj1,T obj2){
        this.obj1=obj1;
        this.obj2=obj2;
    }

    public T add(){
        if (obj1 instanceof String) {
            return (T) ((String) obj1 + (String) obj2);
        } else if (obj1 instanceof Integer) {
            return (T) ((Integer) ((Integer) obj1 + (Integer) obj2));
        } else {
            return null;
        }
    }
}

Comments

0

what do you want exactly in "add" method ? I guess if your obj is int you want to add two numbers or you want to suffix strings. In conclusion you should check object I mean you have to use instanceOf keyword. For example, obj1 instanceof String

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.