2

How please works following generic casting?

private <T, N> N send(final T request) {
    final String serviceUrl = "someUri"

    try {
        return (N) webServiceTemplate.marshalSendAndReceive(serviceUrl, request);
    } catch (final SoapFaultClientException e) {

    }
}

When I call this private method like this:

MyReq request = new  MyReq();
MyResp response = send(req);

How this method casting this object into MyResp object? What mean this in return type:

< T, N>

4
  • <T, N> means, that the method has two type parameters, named T and N. Commented Aug 5, 2014 at 6:35
  • The return type is N. private ... N. Commented Aug 5, 2014 at 6:39
  • your return type is only N. <T, N> is a kind of initialization of the generic variables. This is the syntax of generic variables initialization. Commented Aug 5, 2014 at 6:39
  • <T,N> is not return type but N is Commented Aug 5, 2014 at 6:39

4 Answers 4

1

< T, N>

Is not a return type. N is.

private <T, N> N send(final T request)
               ↑

You have "choosen" type T by passing MyReq argument. Before returning value of marshalSendAndReceive it is being casted to N type. In your case MyResp.

<T,N> 

just declares that this class/method is generic and you may specify generic input type as well ans output.

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

4 Comments

Ok, so (N) webServiceTemplate.marshalSendAndReceive just casting object into N (MyResp) ok? And why I can't just return N object? Why I need there <T,N> ?
<T,N> allows you to use send method with any T argument type and expect N type of returned value from the method. Consider N and T as "variables" that will be initialized when constructing class/using method. The same method might get String input and return Integer output. But in your case it received MyReq and returns MyResp. That's the point of generics in Java.
Non-generic version of this method would be: private MyResp send(final MyReq request) { final String serviceUrl = "someUri" try { return (MyReq) webServiceTemplate.marshalSendAndReceive(serviceUrl, request); } catch (final SoapFaultClientException e) { } }
semi-generic: private <Z> Z send(final MyReq request) { final String serviceUrl = "someUri" try { return (Z) webServiceTemplate.marshalSendAndReceive(serviceUrl, request); } catch (final SoapFaultClientException e) { } } Now it may return any type marked as Z while it can get MyReq as input.
0

The method is parameterized with the generic parameters T and N. It takes an argument of type T and returns a reference of type N.

The return type is not <T,N>, it is just N.

Comments

0

It's about using templates in java similar to templates in C++;

Example:

void add(T x, T y){
System.out.println(x+y);
}

Above function can be called with either of the following...

add(3.0f, 4.5f);
add(2,3);

Means, the function is for any kind of parameters

Comments

0

Here T and N are Generic parameters.

Here the return type is N not <T,N>.

Read more...

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.