Java Generics Tutorial

Java Generics Tutorial

FavoriteLoadingAdd to favorites

– This is a broad tutorial on what Generics are in Java and how to use them.

source

Java468x60
By |2019-06-18T19:42:04+00:00June 18th, 2019|Java Video Tutorials|40 Comments

40 Comments

  1. AKamel June 18, 2019 at 7:42 pm - Reply

    Can u give us an example of :
    if the user want to choose the type ?
    How can we figure witch type he had choose and how to figure witch Scanner we use ? ( nextInt , nextLine , ….)
    Thank u

  2. DeathBender June 18, 2019 at 7:42 pm - Reply

    by the time I'm typing this you are probably aware of the PECS schema (Producer Extends Consumer Super) https://i.stack.imgur.com/KjDLw.png (this picture is not mine i took it from a stackoverflow thread …. pardon me )

  3. Gabor Rajnai June 18, 2019 at 7:42 pm - Reply

    Exellent tutorial. Just one comment about why ? is more generic than T. Simply because you can code a statement like this
    List<? extends T>. So if you code List<T extends Number> you hardcoded, that T must be a Number <? extends T> can extend anything with a type T, so it is more generic.

  4. Dvd B June 18, 2019 at 7:42 pm - Reply

    On the reason why <?> is more generic than <T>. For example:
    public <T> void foo1(List<T> i) { i.set(0, i.get(0)); }

    public void foo2(List<?> i) { i.set(0, i.get(0)); }
    foo1 compiles whilst foo2 doesn't.
    In foo1 i.get(0) has type T, which is the same T of List<T>. In foo2 i.get(0) has unknown type ?#1, different from unknown type ?#2 of List<?#2>. foo2 is more generic on types, and indeed fails to compile.

  5. artifactingreality June 18, 2019 at 7:42 pm - Reply

    this video is best. other videos incorrectly start out by saying java cant determine type at runtime. and another video about generics I discounted just because the guy talked about ArrayList instead of List. noobs man.

  6. Abel Warres June 18, 2019 at 7:42 pm - Reply

    Hey man this is a good tutorial. When you were trying to describe how using wildcard with generics is more flexible than without wildcard, the examples you gave will work without wildcard. Here is an example of when you need wildcard.
    Let's say I have two function printMoreRestrictive and printLessRestrictive below:

    List<Double> l1 = new ArrayList<>();
    List<Integer> l2 = new ArrayList<>();
    l1.add(5.1);
    l2.add(6);

    You can do:
    printLessRestrictive(l1, l2);

    You cannot do:
    printMoreRestrictive(l1, l2);

    You cannot use two different types to refer to same T in this case integer and double but you can with wildcard in the same scope.

    private<T extends Number> void printMoreRestrictive(List<T> l1, List<T> l2){
    for(Number n:l1) {
    System.out.println(n.getClass().getName() + " : " + n);;
    }

    for(Number n:l2) {
    System.out.println(n.getClass().getName() + " : " + n);;
    }
    }

    private void printLessRestrictive(List<? extends Number> l1, List<? extends Number> l2){
    for(Number n:l1) {
    System.out.println(n.getClass().getName() + " : " + n);;
    }

    for(Number n:l2) {
    System.out.println(n.getClass().getName() + " : " + n);;
    }
    }

  7. Chung Kang June 18, 2019 at 7:42 pm - Reply

    Very helpful examples!

  8. Federico B June 18, 2019 at 7:42 pm - Reply

    You're better than my professor.. this helped me a lot, thanks! Keep it up man

  9. Green Eggs and Ham June 18, 2019 at 7:42 pm - Reply

    I don't really get that 'Box with String' Box<String>
    what if the Box class has other methods which didn't return Strings?
    Is Box<String> declaring a Box class that only deals in Strings? Can it have other methods?

  10. Atul Kumar Verma June 18, 2019 at 7:42 pm - Reply

    At 5:25 I think, diamond notation is only supported after Java 8 and above.

  11. Richard Wang June 18, 2019 at 7:42 pm - Reply

    Thanks man. This is a great video, but it would be greater if you did your homework lol

  12. Htng Uvon June 18, 2019 at 7:42 pm - Reply

    diamond are forever

  13. XINGYUN XUE June 18, 2019 at 7:42 pm - Reply

    wonderful explanation. Thanks sir.

  14. Ivan Derlich June 18, 2019 at 7:42 pm - Reply

    If we use "<? extends Integer>" then, what types are not accepted? It seems like everything will work. Like a yes man.

  15. mayur shetye June 18, 2019 at 7:42 pm - Reply

    This is So Awesome ! Appreciate it !!!!

  16. nikhil agarwal June 18, 2019 at 7:42 pm - Reply

    Thanks man….

  17. peter veres June 18, 2019 at 7:42 pm - Reply

    Thank you Trevor! Your "rambling" is the most understandable and helpful tutorial that I have come across so far and it has helped a lot in understanding the subject 🙂

  18. KalecAmagosh June 18, 2019 at 7:42 pm - Reply

    Two space indentation? Gross.

    Great tutorial though!

  19. Lumary June 18, 2019 at 7:42 pm - Reply

    Excellent, thank you! 🙂

  20. MrBuckethead911 June 18, 2019 at 7:42 pm - Reply

    One of the, if not THE, best video on Generics in Java out there! Thanks alot! 🙂

  21. Daniel Ro June 18, 2019 at 7:42 pm - Reply

    this is a great video. good job.

  22. A. P. June 18, 2019 at 7:42 pm - Reply

    Trevor, man, I wish you every success. Thank you as this is the best explanation I have seen on Generics for Java – ever. This is my first Youtube comment, seriously!

  23. Péter Kemenyás June 18, 2019 at 7:42 pm - Reply

    Cool video! Thanks a lot for explanation.

  24. Chris Bennett June 18, 2019 at 7:42 pm - Reply

    Dude this tutorial is so good. I'm really enjoying watching this. I am learning so much about how Generics work. I have an assignment where I have to use Generics, and I'm not really good at them yet. Thanks man.

  25. Kayhan Ozalp June 18, 2019 at 7:42 pm - Reply

    You make it seem so easy and natural, but whenever I think I got it and want to make use of it I find myself trying to bite my tail. Need to work a little more with this.

  26. Elyktaz June 18, 2019 at 7:42 pm - Reply

    Great tutorial!

  27. Okan ciftci June 18, 2019 at 7:42 pm - Reply

    Guy's can someone help me I didn't get poınt What is difference is super and extends

  28. Abhijit Salunkhe June 18, 2019 at 7:42 pm - Reply

    Hi Are you sure that we can add Number in case of List<? super Integer> !!! , I think the only type we can add for sure is integer, hence it is called as lower bounded.
    Just to prove… We can pass List<Integer> to this method, adding Number to list of integers would be invalid.

  29. JAPJEET SINGH June 18, 2019 at 7:42 pm - Reply

    which ide are you using?

  30. Jeremy Mechen June 18, 2019 at 7:42 pm - Reply

    great video

  31. Code Digest With MrWallet June 18, 2019 at 7:42 pm - Reply

    Dude Thats Just Brilliant. I didnt even know Generics is that deep.I clearly saw adele rolling in it. Lol.
    Thanks

  32. Steven Barnes June 18, 2019 at 7:42 pm - Reply

    Best tutorial on generics on the internet, fact! Find a better one, prove me wrong.

  33. qwwsw eee June 18, 2019 at 7:42 pm - Reply

    Thank you this tutorial is brilliant!

  34. XxTreem Tv June 18, 2019 at 7:42 pm - Reply

    Thankyu

  35. Navneet Prakash June 18, 2019 at 7:42 pm - Reply

    i found the diamond thing in intelij ide while using lol less code to write i guess 😀

  36. wBacz June 18, 2019 at 7:42 pm - Reply

    8:23 bum bum 😀

  37. anis sth June 18, 2019 at 7:42 pm - Reply

    Thanks finally understood generics

  38. Andrew Schatz June 18, 2019 at 7:42 pm - Reply

    Hey thanks for doing this video. Honestly one of the most reassuring things is that you even said that wildcards are extremely complicated to understand and even you have some issues with it. It helps normalize my struggles with it in my college course.

  39. Olav Sørlie June 18, 2019 at 7:42 pm - Reply

    Realy sweet tutorial! 😀 Understanding the basics is realy important, and you did the best job explaining it 😀 Thank you, helped me alot 🙂 Throwing a sub!

  40. Zakia Amalia June 18, 2019 at 7:42 pm - Reply

    good explanation. but i think you are too fast, so i must rewind some parts again and again hehe maybe you could slower it a little bit? because im no native english lol

Leave A Comment

*