0

I'm trying to understand below Java method from the Spring API project.

This method is used to get response a HTTP GET at another server.

As per my understanding of abstract datatype, I think that we provide the response datatype as ResponseEntity<T> is enough to interpret the returned value of a method.

Why do we need <T> before the return type of this method?

public <T> ResponseEntity<T> getWithJson(String url, @Nullable String json, Class<T> type) {
JonK
  • 2,097
  • 2
  • 25
  • 36
datnt
  • 372
  • 2
  • 15
  • 2
    For future reference, it's generally not a good idea to include pictures of text in your question unless there's a very good reason to - 99.999% of the time you're better off just pasting the actual text into question instead. – JonK Sep 03 '19 at 10:50
  • @JonK Don't you think that a picture is really helpful in explaining the issue, it helps the reader to quickly realize the meaning. – datnt Sep 03 '19 at 10:55
  • You need a tutorial on Java generics. – Raedwald Sep 03 '19 at 10:56
  • The `getWithJson` method is a [generic method](https://docs.oracle.com/javase/tutorial/extra/generics/methods.html) (<- click link for tutorial) - it's a method with a type parameter, and the `` specifies that type parameter. – Jesper Sep 03 '19 at 10:57
  • @Jesper: the link you provided does not help, it does not explain The format of declaration they mentioned only as below static void fromArrayToCollection(T[] a, Collection c) the format I ask is different, it has < T > after public, and it also has < T> after ResponseEntity as: ResponseEntity – datnt Sep 03 '19 at 11:11
  • 1
    It very much helps. You have to carefully study the **whole** concept of "generics" in Java. Meaning: knowing two words of French isn't sufficient to understand a full sentence in French. You should step back, and read about java generics for a few hours. This is a **large** thing in Java, and it takes time to learn all the details. But knowing all the details is very helpful, because as said: generics matter in java, biggly. – GhostCat Sep 03 '19 at 11:13
  • 1
    Whether the `` is after the keyword `public` or after the keyword `static` does not matter, it's still the same concept: a generic method with a type parameter. – Jesper Sep 03 '19 at 11:20
  • @datnt There are *rare* situations where a picture is useful, however this was not one of them – JonK Sep 03 '19 at 12:27

2 Answers2

1

The <T> is used to signify/identify the name of the Generic type you will be using inside your function. You could use a different name like <Test> for example but then you'd have to use the class name Test wherever you are using the generic class identifier.

If you do not write a generic type name before the return type of the method, Java does not treat the method as a generic function. Rather, it will treat <T> as an actual class and will look for this class in classpath.

Now, when you use this function. Java is going to replace with the Object type that you want to use it for.

ResponseEntity<String> hello() {
    return new ResponseEntity<>("Hello World!", HttpStatus.OK);
}

<String> keyword in this case signifies that <T> needs to be treated as a String type and the function returns an object of type String.

Nabeel Mehmood
  • 378
  • 1
  • 5
  • @ Nabeel Mehmood Thank you for your response. Beside mentioning about Jave Generic, what I want to ask is: The point is we have T after ResponseEntity already, i.e ResponseEntity Logically, then why do we need T infront of it? I think without < T > in front-of that place, we still have enought logic to infer that, we want a generic DataType to be passed into ResponseEntity, right? – datnt Sep 03 '19 at 11:04
  • 2
    `` itself doesn't mean anything by itself. As I mentioned in my answer, if you don't write anything before the return datatype, `` will not be treated as a generic. Rather it will be treated as a class itself. Java will look for a class named `` in your program and then fail to build. The `` before the datatype is pretty much the part of the code that is telling Java that this is a generic function and that any `` present inside the method is NOT a concrete class itself. – Nabeel Mehmood Sep 03 '19 at 11:09
  • 2
    `public ResponseEntity foo()` is no different than lets say `public ResponseEntity foo()`. Its the identifier before the return type that makes it a generic function. – Nabeel Mehmood Sep 03 '19 at 11:12
  • 1
    @ Nabeel Mehmood Thank you for your response. I've learnt something new today. – datnt Sep 03 '19 at 11:20
0

Your looking at Java Generics - background reading:

In this case, go have a look at the source of ResponseEntity. In this case you will see the class/interface itself is "generic" - it behaves differently depending how it was instantiated.

The classic example would be a simple List:

import java.util.ArrayList;
import java.util.List;

class Scratch   {
    public static void main(String[] args) {
        List<String> strings = new ArrayList<>();
        List<Integer> integers = new ArrayList<>();

        // some small details
        strings.add("a string");
        integers.add(Integer.valueOf(6));

        String s = strings.get(0);
        Integer i = integers.get(0);

        System.out.println("s: " + s);
        System.out.println("i: " + i);

    }
}

Output:

s: a string
i: 6

See how there is no need to cast (String) or (Integer) anywhere? The type <T> is now a compile time test instead of a runtime one so whole classes of error can be eliminated just to make your code compile

Geoff Williams
  • 1,320
  • 10
  • 15
  • @ Geoff Williams: the example to provided is not very match to my question, it is just a simple case related to generic data type. – datnt Sep 03 '19 at 11:05