0

I have created a method as bellow

static <N> N addTwoString(N a, N b){


        StringBuilder sb = new StringBuilder();
        sb.append(a);
        sb.append(b);

        return sb.toString();
    }

public static void main(String[] args)
    {


        addTwoString("a", "b");
    }

For this situation I pass two Strings and append that and return it using StringBuilder. Anyhow I am getting the error at return statement saying Type mismatch: cannot convert from String to N. My question here is this method accepts the String values (even the type is N) with out any issues but why it gives error at return statement?

Java-Seekar
  • 1,720
  • 5
  • 30
  • 52
  • 1
    Just change the return type to String and it will do fine – Joshua Sep 24 '13 at 12:07
  • From your comments below I guess, you weren't that busy with generics yet. I'd recommend you to thoroughly read some tutorials on java generics before posting questions about basic stuff here. No offense, but SO is no tutorial platform. Just some advise so you do not get flamed. – Joshua Sep 24 '13 at 13:39

6 Answers6

5

sb.toString() will always return a string, on the other hand your method should return a N type.

Consider, addTwoString(1,2) according to your method, this should return Integer but sb.toString() is a String. That is why its a compiler error.

rocketboy
  • 9,573
  • 2
  • 34
  • 36
  • How can we make any object whic is N type? – Java-Seekar Sep 24 '13 at 12:08
  • N is called a type parameter. It can be anything! Look at this [SO question](http://stackoverflow.com/questions/4073359/understanding-java-generics-type-parameter-conventions). – rocketboy Sep 24 '13 at 12:11
1

Why do you use generics here?

static String addTwoString(String a, String b){
    StringBuilder sb = new StringBuilder();
    sb.append(a);
    sb.append(b);

    return sb.toString();
}

Apart from that, why do you need this method at all? Instead, you could just use either a.concat(b) or a + b!

isnot2bad
  • 24,105
  • 2
  • 29
  • 50
  • 2
    that isn't the same as @K.Senthuran wants - maybe he wants the `.toString()` of any object concatenated, not only `Strings` – Joshua Sep 24 '13 at 12:07
  • I just wanted to test the generic behaviour. This method accept Strings then why doesnt return String? Method parameters type also N and the return type also N. – Java-Seekar Sep 24 '13 at 12:10
  • 1
    In fact, it DOES return a String - but your method signature says that it has to return an object of the same type as the arguments that are passed to the method. So if you pass Integer objects, an Integer object has to be returned - but you always return a String. – isnot2bad Sep 24 '13 at 12:13
1

N is not a String. But, you are trying to pass strings as N and also you are returning a string but return type is N.

I understand that you are learning generics but what you are trying to do tells me you haven't understood the basic.

Read through these-

Oracle Generics

Java Generics: List, List<Object>, List<?>

Community
  • 1
  • 1
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
1
class Test
{
    public static void main(String[] args)
    {
        addTwoString("a", "b");
    }
    static <N> String addTwoString(N a, N b){
        StringBuilder sb = new StringBuilder();
        sb.append(a);
        sb.append(b);

        return sb.toString();
    }
}

your return type should be a String not the generic version if you are sure that the function will return only String type and a cast to return (N)sb.toString(); would also work.

Rishi
  • 11
  • 2
0

If you really want to use generic , then convert typecast return sb.toString(); to return (N)sb.toString();

However your method should just return. Not sure why you want Generic for String?

Optional
  • 4,387
  • 4
  • 27
  • 45
0

The problem is that you don't pass a type parameter for N.

You do this:

addTwoString("a", "b");

while you should be doing this:

MyClass.<String>addTwoString("a", "b");

Remark 1: I think there is a design flaw here. Your method says that it is working with Strings so you don't really need the N type parameter here. This is the proper way to do it:

static String addTwoStrings(String a, String b){
        StringBuilder sb = new StringBuilder();
        sb.append(a);
        sb.append(b);
        return sb.toString();
}

Remark 2: I think you should not reinvent the wheel here. If this is not a homework assignment you are way better off using the built-in method for adding two strings (concat(String))

Adam Arold
  • 29,285
  • 22
  • 112
  • 207