Records In Java - Full Tutorial - The Best New Java Feature You're Not Using

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 เม.ย. 2022
  • How to use Records in Java!
    Complete Java course: codingwithjohn.thinkific.com/...
    Records are a new concept added to the Java language as a preview feature in Java 14, and in a full release in Java 16. In this intermediate Java programming tutorial, we will talk all about when you would want to use Java records, and exactly how to do it.
    If you're confused by records in Java, you're in the right place!
    Learn or improve your Java by watching it being coded live!
    Hi, I'm John! I'm a Lead Java Software Engineer and I've been in the programming industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java learners.
    Let me know what else you'd like to see!
    Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
    📕 THE best book to learn Java, Effective Java by Joshua Bloch
    amzn.to/36AfdUu
    📕 One of my favorite programming books, Clean Code by Robert Martin
    amzn.to/3GTPVhf
    🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
    www.audibletrial.com/johnclean...
    🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
    bit.ly/3QPNGko
    📹Phone I use for recording:
    amzn.to/3HepYJu
    🎙️Microphone I use (classy, I know):
    amzn.to/3AYGdbz
    Donate with PayPal (Thank you so much!)
    www.paypal.com/donate/?hosted...
    ☕Complete Java course:
    codingwithjohn.thinkific.com/...
    codingwithjohn.com

ความคิดเห็น • 537

  • @Mahmoudalkasem1997
    @Mahmoudalkasem1997 2 ปีที่แล้ว +74

    The best person on TH-cam that explains java in such a great way

    • @pavelsakun2308
      @pavelsakun2308 2 ปีที่แล้ว +2

      If you didn't see anyone better doesn't mean he is the best ;-) He is good anyway.

    • @Mahmoudalkasem1997
      @Mahmoudalkasem1997 2 ปีที่แล้ว +6

      @@pavelsakun2308 this is my opinion and l am free to think whatever I want right ;)

  • @keionvergara7608
    @keionvergara7608 2 ปีที่แล้ว +89

    you were the reason i got a 5 on my ap comp sci A exam!! you truly have impacted my education and taught me more than my highschool teacher did in 5 months !

    • @CodingWithJohn
      @CodingWithJohn  2 ปีที่แล้ว +8

      Awesome, and congrats!

    • @Reichstaubenminister
      @Reichstaubenminister ปีที่แล้ว +5

      Funny, 5 would be an E in Germany. Had to think about that for a second.

    • @justinliu7788
      @justinliu7788 11 หลายเดือนก่อน

      It’s 5/5

    • @ItIsJan
      @ItIsJan 10 หลายเดือนก่อน

      ​@@Reichstaubenministersprach der reichstaubenminister

  • @Nanagos
    @Nanagos 2 ปีที่แล้ว +9

    I recently found out about Records but there are just no good tutorials about it. I'm glad you made one now.

    • @CodingWithJohn
      @CodingWithJohn  2 ปีที่แล้ว +1

      Thanks! Yeah there's surprisingly little out there so far. Probably just because it's pretty new.

  • @libertymedicalcommunicatio4908
    @libertymedicalcommunicatio4908 2 ปีที่แล้ว +155

    Congrats on almost reaching 100K. This one should put you over the top.

  • @GreatProgrammerHsin
    @GreatProgrammerHsin 2 ปีที่แล้ว +18

    please never stop making videos, these are so helpful!

  • @arnddeshives147
    @arnddeshives147 2 ปีที่แล้ว +17

    There is a lot of content like this out there, but you are so much better than the rest in terms of tempo, order, quality and presentation, thank you so much! I started studying CS half a year ago and your videos are 5x quicker at explaining the same thing than my prof. Although sometimes there is some depth missing, but it's good that your videos don't get lost in that, that's what makes it so good. Maybe it would be great if there was a playlist for in depth knowledge as well though.

  • @leeamraa
    @leeamraa 2 ปีที่แล้ว +13

    Quality, effective, efficient and much appreciated content.

  • @huskydreaming
    @huskydreaming 2 ปีที่แล้ว +10

    I am advanced in java but it is great to see someone explain it so concise. Amazing explanation on these types of videos. Keep it up!

  • @iCybqr
    @iCybqr 2 ปีที่แล้ว +36

    Could you do a video on Consumer, Predicate, and Supplier? I mostly understand what they do, but not when to use them.

    • @YtubeUserr
      @YtubeUserr 6 หลายเดือนก่อน +2

      if you are reading this John, please make this video happen! Thanks!

  • @pradiptaspointofview1475
    @pradiptaspointofview1475 2 ปีที่แล้ว +2

    Explained quite clearly with detailed examples. Excellent tutorial! This is what I needed to start working with Records in Java.

  • @sijanbhandari7569
    @sijanbhandari7569 2 ปีที่แล้ว +1

    Thanks for teaching me more about records, I was always teaching my fellow programmers about this in my workplace.

  • @shankar7435
    @shankar7435 ปีที่แล้ว

    Never thought learning Java would be such a pleasure. A decade ago, I was frustrated with the Java Reference book and left learning java and continued with my day job in DB language. Now, I am learning java without any issues with the help of youtube. Thanks to the youtube community.

  • @simonrozendal7542
    @simonrozendal7542 2 ปีที่แล้ว

    Hi John. Even when i write Java programs quite a while, i still learn new thing from you. Thank you, you are presenting good Java content. Kind regards, Simon Rozendal.

  • @BkSMedia
    @BkSMedia ปีที่แล้ว +4

    I've been learning Java in my CS degree for the last three years, I had no idea records were a thing!!! So cool!

    • @CodingWithJohn
      @CodingWithJohn  ปีที่แล้ว

      Yep! They're a relatively recent addition.

  • @eperrone
    @eperrone 2 ปีที่แล้ว

    Not a Java developer but I have to read Java and Groovy all the time. Your videos are great background to help me understand what I’m looking at. Great stuff.

  • @MrLuMax5
    @MrLuMax5 2 ปีที่แล้ว

    Great video. Especially for those who know the basics and are interested in new Java Features after J11. Keep these up!

  • @Markki28
    @Markki28 2 ปีที่แล้ว +1

    awesome as always, thank you for sharing man, I am familiar with records but I'm not aware that I can do specific field validations directly into it, that's very useful thanks man

  • @mastershonobi110
    @mastershonobi110 2 ปีที่แล้ว +3

    Thanks John!!! Always looking for ways to improve and writing clearer/better code.

    • @mastershonobi110
      @mastershonobi110 2 ปีที่แล้ว

      Side Note…are you using Java 17 or 16? B/c “records” is not supported in Java 11.

  • @bischoffdev
    @bischoffdev 2 ปีที่แล้ว

    I thought I knew all about records (and I am using those extensively) but this is the first time I heard about the compact constructor! Thanks a lot!

  • @sameersafi6128
    @sameersafi6128 2 ปีที่แล้ว +7

    The way of teaching is totally different.. thank you so much for the deep knowledge that you are sharing... kindly make tutorial on stream api in future

  • @pclclau
    @pclclau 2 ปีที่แล้ว +82

    At 5:00 you mention that Java generates a default, no-arg constructor *even if* you don't create other constructors. That isn't quite true, Java creates a no-arg constructor *only if* you don't create other constructors. This is why your fields get red underlines as soon as you manually create the no-arg constructor, because there would be a way to have final fields that aren't initialized as soon as the object is created. Before you manually created the no-arg constructor this wasn't a problem, because Java wouldn't generate one since you already had the previous one created.

    • @eufraniodiogo50
      @eufraniodiogo50 2 ปีที่แล้ว +8

      That's absolutely true, maybe it was just a runtime exception(subclass of Exception) at his speech. lol

    • @KaraSuraDraw1
      @KaraSuraDraw1 2 ปีที่แล้ว +3

      I don’t get what you mean. I feel like your comment just justified what he said. Or do you think he meant even if you have other constructors Java always generates a default no-arg constructor? I think he just put it there for visuals of a no-arg constructor. Because i got that what you said from the video.

    • @pclclau
      @pclclau 2 ปีที่แล้ว +8

      @@KaraSuraDraw1 He said that Java provides a no-arg constructor no matter what constructor you already have definded. That's not true.

    • @alfasolarenergy
      @alfasolarenergy 2 ปีที่แล้ว +2

      When you create an object of the Class two things happes: 1. MEMORY IS ALLOCATED, 2. THE CONSTRUCOR IS CALLED. By default Java offers a 0 argument construcotrs which are 0 for numeric types, and false for boolean and null. The moment we create our impemtation of constructor ( a constructor which initialize fields) the 0 arg one is no longer called.

    • @TechiiEngineer
      @TechiiEngineer 2 ปีที่แล้ว +1

      True , if you have created just an args-constructor ;
      You cant created object as : new Employee();
      That means Employee class doesn’t have no arg constructor ;
      You have to create one.

  • @nischalofchrist
    @nischalofchrist 2 ปีที่แล้ว +1

    Thank you, I've always wanted to learn about Records.

  • @randomuserrandom8613
    @randomuserrandom8613 2 ปีที่แล้ว

    Thank you so much. This was very helpful and the way you explain things makes it so easy to understand them. Keep up the good work, John!

  • @RamGorre
    @RamGorre 2 ปีที่แล้ว +1

    That's an awesome no BS explanation. Too good.

  • @katisbetter2952
    @katisbetter2952 2 ปีที่แล้ว +1

    Keep up with the good work, you’re helping a lot of people 🙏🏻

  • @ccmawil20
    @ccmawil20 2 ปีที่แล้ว

    Explanation is clean and clear. please publish more videos like this. Thanks

  • @antonkimberg8914
    @antonkimberg8914 2 ปีที่แล้ว +2

    Hi John,
    Thank you for your superb content. Me and other group of people from Estonia are learning Java and your channel helps us to understand hard material in a simple way.
    Could you please make a video tutorial about streams in future?
    Thank you in advance,
    Tony

  • @jimjimjimjimjim6572
    @jimjimjimjimjim6572 2 ปีที่แล้ว

    Nice pace, and super clear, cheers my man

  • @olivierbegassat851
    @olivierbegassat851 8 หลายเดือนก่อน

    Your exposition is always so direct and simple. I love it 🙂

  • @emilmathew2257
    @emilmathew2257 2 หลายเดือนก่อน

    Have been following your videos for a while now. Thank you for the awesome content 😊

  • @chenchi6623
    @chenchi6623 7 หลายเดือนก่อน

    I like the style of teaching, very clear examples and no bs, earned a sub...

  • @mohamedsaleh5970
    @mohamedsaleh5970 2 ปีที่แล้ว

    You helped me understand concepts that was difficult to understand on my own.

  • @maneshipocrates2264
    @maneshipocrates2264 2 ปีที่แล้ว

    Thanks alot John. This talk should be included in the official doc.

  • @Jody-nf2bz
    @Jody-nf2bz ปีที่แล้ว

    Very well explained, readable size text, excellent class! Thank you!

  • @ayberkctis
    @ayberkctis 2 ปีที่แล้ว +1

    Hey John!
    If i knew Records before, i would use them in my previous task in my company because they fitted my task perfectly!
    Thank you for your effort :)

  • @longtran12345678
    @longtran12345678 2 ปีที่แล้ว +1

    your videos really help me to focus on Java again :)

  • @lucashenrique6076
    @lucashenrique6076 2 ปีที่แล้ว +1

    Thanks for the Video John! Greetings from Brazil 😉

  • @PratikKumar42
    @PratikKumar42 2 ปีที่แล้ว +1

    Great video, these really helps alot.

  • @AbdelhameedGamal-ki7th
    @AbdelhameedGamal-ki7th 5 หลายเดือนก่อน

    So simple and clear explanation. I really appreciate your help.

  • @hunelysium
    @hunelysium ปีที่แล้ว

    Hello John,
    it's always great to listen to your explanations on Java! Thank you for your valuable wisdom!!
    On a side note, noticed your voice and the way you talk has changed since you become a father :)
    Hope you and your family well during this different times!
    Keep making thus great videos, always love watching them!

  • @argonwheatbelly637
    @argonwheatbelly637 ปีที่แล้ว

    Guitars 🎸, drums 🥁, and programming. 💻 Yep! You're definitely part of my tribe. Excellent video! Keep it up.

  • @mustapharaimilawal8053
    @mustapharaimilawal8053 2 หลายเดือนก่อน

    Thanks a lot for doing this tutorial. Clear, easy to follow explanation as always.

  • @jaimesastre6393
    @jaimesastre6393 2 ปีที่แล้ว

    really cool!!!
    Spring had some simplification,
    but here it's much better and has also interesting options!
    Thanks 👍👍

  • @JorgetePanete
    @JorgetePanete 2 ปีที่แล้ว

    Perfect tutorial, much appreciated!

  • @bacon37460
    @bacon37460 2 ปีที่แล้ว +1

    Nice feature, thanks John for sharing it with us.

  • @sandunicula6185
    @sandunicula6185 2 ปีที่แล้ว +1

    Great presentation! Thanks!

  • @RicardoSilvaTripcall
    @RicardoSilvaTripcall 2 ปีที่แล้ว

    Great explanation !!! Congrats ....

  • @chacha-bm4jc
    @chacha-bm4jc 2 ปีที่แล้ว +2

    Thanks a lot John! Would be there any video about sealed classes? It is also quite cool feature

  • @jaspreet821
    @jaspreet821 8 หลายเดือนก่อน

    Great episode my man!! 😮😮

  • @haidaraibrahim9195
    @haidaraibrahim9195 2 ปีที่แล้ว

    I've watched all of your videos and they r really really helpful and easy to understand, seriously.. u're an amazing teacher and I wish all success for u and ur channel.
    Small demand: I'd like to learn about Streams in ur way the next tutorial, if that's fine, however, any tutorial u make wod be awesome.
    Thx man, you're LaJend (J refers to "Java legend"🙂)

  • @Naimadmdp
    @Naimadmdp ปีที่แล้ว

    Nice content mate, thanks for share!!!

  • @cverde1234
    @cverde1234 2 ปีที่แล้ว +6

    This is really similar to records in C#. We don't have compact constructors though. I think compact constructors are really neat.

  • @fazithfouseen1548
    @fazithfouseen1548 2 ปีที่แล้ว +6

    Hi John, Please upload Microservices and spring boot tutorials as well

  • @tuna5395
    @tuna5395 ปีที่แล้ว +1

    Thanks bro you helped with my project !

  • @alexornwara3182
    @alexornwara3182 2 ปีที่แล้ว +1

    I cannot believe I completed many courses and projects in Java without using this 😔🤯 My goodness, think of the time I could've saved! Thank you for making this video. It's so engaging, informative, and very easy to understand.

    • @yaroslavpanych2067
      @yaroslavpanych2067 หลายเดือนก่อน

      That means, that this stuff is not really important. It is not a unique cure to any problem you had.

  • @budiardjo6610
    @budiardjo6610 4 หลายเดือนก่อน

    after i am learning about memory management, stack and heap and jvm internals i could really understanding this properly what a powerfull feature

  • @RahulSingh-qe4sw
    @RahulSingh-qe4sw ปีที่แล้ว

    Thank you so much , explaination was very nice.

  • @davidfernandotorres3484
    @davidfernandotorres3484 2 ปีที่แล้ว

    Amazing bro! Thanks for the info

  • @Atom-Mercury
    @Atom-Mercury 10 หลายเดือนก่อน

    Very well explained. Thank you kindly.

  • @WisomofHal
    @WisomofHal 2 ปีที่แล้ว

    Wow, awesome video! There are many ways I can think of to use Records.

  • @KarolKasperek
    @KarolKasperek ปีที่แล้ว

    u have the best java keyword's tutorials, ty

  • @andres98
    @andres98 2 ปีที่แล้ว

    I was waiting for this video so badly :))

  • @B-Billy
    @B-Billy 11 หลายเดือนก่อน

    That's a really good teaching... Thanks 🎉

  • @matiasmoresi5040
    @matiasmoresi5040 ปีที่แล้ว

    Mindblown by this feature!

  • @shivk498
    @shivk498 2 ปีที่แล้ว

    Java is usually known for its cumbersome syntax but such awesome improvements like records would make java lovers more excited.

  • @anantharamu80
    @anantharamu80 2 ปีที่แล้ว

    Love this. Very informative.

  • @frankjuuh
    @frankjuuh 2 ปีที่แล้ว

    Love you, John. Best teacher.

  • @raz0229
    @raz0229 2 ปีที่แล้ว +3

    You are so underrated! Hope you reach 1 mil subs soon

  • @ChristienChapman
    @ChristienChapman 2 ปีที่แล้ว

    Good tutorial. I will use this from now on, much more simple way to do the same thing. God bless you.

  • @tanacing347
    @tanacing347 หลายเดือนก่อน

    Gracias John!!!

  • @evagor730
    @evagor730 ปีที่แล้ว

    Wonderful explanation! Thank you! Shorty and clear)

  • @ting-yuhsu4229
    @ting-yuhsu4229 2 ปีที่แล้ว

    This is amazing! Thank you :D

  • @MortenSlottHansen
    @MortenSlottHansen 11 หลายเดือนก่อน

    Nice and precise explanation without any fuzz 🎉

  • @danigonzalez6231
    @danigonzalez6231 2 ปีที่แล้ว

    Thank you sooooo much!

  • @AHMED-ry2gy
    @AHMED-ry2gy 2 ปีที่แล้ว

    Hey John, Congrats on reaching 110K, please do videos on java streams, java sockets, Mockito and MVC in the future, thank you

  • @MrBrackets
    @MrBrackets ปีที่แล้ว

    great video, thank you!

  • @TakeshySun
    @TakeshySun 2 ปีที่แล้ว +5

    @
    Coding with John , Hi, could you please record a video about Java 8 features like Optional and Streams. It will be so wonderfully. And big thank you for short and really useful videos!

  • @ayed-al-nator
    @ayed-al-nator ปีที่แล้ว

    Beautiful video 💚 thank you 😊

  • @DevPieter
    @DevPieter 2 ปีที่แล้ว

    This is sick! I wanted to find this sooner 😥

  • @givemepepsipls536
    @givemepepsipls536 2 ปีที่แล้ว

    he best person on TH-cam that explains java in such a great way

  • @bouaddiismail
    @bouaddiismail 2 ปีที่แล้ว +1

    great lesson as always , grettings from Morocco

  • @kirankumarsukumar
    @kirankumarsukumar ปีที่แล้ว

    Omg. You are so amazing at teaching. Thanks Master Yoda

  • @user-qb3ck7cq3k
    @user-qb3ck7cq3k ปีที่แล้ว

    Thank you brother!

  • @guestmode867
    @guestmode867 2 ปีที่แล้ว +1

    Tip: If you want to store mutable objects in records:
    public record EmployeeRecord (java.util.concurrent.atomic.AtomicReference name, int employeeNumber) {} /* Here, _only the value of AtomicReference name_ (a String) is mutable; the value can be changed with employeeRecord.name().set("John"); and can be gotten with employeeRecord.name().get(); */
    (or)
    import java.util.concurrent.atomic.*;
    public record EmployeeRecord (AtomicReference name, AtomicInteger employeeNumber) {} /* Here, _both_ the values stored by name and employeeNumber (String and _int_ respectively) are mutable. The value of employeeNumber can be changed by employeeRecord.employeeNumber().set(x); (x must be an int or an Integer (if it is an Integer, it will be unboxed)) and can be gotten with employeeRecord.employeeNumber().get(); */
    /* FAQ: employeeRecord.employeeNumber().get() returns a variable of type int (may autobox to Integer). If employeeNumber is to be directly of type Integer, it should be declared as java.util.concurrent.atomic.AtomicReference employeeNumber . */

  • @raoufbenabdelwahed6362
    @raoufbenabdelwahed6362 ปีที่แล้ว

    well explained as usual !

  • @electricant55
    @electricant55 2 ปีที่แล้ว +43

    Except for that neat overriding constructor, it seems like Lombok is still a more useful option, unless you can put annotations on the fields declared in record for things like JPA or JSON serialization

    • @Darya-pu6ik
      @Darya-pu6ik 2 ปีที่แล้ว

      what is Lombok?

    • @ThiagoHenriqueDS
      @ThiagoHenriqueDS 2 ปีที่แล้ว +14

      @@Darya-pu6ik It's a library that allows you to use annotations in order to create things like getters, setters, equals methods, etc.
      For example, instead of write getters and setters you just use the annotation @Data, @Getter or @Setter above a constructor:
      @Getter
      @Setter
      public Car(String color, String name) {
      }

    • @Darya-pu6ik
      @Darya-pu6ik 2 ปีที่แล้ว

      @@ThiagoHenriqueDS thanks, looks helpful!

    • @shivk498
      @shivk498 2 ปีที่แล้ว +3

      Lombok is indeed very useful but it still a third party plugin. Java is usually known for its cumbersome syntax but such awesome improvements like records would make java lovers more excited.

    • @pavelsakun2308
      @pavelsakun2308 2 ปีที่แล้ว +1

      Seeing comments like this makes me think other devs are spending a lot of time to generate POJOs/DTOs and that's hardly true. That kind of objects is being written once and the rest 99+% of time is spent for writing business logic, so even with what vanilla java and IDEs are providing I do not see HUGE problem of implementing them without record/lombok. The only exception here is readability, but...do you really often open classes named '*DTO" for reading? I really doubt.

  • @yansoe2191
    @yansoe2191 ปีที่แล้ว

    Hay John,
    Thank you for the great content. You really simplify this. Do you mind covering Executor Service (Multithreading)

  • @sovrinfo
    @sovrinfo 2 ปีที่แล้ว

    Thank you a lot!

  • @overrim13
    @overrim13 2 ปีที่แล้ว

    Thank you for explaining some unexplored topic

  • @gasgeek434
    @gasgeek434 2 ปีที่แล้ว

    thank you so much for your videos

  • @BillyCha00
    @BillyCha00 2 ปีที่แล้ว +1

    Hi John, you are the best!! please upload Microservices and spring boot tutorials :)

  • @vladimirspasov5591
    @vladimirspasov5591 2 ปีที่แล้ว +1

    Great explanation

  • @jamfamily4287
    @jamfamily4287 ปีที่แล้ว

    This is an extremely useful and time saving feature!

  • @kesavenvulliamay4637
    @kesavenvulliamay4637 2 ปีที่แล้ว

    Very Informative

  • @christianrodier3381
    @christianrodier3381 2 ปีที่แล้ว

    That is very useful!

  • @hbfl3x50
    @hbfl3x50 ปีที่แล้ว

    Very informative 👏

  • @darkworld8544
    @darkworld8544 2 ปีที่แล้ว +1

    Hi John thank Q for ur valuable information sharing love from India 🇮🇳

  • @yonneltg1150
    @yonneltg1150 2 ปีที่แล้ว

    Perfectly explained.

  • @Wallasmod
    @Wallasmod 2 ปีที่แล้ว +1

    Excellent video

  • @kermosaarse
    @kermosaarse ปีที่แล้ว

    That's similar to class declaration in Scala, except that there you can simply specify var or val in front of field name to make it either mutable or immutable. Or make it a case class with immutable fields by default. But in Scala, there is no need for that "compact constructor" because you can write any validation code directly inside the class body.