Java Generics Tutorial

Поділитися
Вставка
  • Опубліковано 5 жов 2024
  • Twitch: / keeponcoding
    Instagram: / keep_on_coding
    Discord: / discord
    My Gear: amazon.com/sho...
    #keeponcoding #tech #programming

КОМЕНТАРІ • 302

  • @KeepOnCoding
    @KeepOnCoding  4 роки тому +18

    ☕Want to master Java? Get my complete Java course: bit.ly/42mQlXE

  • @KnakuanaRka
    @KnakuanaRka 4 роки тому +128

    Also, something else to add about generics:
    If you’re expecting some specific property of the class you put into a generic, you can write that into your generic. For example, if your class had an array of T’s that you wanted to sort, just putting MyClass will make your compiler complain that you’re doing something unsafe.
    This is because you can only use sort() on the array if the class T implements the Comparable interface and its compareTo() method, and there’s no guarantee someone won’t try to make it with some class Java doesn’t know how to compare or sort (like a custom one).
    The solution here? Write your class header as MyClass. This tells the compiler that you will only make MyClass’es with types that can be sorted. This uses “extends” as opposed to the “implements” usually used for interfaces because you’re just making a promise about the generic class, not actually implementing one, and the second T is because Comparable is also generic (so you need to say that T implements a method to compare T objects specifically).

    • @eustachybakielka
      @eustachybakielka 3 роки тому

      It's not because of reification of arrays?

    • @KnakuanaRka
      @KnakuanaRka 3 роки тому +1

      @@eustachybakielka Not sure what you’re referring to.

    • @eustachybakielka
      @eustachybakielka 3 роки тому

      @@KnakuanaRka "For example, if your class had an array of T’s that you wanted to sort, just putting MyClass will make your compiler complain that you’re doing something unsafe"

    • @KnakuanaRka
      @KnakuanaRka 3 роки тому +1

      @@eustachybakielka What the heck is reification of arrays?

    • @eustachybakielka
      @eustachybakielka 3 роки тому +1

      @@KnakuanaRka What is reification in Java?
      In the context of Java as a programming language, reification is the process by which a user program or any aspect of a programming language that was implicit in the translated program and the run-time system, are expressed in the language itself. In Java, there exist "reifiable types" that are "completely available at run time" (i.e. their information is not erased during compilation).
      Example:
      //Allocates an array of type String, so it is reified
      String[] aStringArray = new String[10];
      //Allocates a list with no type, Java does not reify generic types
      List aStringList = new ArrayList();

  • @lindawisebear
    @lindawisebear 4 роки тому +157

    This is the best explanation of Generics I've seen on the internet. I loved how you started with the problem to solve, and then showed how generics is useful when tackling that problem :)

  • @philippebaillargeon5204
    @philippebaillargeon5204 3 роки тому +41

    I like the way you explain things: Java developers created Generics to solve a problem, so I think its pretty logical to talk about the problem before talking about the concepts that were created to solve it. Great video !

    • @mastershonobi110
      @mastershonobi110 2 роки тому +1

      Super great point(illustrate the problem to solve, before the concept). A lot of Java Channels should be taking notes!!

    • @angledcoathanger
      @angledcoathanger 2 роки тому

      Totally agree - it's hard to get excited about a key if you haven't seen the lock that it opens

  • @deeptimonga7375
    @deeptimonga7375 4 роки тому +40

    Keep On Making such videos on Keep On Coding channel. Really Helpful !!

  • @TS-wj4im
    @TS-wj4im 3 роки тому +2

    Dude thank you. My text book way over complicated generics. Now it all makes sense.

  • @dalvandi
    @dalvandi 3 роки тому +6

    Possibly one of the best, clearest, and crispest explanations on the generic class that I've seen! Keep up the great content dude!

    • @ramiayash7658
      @ramiayash7658 3 роки тому +1

      i highly agree with you, the guy is so clear with his explanations

  • @mohannadfadhal4282
    @mohannadfadhal4282 3 роки тому +3

    Thank you so much, Sir. You made the explanation for generics very simplified, especially, by the style of starting with the problem first and then showing the solution! 🤩

  • @abdullahmajed7554
    @abdullahmajed7554 4 роки тому +1

    that was really simple i love the way you edit the video making switch between you and the code it feels so clean, thank you

  • @IgorBeneli1014
    @IgorBeneli1014 4 роки тому +1

    You're incredible you explained with 12min a concept that my professor couldn't do it with 1 hour. Keep doing this amazing job thank you so much!

  • @BoetFly
    @BoetFly 4 роки тому +19

    Man, thank you so much, i finally understand those generics now :D

  • @shagiwan
    @shagiwan 2 роки тому +1

    Second year of studying Software Development in Uni and this is just the best explanation ive heard! Great job man!

  • @conea6891
    @conea6891 3 роки тому +1

    I like this fast paced explanation.

  • @philyeo
    @philyeo 3 роки тому +1

    awesome stuff. I liked the way you explained it - showing the problem first and how generics can help save the day!

  • @JosephGallagher
    @JosephGallagher Рік тому

    This is the most concise and easy to understand video I've seen so far on Generics, thank you so much :D

  • @delmayank
    @delmayank 3 роки тому +1

    Lounge Coding! I really liked the theme. A new touch to coding tutorials. I am your fan now. You rock! Oh! And the stuff that you covered in this tutorial is of great value. You explained it easily and flawlessly.

  •  3 роки тому +2

    Wow! You explained it all so good. I finally feel like I understand this. Thank you so much. Please keep it coming.

  • @abdullahmoiz8151
    @abdullahmoiz8151 4 роки тому +6

    Man this was a fantastic explanation
    I particularly liked how you first showed what the problem was then showed how generics solve it

  • @hannahc8436
    @hannahc8436 2 роки тому

    This was such a great explanation of Generics. I've been so confused about it for the past few weeks. Thank you!!

  • @prabhakarlal4799
    @prabhakarlal4799 4 роки тому +1

    Nice and simplified explanation of a complicated topic. Thank you.

  • @fredericoamigo
    @fredericoamigo Рік тому

    Thank you so much for this! I love your Java tutorials. So simple, clean and explanatory. Keep up the good work!

  • @ravenmadd1343
    @ravenmadd1343 3 роки тому +2

    Why can't everything be explained this clearly and quickly?
    Thanks for doing this and keep up the good work.

  • @shreypatel9379
    @shreypatel9379 3 роки тому

    This was the best explanation on youtube, short and concise. Thank you and keep making such videos

  • @PrakashVl
    @PrakashVl 3 роки тому

    This tutorial is helpful for filling the gaps in generics, thanks for explaining thoroughly.

  • @isah.2980
    @isah.2980 3 роки тому +1

    Thank you so much! Please, keep doing videos like this!

  • @alexmedina5135
    @alexmedina5135 4 роки тому

    what a good, easy, and elegant way to explain generics ... keep it up!!

  • @lazpz617
    @lazpz617 Рік тому

    This video really helped me understand Generics way more than my instructors classes

  • @wovasteengova
    @wovasteengova 3 роки тому +1

    This looks like it's from java, a beginner's guide. I too was confused on what generics fix but thank you for explaining it in the beginning of the video. I think you should do that to all your videos

  • @gauravkumarsingh640
    @gauravkumarsingh640 3 роки тому

    This is legit the best video I could find to understand Generics!

  • @athardaif
    @athardaif 3 роки тому +1

    I was struggeling with understanding generics until I watched your video. Thanks a lot :)

  • @anx007d
    @anx007d 2 роки тому

    Thank You very much .. after looking many videos from different sources ....... this video absolutely cleared my thoughts for Java Generics.

  • @ziekaman2450
    @ziekaman2450 2 роки тому

    Thankyou so much. You're very talented in explaining complicated Java thingys!

  • @himanshushukla787
    @himanshushukla787 3 роки тому

    bro your java playlist is so underrated. I love it

  • @earl0fBronze
    @earl0fBronze 4 роки тому +1

    I read a thirty page chapter on this, one ear out the other. This video is outstanding, mind blown.

  • @morolillyshean8816
    @morolillyshean8816 2 роки тому +1

    Yes, this is the best one I found. Thanks!

  • @101matthias
    @101matthias 3 роки тому +6

    I just watched 9 hours of online classes from my teacher on generics and I understood nothing. Less than nothing. 10 minutes of you and everything is clear it's super easy actually.

    • @technocoh
      @technocoh 2 роки тому

      yeah not every professional or teacher is able to share his knowledge the proper way... ;)

    • @pinolskun8764
      @pinolskun8764 2 роки тому

      @@technocoh yeah if someone need 9 hours to explain a concept you can safely assume they are the first who don't understand it

  • @MrMarkjams
    @MrMarkjams 4 роки тому +17

    This is a great video! Could you do one on Interfaces?

  • @monetoast
    @monetoast 3 роки тому +1

    Exceptional video, very clear!

  • @chinemelumj5349
    @chinemelumj5349 2 роки тому

    Finally understood generics, thank you so much!.

  • @youvegotmail9385
    @youvegotmail9385 Рік тому

    Not sure why it's still taking me a minute to wrap my head around Generics. I'm sure it'll click, but it's so funky to me even with your great explanation.

  • @MrDoctorSchultz
    @MrDoctorSchultz 3 роки тому +3

    Smooth, informative, and straight to the point. Keep up the good work!

  • @19sunheart96
    @19sunheart96 2 роки тому +1

    Nice video! Very easy to understand explanation of the concept.

  • @leonrickert4061
    @leonrickert4061 4 роки тому +2

    Keep on man, cool teaching style

  • @SaljooqAltaf
    @SaljooqAltaf 4 роки тому

    This just might be personal preference but the best and most detailed explanations for anyone studying comp sci are found in this channel - no idea why views haven't hit a million

  • @brownyeet3981
    @brownyeet3981 3 роки тому +2

    Holy shit THANKS!

  • @nicholaschua2300
    @nicholaschua2300 4 роки тому +1

    Thank you for this quick video. You saved me 30 minutes of reading long paragraphs from my lecturers.

  • @leeamraa
    @leeamraa 4 місяці тому

    correction: you should make this function type-bounded as well, like this --> "boolean absEqual(NumericFns

  • @KeepOnCoding
    @KeepOnCoding  4 роки тому +18

    Have you ever used Generics in your code?

    • @Jilnn
      @Jilnn 4 роки тому +1

      nope

    • @Chaz-LeeP
      @Chaz-LeeP 4 роки тому +2

      yea! coming from c++, i was taught that its called templates

    • @ActualVital
      @ActualVital 4 роки тому +2

      Yup. My Data Structures professor would always require us to use them. Still don’t understand them. Hopefully this video will help me understand!

    • @guitarman813
      @guitarman813 4 роки тому +4

      Yes, in my latest Java project. While they seem easy in theory, implementing them is another matter! I hope to gain knowledge from this video and other sources and thus strengthening my performance in this area.

    • @rossocorsa6577
      @rossocorsa6577 4 роки тому +3

      Keep On Coding Yee for my assigment

  • @2010aishwary
    @2010aishwary 4 роки тому +1

    very nice to the point video, subbed

  • @kaifahsan8459
    @kaifahsan8459 4 роки тому +1

    Thanks for the video mate. Pretty well explained.
    Would be awesome if you keep making videos about core and advanced OOP concepts like this.

    • @KeepOnCoding
      @KeepOnCoding  4 роки тому

      Thank you sir. Did you have a specific topic in mind?

    • @kaifahsan8459
      @kaifahsan8459 4 роки тому

      Probably topics like interfaces, polymorphism, exceptions, collection frameworks etc.
      There's a huge Quora thread that might help you as well:
      www.quora.com/What-is-a-complete-list-of-topics-of-Core-Java-and-topics-of-Advanced-Java

    • @KeepOnCoding
      @KeepOnCoding  4 роки тому

      Awesome! Thanks!

  • @tetsuorulin9009
    @tetsuorulin9009 2 роки тому

    I love teaching to the problem. Great video and thanks!

  • @CarlosRamirez-uz7hs
    @CarlosRamirez-uz7hs 3 роки тому

    Great video man, keep doing what you do

  • @saintfrancisbeats9639
    @saintfrancisbeats9639 4 роки тому +1

    Dope! I am currently in an Intermediate Programming college course and they just want us to read a document. I was so lost. This clarified alot. Also it cause me to switch from Netbeans to IntelliJ IDEA hahaha

  • @nikeshparajuli6345
    @nikeshparajuli6345 4 роки тому +4

    it's really helpful,thanks a lot .

  • @abhijeetmokale9024
    @abhijeetmokale9024 3 роки тому

    Seriously this way of learning from the problem is amazing! Thanks Sir!!

  • @RubenGarciaCS
    @RubenGarciaCS 3 роки тому

    Excellent quality! Thank you

  • @AshBeastreal
    @AshBeastreal 2 роки тому

    Wow this is one of the best explainer videos. Loved watching it🤩

  • @wesNeill
    @wesNeill 3 роки тому

    Lol @ the smooth jazz in the background.
    But good content!

  • @olegc6209
    @olegc6209 3 роки тому +1

    Awesome explanation, thanks for your help!

  • @kennethfrederickx
    @kennethfrederickx 2 роки тому +1

    wish my teacher knew how to explain this like u, great video

  • @faikyesilyaprak8761
    @faikyesilyaprak8761 3 роки тому +1

    That was lit bro!!!! Thanks for crystal clear explanation :))))

  • @dablo898
    @dablo898 3 роки тому +1

    Thank you, I learnt something new today

  • @zachhewitt4328
    @zachhewitt4328 4 роки тому +1

    10/10 man! Keep it up. It's better than my current professor!

  • @ledtargaouschi5831
    @ledtargaouschi5831 4 роки тому

    Your videos are very helpful and your teaching methods are very clear

  • @damyandimitrov611
    @damyandimitrov611 3 роки тому +1

    Very well explanation and approach

  • @bibahbibah5108
    @bibahbibah5108 3 роки тому +1

    i like the u develop the subject, uake java very easy to work with

  • @nisarullah7734
    @nisarullah7734 3 роки тому +1

    Great work keep it up

  • @furioustodd5105
    @furioustodd5105 4 роки тому +3

    I have always found Generics a hard topic. Your explanation with code was great. Huge thumbs up 👍🏻

  • @leslyp.blaise7238
    @leslyp.blaise7238 3 роки тому

    Explanation clear and concise thank you

  • @tolstoievski4926
    @tolstoievski4926 2 роки тому

    You can also declare generic type at method scope id you're planning to use static methods (methods that are not requiring you to instantiate class to use it)

  • @mattrazol
    @mattrazol 4 роки тому +1

    Hello there!
    In the last example.. what about if, instead of using the question mark (therefore a generic object), you declare another generic type (let's say V) that also extends Number? Something like this:
    boolean absEqual(NumericFns num) {
    ...
    }
    Therefore you can ensure that for example a string (or any other object that doesn't extend Number) wont be passed, and potentially break your code?
    I'd be glad to hear your opinion on it. 😃

    • @gaspartomi971
      @gaspartomi971 4 роки тому

      Late answer, but here it is! What you suggest is unnecessary, since the class NumericFns can only be constructed with a number class argument anyways.
      So that parameter has a ? as argument, thats true, but it is impossible for that ? to not be a number (NumericFns constructor wont allow it).

  • @anithachowdary9716
    @anithachowdary9716 4 роки тому

    I understood the topic very well.Thank you so much Please share more videos

  • @GeorgeKonstantinou-v2i
    @GeorgeKonstantinou-v2i 3 роки тому

    well thank you for explaining to me what i didn't learn from the class.

  • @sultanrecords1362
    @sultanrecords1362 3 роки тому +1

    Very Nice Explanation

  • @fahadtahir6
    @fahadtahir6 4 роки тому

    Very informative and loved your communication style

  • @toniiicarbonelll287
    @toniiicarbonelll287 3 роки тому +1

    MAAAAAAAAAAAAAAAAAN that was just awesome thank you very much!

  • @ritexcorp
    @ritexcorp Рік тому

    That was a great explanation of Generics, keep up the good work, I just subscribed to your channel. Do you have something on Java annotations?

  • @davidxdhloqud
    @davidxdhloqud 2 роки тому

    I'm spanish speaker, your videos are sooo good than I can understand, thank you

  • @999loaderu
    @999loaderu 3 роки тому

    really well explained m8. Appreciate it. thx

  • @porkfreegaming5278
    @porkfreegaming5278 2 роки тому +1

    You are awesome! thank you good sir

  • @89pravinb
    @89pravinb 3 роки тому

    Thank you for creating this video! Very concise and informative

  • @xxanton8xx
    @xxanton8xx 4 роки тому

    You look like you're gonna fall asleep the next second, but you explained the topic very well!

  • @iRoseRiyadh
    @iRoseRiyadh 4 роки тому +1

    Best java tutorial

  • @ItsTheDannyShow
    @ItsTheDannyShow 4 роки тому +1

    Subscribing man. You helped so much.

  • @quikel7045
    @quikel7045 2 роки тому

    Watched 2 videos, had it at uni, read documentation and still didn't understand why Wildcards are useful. You basically explained it in 2min with a simple example. Thank you very much.

  • @shecodesthings3978
    @shecodesthings3978 3 роки тому

    Ah, another example of my previous compsci professors failing to teach something fairly straightforward... *facepalm*
    You, on the other hand, taught it in such a practical and applicable manner: Discuss a simple problem and then propose a solution (and actually working through the example)
    Super easy to remember too. Great video!

  • @pratikthorat3480
    @pratikthorat3480 2 роки тому

    I know what generics are but I still feel I learnt a few new things here. God Dayummm you are awesome.

  • @beatakazmierczak2543
    @beatakazmierczak2543 2 роки тому

    Simply explained! Thank you!

  • @himangshuchowdhary4369
    @himangshuchowdhary4369 2 роки тому

    Great explanation KOC!

  • @VishnuSunilkumar
    @VishnuSunilkumar 3 роки тому

    perfect. That deserves a thumbs up.

  • @BBlackSmitHH
    @BBlackSmitHH 2 роки тому

    Him:
    *pressing light switch while clicking fingers*
    lol

  • @kylemanley915
    @kylemanley915 4 роки тому

    Sweet tutorial, just subscribed

  • @lurkhidden9126
    @lurkhidden9126 3 роки тому

    Learned hella! Good looks my guy

  • @donwinston
    @donwinston 3 місяці тому

    You didn't cover generic methods and the parameter type lists before the returned type in a method definition. I've been using generics for years and just learned this today! Don't have to use this when you're just a consumer of generic classes.

  • @chikken007
    @chikken007 4 роки тому +1

    Great explanation! Just gained a new Sub. Thank you and keep it up please!

  • @paulocabelloacha4195
    @paulocabelloacha4195 2 роки тому

    Magnific expanations, thank you man!

  • @TKollaKid
    @TKollaKid 2 роки тому

    Great explanation - clear and helpful. Thank you.

  • @arnabmukherjee5840
    @arnabmukherjee5840 4 роки тому +1

    Awesome tutorial bro

  • @okikiomisande8732
    @okikiomisande8732 2 роки тому

    Awesome explaination, thank you!

  • @premalupadhyay3555
    @premalupadhyay3555 3 роки тому

    Great and simple explanation, I like this way of teaching a lot.