Intermediate Java Tutorial – 19 – Generic Return Types

///Intermediate Java Tutorial – 19 – Generic Return Types

Intermediate Java Tutorial – 19 – Generic Return Types

FavoriteLoadingAdd to favorites

Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –

source

Java468x60
By |2019-05-11T18:51:04+00:00May 11th, 2019|Java Video Tutorials|50 Comments

50 Comments

  1. Keertheswaran Thevarajan May 11, 2019 at 6:51 pm - Reply

    What is T extends comparable <t>

  2. star moh May 11, 2019 at 6:51 pm - Reply

    hello , what if i wanted to use max with diff type of data, like max ( "st" , 5, '4') ,thank you

  3. LWER14 May 11, 2019 at 6:51 pm - Reply

    Bucky Larson the GOAT

  4. Michael Lesley May 11, 2019 at 6:51 pm - Reply

    ótima videoaula!!

  5. Kappa123 May 11, 2019 at 6:51 pm - Reply

    5:15 *thots

  6. faouziauk1 May 11, 2019 at 6:51 pm - Reply

    Big question…could you show the same generic tutorial (which is very good) for spring hibernate as in having a generic service class that extends CrudRepo interface

  7. Daniel Martinez May 11, 2019 at 6:51 pm - Reply

    I fucking love you dude

  8. vishal1982 c May 11, 2019 at 6:51 pm - Reply

    how m became greatest?

  9. Neela May 11, 2019 at 6:51 pm - Reply

    Sir you have only shown one return type using Comparable class. what about the other return types?Can you give a full reference please?

  10. Scott May 11, 2019 at 6:51 pm - Reply

    Created by Scott February 16th, 2018
    Best tutorials ever.

  11. Nat B May 11, 2019 at 6:51 pm - Reply

    If the type is generic, why can't the function accept mixed data types like

    max(1, "tots", 'c')

  12. Nat B May 11, 2019 at 6:51 pm - Reply

    Comparable is an interface why did he use extends and not implement?

  13. Vim sap May 11, 2019 at 6:51 pm - Reply

    This is good to learn.Need clarification for why it is return tots? and what basis it return tots, how the code is working internally?

  14. Ajay Reddy May 11, 2019 at 6:51 pm - Reply

    i tried this one . insted of a i put "m"
    package javaInter;
    import java.util.*;
    public class generic2 {
    public static void main(String[]args){
    System.out.println(max(2,99,4,6,89,78));
    }
    public static <T extends Comparable <T>> T max(T a,T b,T c, T d,T e, T f) //i added "d,e,f"//
    {
    T m=a;
    if(b.compareTo(m)>0)// i changed it to "m"//
    m=b;
    if(c.compareTo(m)>0)
    m=c;
    if(d.compareTo(m)>0)
    m=d;
    if(e.compareTo(m)>0)
    m=e;
    if(f.compareTo(m)>0)
    m=f;
    return m;
    }
    }

  15. P092O9901O09 May 11, 2019 at 6:51 pm - Reply

    this syntax is getting hairy
    public static <T extends Comparable<T>> T max(T aar, T bar, T car){

  16. Shihab May 11, 2019 at 6:51 pm - Reply

    public static <T extends Comparable<T>> T max(T… x)
    {
    T m=x[0];

    for (int i = 1; i < x.length; i++) {
    if( x[i].compareTo( m ) > 0 )// x[i] is greater than m
    {
    m=x[i];
    }
    }

    return m;
    }

  17. Zachary Patterson May 11, 2019 at 6:51 pm - Reply

    Go on any wikepedia page and click the first link on every wikepedia page you get to… you will end up on philosophy at one point.

  18. Goran Marinkovic May 11, 2019 at 6:51 pm - Reply

    For MINIMUM:

    import java.util.*;

    public class apples {
    public static void main (String[] args){
    System.out.println(min(23,42,44));
    System.out.println(min("apples", "tots","chicken"));
    System.out.println(min("-", "%", "++"));

    }

    public static <T extends Comparable<T>> T min (T a, T b, T c){
    T m = a;

    if(b.compareTo(m) < 0 )
    m = b ;
    if(c.compareTo(m) < 0 )
    m = c ;
    return m;
    }
    }

    output:
    23
    apples
    %

  19. Vishnu Kh May 11, 2019 at 6:51 pm - Reply

    Can we use this concept to return objects of different classes?? If yes, please provide a simple example as well..Thanks in advance…

  20. Tejus Wadbudhe May 11, 2019 at 6:51 pm - Reply

    import java.util.*;
    public class genericreturn {

    public static void main(String[] args) {
    System.out.println(max(23, 25, 30));
    System.out.println(max("-", "%", "++"));
    }

    public static <T extends Comparable<T>> T max(T a, T b, T c){ //T is comparable to any of its data type
    T m=a;

    if(b.compareTo(a) >0)
    m=b;
    if(c.compareTo(b) >0) //changed from c.compareTo(m)
    m=c;
    return m;
    }

    }

    OUTPUT:-
    30
    ++

    Why the output changes from – to ++

  21. Harsh Purani May 11, 2019 at 6:51 pm - Reply

    Can anyone help me how this max methods output in string is "tots"

  22. nikita kumari May 11, 2019 at 6:51 pm - Reply

    plz explain me wat is 'T extends Comparable <T>'?

  23. What IDE do you use?

  24. DarkEagle May 11, 2019 at 6:51 pm - Reply

    What about generic constructors? I tried declaring constructors as being generic, but it gives me an error message. How do i make a constructor generic?

  25. Michael Moore May 11, 2019 at 6:51 pm - Reply

    just curious , what makes "tots" the max compared to apples?

  26. Faisal Salem May 11, 2019 at 6:51 pm - Reply

    great tutorials dude

  27. Kaven Haul May 11, 2019 at 6:51 pm - Reply

    what IDE are you using???!

  28. Rohim Uddin May 11, 2019 at 6:51 pm - Reply

    i dont get it why is it that no matter how long the String variables are the generic method will always return the middle String variable according to order you pass the variables through.

  29. The Enigmatrix May 11, 2019 at 6:51 pm - Reply

    What's the difference in writing 'T extends Comparable <T>' and 'T extends Comparable'?

  30. Ahmed Mourad May 11, 2019 at 6:51 pm - Reply

    //Created by Ahmed Mourad on 05/02/2016

    public class Generic {

    public static void main(String[] args){

    Integer[] iray = {1, 2, 3, 4, 5};
    Character[] cray = {'a', 'h', 'm', 'e', 'd'};

    print(iray);
    print(cray);

    System.out.println(max(iray));
    System.out.println(max(cray));
    }

    private static <T> void print(T[] ray){

    for (T x : ray)
    System.out.printf("%s ", x);

    System.out.println();
    }

    private static <T extends Comparable<T>> T max(T[] ray){

    T m = ray[0];

    for (T x : ray)
    if (m.compareTo(x) < 0)
    m = x;

    return m;
    }
    }

  31. reem masoud May 11, 2019 at 6:51 pm - Reply

    Please tell me if I have mistaken, but there is a mistake in the compareTo() method in your code.

    if (b.compareTo(a) >0 )
    m=b ;

    means that if 'a' is greater than 'b', then 'b' would be the maximum number which is wrong

    because in the method compareTo(), it compares the argument so
    a>b returns +1
    a<b returns -1
    a=b returns 0

    Correction:
    if (b.compareTo(a) <0 )
    m=b ; //meaning that if a is less than b, then b is the maximum number
    if (c.compareTo(m)<0)
    m=c;

  32. Sodere Ethio May 11, 2019 at 6:51 pm - Reply

    public static <T extends Comparable<? super T>> int maks(T[] a) what does it mean the <? super T> part?

  33. Jinhua Chen May 11, 2019 at 6:51 pm - Reply

    import java.util.*;

    public class jeremy {
        public static void main(String[] args) {

            System.out.println(max(23, 42, 6));
            System.out.println(max("bb", "az", "ad"));
        }

        public static <T extends Comparable<T>> T max(T a, T b, T c) {
            T m = a;

            if(b.compareTo(a) > 0)
                m = b;

            if(c.compareTo(m) > 0)
                m = c;

            return m;
        }

    }

  34. karima ali May 11, 2019 at 6:51 pm - Reply

    <T extends  comparable<T>>
    the T in this case is class or interface ?why use extends

  35. Bartol J. May 11, 2019 at 6:51 pm - Reply

    why it returns tots????? tots has the less number of characters, at first i thaught it will return chicken since it has the greatest number of characters

  36. cw41486 May 11, 2019 at 6:51 pm - Reply

    is the <T extends Comparable<T>> a type of casting for the variable to be returned by the method?   a little confused on what that means

  37. Pejaki May 11, 2019 at 6:51 pm - Reply

    Why does T extend Comparable T, when Comparable is an interface, surely it should be T implements Comparable T???

  38. montycheese May 11, 2019 at 6:51 pm - Reply

    tots > apples because the compareTo method for Strings compares things lexicographically. Also shorter strings will be always return higher than longer strings, uppercase will triumph lowercase. Refer to http://stackoverflow.com/questions/7874972/lexicographic-order-in-java

  39. Jan May 11, 2019 at 6:51 pm - Reply

    thanks mate you really help me out as our teacher really isnt too good 🙂

  40. Amy Novun May 11, 2019 at 6:51 pm - Reply

    @FirstPersonOnUtube compareTo also sorts characters alphanumerically.  tots starts with 't', thus a higher alphanumeric value than 'a' or 'c'.  Capital letters also have different values because they are different characters than their lowercase parts.

  41. FirstPersonOnUtube May 11, 2019 at 6:51 pm - Reply

    How is tots a maximum though? How is a string variable determined to be the max?  (How is tots > apples or chicken?)

  42. krikkku May 11, 2019 at 6:51 pm - Reply

    Thanks for these great videos. I just started learning Java from a book couple of days back and they talked of generic mentioning that it will be covered in a later chapter. Really frustrating! So started looking for videos on this topic, and found your channel. BTW, is there any way to do addition on generics too? Thanks.

  43. Jordan Lea May 11, 2019 at 6:51 pm - Reply

    For the first if statement why not use (b > a). Same idea but much shorter.

  44. Sina Madani May 11, 2019 at 6:51 pm - Reply

    Wow, that's pretty awesome

  45. Esice Java May 11, 2019 at 6:51 pm - Reply

    What does he mean by the compareTo method either returns a -1, 0 or +1, if it less it's going to return a negative number, if both the things it's comparing are equal it's going to return a 0 or if it's greater it's going to return a positive number?

  46. Anastacio Monsivais May 11, 2019 at 6:51 pm - Reply

    Beautiful tutorials

  47. Banawani May 11, 2019 at 6:51 pm - Reply

    I find all of this very confusing previous tutorials weren't that hard to understand but this..

  48. Games. May 11, 2019 at 6:51 pm - Reply

    1:05 can't you just do string.length() > anotherString.length()

  49. Darkwing Dumpling May 11, 2019 at 6:51 pm - Reply

    The fuck is Max tots?

  50. MG Official May 11, 2019 at 6:51 pm - Reply

    Loool , brooo yeah for strings the max isn't about length , but it's actualy about the position of chars for one char example 'b' is bigger than 'a' ect … , if there is two chars like 'az' vs 'ac' , so 'az' is the bigest , for 3 same process compare first value if it's equals then compare second char and it goes like that :D,you can make your own CompareTo it's easy , Good luck

Leave A Comment

*