Optionals In Java - Simple Tutorial

  Рет қаралды 220,670

Coding with John

Coding with John

Күн бұрын

Пікірлер: 452
@stevenschelling8452
@stevenschelling8452 2 жыл бұрын
This is the gold standard of coding tutorials, thanks for such well-structured info
@iamarnabpal
@iamarnabpal Жыл бұрын
Seriously... Pure GOLD!
@YatiAcharya
@YatiAcharya Жыл бұрын
You know how I am certain this is the best Java channel? I have no interviews to crack, I have no exams to clear and I'm still here learning because it just is explained in such an interesting, problem-solution kinda way. Brilliant work, John!
@Amitjha406
@Amitjha406 Жыл бұрын
Agreed
@BryanChance
@BryanChance Жыл бұрын
Yes, I second that. This is the best Java channel. :)
@nikamz6077
@nikamz6077 2 жыл бұрын
My coding exam is in just about a month - and I would like to thank you, John! You have helped me so much. If I pass, thats because of you. Love your channel! Greetings from germany. :)
@nikamz6077
@nikamz6077 2 жыл бұрын
Update: I passed. 😍💪🏻
@celeryman1650
@celeryman1650 2 жыл бұрын
Congrats!!
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
Great to hear! Congrats!
@MarynaSapiton
@MarynaSapiton Жыл бұрын
@@nikamz6077 Congrats!
@ItIsJan
@ItIsJan Жыл бұрын
​@@nikamz6077gut gemacht!
@zaph8015
@zaph8015 Жыл бұрын
Null safety is one of the reasons I fell in love with Kotlin. It's caught me out so many times and helped me to think about where NPEs might be found even when working in other languages.
@whiterabbit9310
@whiterabbit9310 5 ай бұрын
Dude, I can say for sure that your way of explaining is the best I've seen on KZbin ever. That's considering that I'm not a native speaker. Thank you a lot.
@Cloydz
@Cloydz 2 жыл бұрын
i just started my internship for the summer and the code they have us working with has a ton of optionals, which was something i had never heard of. You’re videos are truly always a highlight to my day and explain it so neatly and concisely, not to mention the timing! please keep doing what you do
@wellingtonaraujo5418
@wellingtonaraujo5418 11 күн бұрын
after some years i still get myself drinking coffee and watching your videos, awesome content
@LightningLion500
@LightningLion500 2 жыл бұрын
I just started watching your channel and I love your calm and reassuring way of explaining complex coding aspects! It motivated me to work a little harder on my own projects, unlike the often condescending explainations of the teachers at my university. Thanks John!
@midhunmadhu5305
@midhunmadhu5305 2 жыл бұрын
I don't usually comment on any youtube videos. But here John, You are doing an excellent job describing topics in a simpler manner. Thank You.
@sg4644
@sg4644 Жыл бұрын
I've a exam on Java tomorrow and I've to say your videos on Java are probably the best on KZbin. Will be binge watching the your entire Java playlist tonight.
@gulbalasalamov1367
@gulbalasalamov1367 2 жыл бұрын
Love the cat example and box metaphor! :) 4:45
@jpp855
@jpp855 4 күн бұрын
Reality: Optional is just workaround of a big and unsolved problem in Java language.
@jaylawlmc1915
@jaylawlmc1915 2 жыл бұрын
THANK YOU SO MUCH for actually explaining when are where optionals are actually useful as opposed to using a return annotated with "@Nullable". I am lucky to have understood this topic before watching your video, but before i got to that point i was *severely struggling* to get behind why i would use Optional instead of null in the first place. That is due to many tutorials on optionals just leaving things at "it's a container where a value may or may not be present" but not going as far as you did. I'm absolutely certain this video of yours will help tons of people!
@pauldhartley
@pauldhartley 2 жыл бұрын
I must confess to still being confused at the usefulness of optionals, though the video explains admirably some of the characteristics.
@jaylawlmc1915
@jaylawlmc1915 2 жыл бұрын
@@pauldhartley cleaner and more functional code. Try working with streams for example. You cannot unlock the full power of streams using null checks
@jaylawlmc1915
@jaylawlmc1915 2 жыл бұрын
@@pauldhartley I point out functionality because you cannot much with a null value, other than check it and pass it. Optionals on the other hand, are integrated into javas core functionality. You can do tons of things with them just with what java provides. That doesn't even take into account what you or others may code for it
@mayankgupta9277
@mayankgupta9277 Жыл бұрын
Can't resist writing a review, I am preparing for a job as a developer and chose Java as my technology, I have been learning since 4 months now, I was completely devastated and lost few days back as I am not getting any calls for the interviews, luckily discovered your channel and since then I have been going through all the concepts again from your videos, it's just making me so much more confident and stronger, your energy is just pure GOLD! All the topics have been explained crisply. Hope I'll soon land a good offer! Lots of love from India! ❤❤❤❤
@mayankgupta9277
@mayankgupta9277 Жыл бұрын
Late Update: I am thrilled to announce that one month ago, I joined as a Java Developer in one of the best companies out there. Everything is perfect; my new job fulfills everything I have been looking for, and it feels like a dream 😍. Many thanks to John for his positive energy, knowledge, down-to-earth attitude, and all the help during my journey. I owe this to you John. Thanks to God and everyone for believing in me.
@juanantoniosicardocontrera8390
@juanantoniosicardocontrera8390 10 ай бұрын
It's very difficult to find such well explained, well paced and easy to understand coding tutorials on youtube, thanks and congrats for having such good content.
@RsZ789
@RsZ789 2 жыл бұрын
Great job, John. Can you please cover logging and best practices for it?
@RotatingBuffalo
@RotatingBuffalo 2 жыл бұрын
This is an example of a monad, in case anyone here has heard of monads but doesn't know what they are useful for yet. They generally provide a "Wrapper type" that you can use to chain operations safely on, without having to do intermediate work between every function/method.
@AlamMahmood
@AlamMahmood 10 ай бұрын
As a kinesthetic learner I would like to thank you for bringing the actual cat for this videos which was Optional
@gamingislove3030
@gamingislove3030 Жыл бұрын
Best video i ever saw on Optional.🎉🎉
@hashcodehub6717
@hashcodehub6717 2 ай бұрын
love all of your content , it is clear precise and easy to consume i was thinking that it was really really tough but now i am very much comfortable with optionals
@pastasempa
@pastasempa 2 жыл бұрын
Hi John, I had an interview for SDE 3 at Walmart today, and I literally was able to answer everything along with proper Implementation, knocked it out of the park! Your tutorials are very brain-friendly, which is rarity nowadays :)
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
Awesome, thanks for sharing! Hope you get a great offer!
@satioOeinas
@satioOeinas Жыл бұрын
did you get the offer bro?
@CodingWithJohn
@CodingWithJohn Жыл бұрын
rip @pastasempa
@pastasempa
@pastasempa Жыл бұрын
@@CodingWithJohnl got an amazing offer and bought a nice cruiser motorcycle with that paycheck!!
@CodingWithJohn
@CodingWithJohn Жыл бұрын
Hey that's amazing, congratulations!!
@gerdsfargen6687
@gerdsfargen6687 2 жыл бұрын
So good..guys, just keep coming back to John's channel. He explains everything with the greatest of ease.
@RealNikolaus
@RealNikolaus Жыл бұрын
Always wondered what the true benefit of optionals vs null checks is. This really cleared things up for me!
@viswanathsairam5956
@viswanathsairam5956 2 жыл бұрын
Optionals demystified with your video. Thanks a lot for this precise explanation
@christianolsson8644
@christianolsson8644 2 жыл бұрын
What an excellent video! I have studied Java but not worked with it and now I try to polish my knowledge to get a job in Java. I want to thank you for your easy to understand and amazing videos that take up very important and interesting topics. You're an amazing teacher and you get excited to learn more to look at your videos :)
@mohamedlegtibi802
@mohamedlegtibi802 Жыл бұрын
Best teacher so far. Greetings from Morocco 🇲🇦
@MariaBose-ky9jf
@MariaBose-ky9jf 2 жыл бұрын
I love your channel for the calm and precise manner you describe things with - and also the nerdy references ahah
@jaswdrTheDev
@jaswdrTheDev 5 ай бұрын
Optionals are extremely useful and explicitly and semantically tell the caller to deal with nulls 😊
@ramkumar_balu
@ramkumar_balu Жыл бұрын
Perfect and standard video about the Optional.
@g_r_x_v
@g_r_x_v Жыл бұрын
It would have taken me hours to look for and understand this information by myself. Thanks John.
@Ramison273
@Ramison273 2 жыл бұрын
my god, you are the best. i am from brazil and i am learning a lot with you. i love java and i found your channel last month, i am watching all your videos, your english and your explanation is so easy to understand.. thank you 😁😁
@gabrielfono844
@gabrielfono844 Жыл бұрын
this is so amazing as backend engineer, I 100% agree thanks a lot
@sanjayranga2683
@sanjayranga2683 Жыл бұрын
The best possible explanation of Optional . You ve earned a subs and keep up.
@ReBufff
@ReBufff 2 жыл бұрын
Didn't even know what optionals were until now. Thanks!
@tostanco1117
@tostanco1117 2 жыл бұрын
I do recognize the way you speak so clearly.
@alinesinclaire2854
@alinesinclaire2854 Жыл бұрын
epic explanation with the cat in the box it was so funny!
@shakilahmed4647
@shakilahmed4647 2 жыл бұрын
Before this I used to think optional is a mysterious box in Java. Thanks for the video
@miltonezeh5085
@miltonezeh5085 Жыл бұрын
You are the BEST on these streets.
@mdk1983
@mdk1983 9 ай бұрын
A 6'5'' John saying "OR ELSE" to the face is quite a threat 😀 Wonderful and interesting tutorial as always. You're the best John!
@LutfarRahmanMilu
@LutfarRahmanMilu Жыл бұрын
Absolutely brilliant teaching style.
@mohammedalbj4268
@mohammedalbj4268 Жыл бұрын
This is pure gold, thank you so much!
@pebble6248
@pebble6248 2 жыл бұрын
Coding with John deserves more attention.
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
Feel free to share! And thanks for watching!
@victor__425
@victor__425 2 жыл бұрын
Awesome! The society demands explaining all java features basing on this cute cat
@emilywilson7835
@emilywilson7835 Жыл бұрын
This channel brings me so much joy, and taught me so much. Thanks!
@vijaydhanakodi5591
@vijaydhanakodi5591 2 жыл бұрын
you should really teach design patterns. one of the best tutor in the universe.
8 ай бұрын
Thanks for this class, Schrodinger! 4:35
@MusicMan121
@MusicMan121 2 жыл бұрын
Cats definitely love playing with/in Optionals
@rlasc84
@rlasc84 2 жыл бұрын
Nice solution for dealing with null exceptions. So much cleaner than use ifs to check the return value. Hopefully some day java implements de null safe operator like groovy or kotlin. Great video John!
@aeraf
@aeraf 3 ай бұрын
Thank you for explaining the concept
@personalaccount1515
@personalaccount1515 Жыл бұрын
Thanks! Direct to the point, it worth all 16 minutes. If I could give 100 likes I will ask why I can't give 1,000
@chinmayrath8494
@chinmayrath8494 2 жыл бұрын
please keep blessing us with such non trivial concepts. much love !!
@Amitkumar-dv1kk
@Amitkumar-dv1kk 2 жыл бұрын
The alternative is to just add a method signature that throws a null pointer exception, this will convey the message as well.
@dinaadel1672
@dinaadel1672 2 жыл бұрын
your videos are really useful and easy to understand.. and you are explaining lots of interview subjects.
@magadiflo-dev
@magadiflo-dev Жыл бұрын
Excellent explanation, calmly, deliberately and well mastered the subject, thanks for sharing your knowledge, greetings from Peru 🇵🇪
@liliabekuzinaensosense8987
@liliabekuzinaensosense8987 2 жыл бұрын
You are just AMAZING! Please never stop! 💫
@Fnaf-vh9cs
@Fnaf-vh9cs 2 жыл бұрын
this is something new definitely, IL look tomorrow into video and check some exercises. thanks John
@yassineabidarIt
@yassineabidarIt 2 жыл бұрын
Great content thank you. Just for Optional the right use case was the use in the public method rather than the private method.
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
Yep, generally for a private method it's not really a big deal.
@ShermukhammadKarimov
@ShermukhammadKarimov 7 ай бұрын
thanks for the clear explanation
@ahouzimohamed6387
@ahouzimohamed6387 2 жыл бұрын
Hi John thank u for ur efforts , can u please do a tutoriel about the spring and spring boot it will be amazing by u
@muhammetalimutlu6748
@muhammetalimutlu6748 10 ай бұрын
Why is this guy so sympathetic
@pradheeshkumar-v4p
@pradheeshkumar-v4p 23 күн бұрын
Hi John !! Your Java tutorials are compendious , please post videos specific to java development like building REST API , Spring Data JPA , Hibernate , Spring MVC etc.. That would be of a great help. Thank You !!
@OmarMendozaKS
@OmarMendozaKS 2 жыл бұрын
Best "Real life" explanation ever, haha, the cat deserves credit for it!!
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
Fred was a good sport. It didn't turn out exactly like I had imagined if he fully cooperated, but worked well enough.
@juvipana
@juvipana Жыл бұрын
This was awesome, you made me chuckle. Made it really easy to follow, thank you!
@rikkarth_
@rikkarth_ Жыл бұрын
Thank you, this video has helped me immensely.
@Fanchiotti
@Fanchiotti 2 жыл бұрын
Couple of questions: 1) Can you pass a Runnable to the orElse() method? 2) Can Optionals return lists or vectors?
@wikkkkid
@wikkkkid 2 жыл бұрын
You can’t pass a Runnable to orElse unless your Optional is a wrapper for a Runnable type (which would be strange..) Optional is defined as Optional and so can return whatever you define T as, for a list of Person objects you would use Optional however this is a bad practice as in the case of collections if there is no list (I.e no records were found in a data store) you should return an empty instance of that collection. The orElse is defined as “T orElse()” this is always executed even when the Optional is not empty, I.e .orElse(new Person()) will always create the person instance (but if the optional is empty then this will just be disposed of and the GC will do it’s work on the next GC run.. if you want to perform logic if your optional is empty you should use orElseGet which is defined as T orElseGet(Supplier), the supplier is only executed if the Optional is empty. Hope this helps :)
@ginasomara267
@ginasomara267 2 жыл бұрын
Bro life saver. Your videos are amazing.
@cosmoatlas2435
@cosmoatlas2435 2 жыл бұрын
wish I had you the sophomore year of college. Great work!
@lolloBriggi
@lolloBriggi Жыл бұрын
Sometimes, I feel like programmers always need to overcomplicate things and come up with Schrödinger's cat solutions. I'll stick to 'if not null else'. Verbosity is better than Schrödinger's cat solutions to me.
@siddharthgollapudi6195
@siddharthgollapudi6195 10 ай бұрын
loved that subtle Schrodinger's cat reference
@LeandroOliveira-kn9qx
@LeandroOliveira-kn9qx Жыл бұрын
The best is the final .. advice where to use, generally in return of funciona, not every where creating a mess.
@vamonosraza4361
@vamonosraza4361 2 жыл бұрын
I am So grateful for your videos!
@MrLuMax5
@MrLuMax5 2 жыл бұрын
Have been waiting for this for a long time, thank you!
@pliegosevilla
@pliegosevilla 2 жыл бұрын
You remind me of those super cool, good and cute uni teachers everybody loves. Your videos are so fresh and spot on. I really like them, so thanks!!!!
@Iceman-gm1fu
@Iceman-gm1fu 2 жыл бұрын
thanks for the video. I liked the cat object being used since cats like boxes. to me, it sounds like optionals are used as a communiation device to say something may or may not be there. really drive home the idea that it might be null. if I could, I think I would rename the class to Maybe: Maybe maybeCatExists = findCatByName("Fred"); the next question that I want to ask is "What are actual use cases of this?" You have me thinking. Checking the database for a record is a great example. I am thinking about using a jdbc bridge connecting java to sql. Thinking of right/left join on two or more tables where there won't be an exact match one very row. Maybe maybeRecordExists = findRecordByName
@redcrafterlppa303
@redcrafterlppa303 2 жыл бұрын
We probably get a second optional class in some future release of java. Since there are some huge performance improvements planned for the near future especially effecting optional. And then to keep compatibility they might add a similar type with better performance. Like they did with Vector and ArrayList.
@chentang514
@chentang514 2 жыл бұрын
Amazing tutorial. Very clear.
@j.franciscox3318
@j.franciscox3318 Жыл бұрын
Love that desk setup, could you do a video about your desk setup? And maybe other stuff as well, such as your favorite IDE, text editors etc.
@TechOnScreen
@TechOnScreen 2 жыл бұрын
you just compiled all the facts about optional, thanks. can you also make a video on streams and lambdas and collections using them together to build business grade logical error-proof solutions..
@neolin.99
@neolin.99 Жыл бұрын
ah, a classic "we do not know if the cat is null or not null until we open the box, so until then, it is simultaneously null and not null"
@siegfriedkurz8753
@siegfriedkurz8753 2 жыл бұрын
Impressively explained.
@ultimatehuzefa
@ultimatehuzefa 2 жыл бұрын
Best Java Tutor!!
@petarJK
@petarJK 2 жыл бұрын
Excellent video! Very well explained. It's one thing to know something, totally different thing to be able to explain it well. Thanks!
@Ke5gggggel
@Ke5gggggel Жыл бұрын
Love your videos! Always looking if you made a video on the java subject I am looking for first before I watch a video of somebody else. Do a Tutorial about Streams please!
@arcane3327
@arcane3327 2 жыл бұрын
The explanation with your cat was really great. Thanks for the good tutorial!
@JoaoLima-pq1hm
@JoaoLima-pq1hm Жыл бұрын
Precious content, thank you! Greetings from Brazil 😊
@xtay2287
@xtay2287 2 жыл бұрын
Didn't know about the map-method. Thanks :)
@Anderzon1994
@Anderzon1994 2 жыл бұрын
Thanks for sharing your knowledge! I enjoy watching your tutorials!
@MBerg-qq6gf
@MBerg-qq6gf 8 ай бұрын
At 10:50 instead of demonstrating orElse with an "unknown cat" I would have used something like _string catDisplayValue = optionalCat.orElse("");_ because the whole point is to skip the "fake cat" (whether null, empty or unknown) and use directly the fallback value that needs to be used in the println when there's no cat! Please note that I suggested *pseudo code* -- my suggestion is not entirely syntactically correct (I come from c# and I don't know the Java equivalent to C#'s question mark yet)
@flytoinfinityvivi
@flytoinfinityvivi 7 ай бұрын
How to know if a youtube video is successful and worth watching? When you even enjoy watching the ads inserted into the videos lol love it
@dhilip77
@dhilip77 2 жыл бұрын
Your explanation are really simple, when could I expect spring boot
@asherkhan2656
@asherkhan2656 Жыл бұрын
Great explanation John
@dennisvanmierlo
@dennisvanmierlo 2 жыл бұрын
A cat in a box. It’s the perfect combo👌 Thank you for this great tutorial!
@RishiRajxtrim
@RishiRajxtrim Жыл бұрын
thanks
@biswarana4491
@biswarana4491 Жыл бұрын
Cat be like : things i need to comply with for some food..
@BaalZepar
@BaalZepar 2 жыл бұрын
I would've loved a secondary parameter to the .of method that asks for a default of the type contained in the optional, and a method orDefault on the optional, that would basically act like the get but return the default in case of an empty optional. This would allow for a null object pattern to be implemented from the start of the optional and for the users of the returned optional to decide if they care if the optional is empty or to go forward with the default provided by the creator method of the optional. It will also make sense, because who know better what a suitable default of the optional is, if not the creator of the returned value, and if the users of the value want they can always go with orElse, orElseGet, etc if they need a different default behavior on empty optionals. With the way it is now, everywhere you will get an optional as a return type you will have to interact with it in some way, granted, better than an if statement everywhere but can be better imo.
@isleschild
@isleschild 7 ай бұрын
thanks for the work, this was excellent.
@abiz504
@abiz504 2 жыл бұрын
No cats were harmed during making of this tutorial :D You forgot to add that! Good tutorial loved it :)
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
Yeah, Fred is a trooper
@Softrough1989
@Softrough1989 2 жыл бұрын
Very good explanation and I loved the cat and the box. Thanks
@Ahmad_Al-Deeb
@Ahmad_Al-Deeb 7 ай бұрын
"As a side note, if you know for sure, that the thing that you want to put into this optional is not NULL, then you can instead just use optional dot of" 6:06 Well, it sounds useless in this case, since the reason we used it was because of null case, right?
@elTripaSeca
@elTripaSeca 2 жыл бұрын
tutorial begin 1:35
@rezahajivand7498
@rezahajivand7498 Жыл бұрын
Hello mr jhon . Thanks for your teaching video 🎉🎉🎉
@azam11250
@azam11250 9 ай бұрын
great explaination sir. really liked ur videos and learned a lot. Thanks sir
Java Reflection Explained - bɘniɒlqxƎ noiɟɔɘlʇɘЯ ɒvɒᒐ
20:07
Coding with John
Рет қаралды 190 М.
Lambda Expressions in Java - Full Simple Tutorial
13:05
Coding with John
Рет қаралды 771 М.
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 2,6 МЛН
БУ, ИСПУГАЛСЯ?? #shorts
00:22
Паша Осадчий
Рет қаралды 3 МЛН
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 9 МЛН
Players push long pins through a cardboard box attempting to pop the balloon!
00:31
Set and HashSet in Java - Full Tutorial
20:43
Coding with John
Рет қаралды 233 М.
Vectors in Java: The 1 Situation You Might Want To Use Them
16:13
Coding with John
Рет қаралды 85 М.
Array vs. ArrayList in Java Tutorial - What's The Difference?
17:36
Coding with John
Рет қаралды 555 М.
Generics In Java - Full Simple Tutorial
17:34
Coding with John
Рет қаралды 1,1 МЛН
Java Custom Exceptions Tutorial - It's Way Easier Than You Think
14:29
Coding with John
Рет қаралды 166 М.
Stream API in Java
26:04
Telusko
Рет қаралды 358 М.
Java. Для чего нужен Optional?
18:26
Sergey Arkhipov Java Tutorials
Рет қаралды 13 М.
How To Call a REST API In Java - Simple Tutorial
29:14
Coding with John
Рет қаралды 284 М.
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 2,6 МЛН