Learn Java in One Go! ⚡ Exceptions, Generics & Collections Made Easy
Skills:
ML Maths Basics50%
Key Takeaways
Covers Java concepts, including exception handling, generics, and collections
Full Transcript
goes is that you if possible please raise your hand and I will ask you to unmute yourself then you tell me your question and then I give you the answer okay so that's the template that we're going to follow so yeah let's get started any questions in Java please don't hesitate ask me anything that you want to clarify and I'll try my best to help you understand that concept so anything that you have for me please let me know if you don't say anything most probably I'm just going to sit here and do thing but uh I would really appreciate if you can you know like unmute yourself or at least raise your hand and then I will unmute you and then you can ask me a questions this is not a lecture class this is basically just a doubt resolution class so I will not be giving any lectures it's just that I will be answering doubts. My voice is not audible because I'm not saying anything. I hope you can hear me now. Adita, can you Oh, you have not connected to audio. That's why it's not audible. You need to connect to audio. Please. So, any questions in Java? Anything at all? insertion bubble and selection sorts. Well, that is not related to Java to be honest. It's a data structures and algorithms question or topic I must say. So, I'm not sure how relevant it is to our our conversation today. These are three different sorting algorithms with three different time complexities. Uh that's what they are, but they're not related to Java. If you still want me to go ahead and explain, I can do that. But I want to save the time for those people who want to ask questions specific to Java because that's what this uh session is for. Hello. >> Hello. Yes. >> Sir, can you explain about exception handling and wrapper class >> and wrapper class, right? >> Yes. Yes. >> Okay. Let's talk with let's start with exception handling. Okay. So, let me also share my screen. All right. I hope you can see my screen now. Can you? >> Yes sir. >> Okay. Let's try to understand what is exception handling. For that we need to understand what is exception. Do you know what is an exception? >> Yes sir. >> Can you tell me what do you know about exception? >> Exception is nothing but it is a bug in the code. >> Bug in the code >> where >> where we can uh means where we can resolve on our own. >> H okay. Okay. So exception uh let's try to look at it. Okay, please do not draw on my screen. Uh one second. Let me remove that. Just one second. H better now. All right. So there are problems that can come in your code. For example, if I write system.out.print ln hello. But if I miss this comma, right? And if I run this code, it will not run. Correct? >> Yes, >> it will not run. Right? So there are two types of problems that can come in while running a Java code. One is called as error and the other is called as exception. An error is a problem that cannot be fixed. But an exception is a problem that can be fixed. Now what is the meaning of fixing a problem? Fixing a problem as in like the code cannot the code does not know what to do when the problem has occurred. that will happen when we are doing or when we are dealing with error. For example, no matter what I write in this code, it will never run because there is a semicolon missing. >> Yes. >> Right. So, this is an error. However, an exception is a problem that can be handled. An exception is a problem that can be handled. >> Yes. >> Okay. An exception is a problem that can be handled. And the technique of doing that is called as exception handling. Okay. So we need to understand what is an exception. Now there are two types of exceptions. Exceptions that can occur in runtime and exceptions that can occur in compile time. Let me give you an example. So suppose I write a function public uh public void or public let's say double or your or let's say float divide two numbers. Okay. Is this code clear? Yes, sir. Is this code clear? Now, so let's say I go over here and I start to call this function. Let me make the static only static. Okay. Now if I say uh system.out.print ln divide two numbers and let's say I pass 10 comma 5 then what will be the answer? >> 2.0 >> right? But what if I make this zero? Then what will happen? >> Exception. >> An exception will be thrown. Right? So you can it says infinity. Oh that's because I'm doing it like this. Okay. What if I write it like this? You see an exception getting thrown. >> Yes sir. >> It says exception. Exception in thread main java.lang.arithmetic exception. So this is a type of exception. So first of all in Java exception is a class. You can see exception is a class that can have multiple children. So exception class has one child called as runtime exception. runtime exception. You can see that it will say that class runtime exception extends exception. Right? So runtime exception is an exception that can occur in runtime otherwise everything else will be occurring in compile time. So this is a runtime exception which is arithmetic exception. If I open this class you will see that it says arithmetic exception extends runtime exception is you see so now we can handle this exception by writing a try catch block. So try and then we write catch and here you can write the write the exception that we want to handle and we can say handling exception. So now if I run this code this line will throw an exception and that exception is getting handled in the catch block. This is how exception handling works in Java. That's >> sir. What if uh multiple catch blocks it will be present? >> You you can actually add multiple catch blocks also. For example, let's say uh you can say int n is equal to new or I can just write something like this. Just give me one second. Okay, this is an array. Okay. Now let's say if I write you see this code that I've written. >> Yes. Yes. >> Now you can see what what is happening. So first of all br line can throw a IO exception. So bio exception is a runtime exception. Sorry compile time exception because it is extending directly the exception class. So uh okay we'll come to this part later but you can see that the I have written I'm taking two arguments from the user in the console and then I am dividing those values of the array correct >> yes sir >> is this clear now let's say if I write for example if I write you you can see the size of the array is 10 10 >> yes sir >> size of the is 10 right now let's say if I write uh if I write let's say 9 and then if I write five then this will give me a value one. Why? Because 9 is at what is at 9? It's at uh 01. Like 9. At 9 we have eight. >> Correct. And at five we have >> five we have five. No, I'm I'm saying it wrong. I guess at 9 we have nine and at five we have five. Right? But let's say if I rerun this code and if I say 8 by 4 then it gives me two. Correct? But what if I say 9 by 10? You see now I have a different type of exception. >> Yes. Why? Because I'm accessing in B I'm accessing the value 10 which is not present in the array. >> Right? So this this part is giving me an exception. Right? Similarly I can write let's say 9 and zero. It gives me this exception arithmetic exception. So I can add one more catch block saying are you understanding what I've done? So if this exception is thrown this catch block will be invoked. If this exception is thrown then this catch block will be invoked and so on and on. But the hierarchy is also going to take care. If let's say this exception and this exception are related or okay let's not talk about that relation for now but what I'm trying to say is that hierarchy matters whatever exception is uh is whatever catch block is written first that exception will be checked for first >> okay >> something like that >> okay >> now in this case >> just just one one more line I want to say in this case you can see that both the catch blocks have the same body handling exception Right? >> Both of them have the same body. So you can do a multicat statement like this. And now you don't need this anymore. >> Not needed. >> Look. >> H. Now you were saying something. >> I have a doubt in integer a equal to integer percent. What it will take? Basically >> this part. >> Yeah. So this is so if I write nine over here this is this is coming as a string and which I'm converting into an integer by writing integer.parent >> sir is it array uh it will it is it taking input equal to >> yeah b read line is an input which is coming from here >> or else it it is taking from the array >> I'm not able to I'm not able to understand your question. Are you trying to ask about line number 13 and 14? Yeah, I am sir I am asking about integer a equal to integer parse in uh 13th line is there now sir is it taking the input? >> Yeah, this is taking the input from here. For example, if I put the code in debug mode and run this code, you will see that this line is now running and it if I write nine then the code goes to the next line. See now the value for a is equal to 9. But if I write 10 over here then the value for B will become 10. >> Okay. In 15th line is it taking the integer uh uh it is taking the input of 13th line. 13th and 14th line. >> Yes. Yes. Yes. >> Yes. Yes. Correct. >> Yeah. So that's my >> So this is what is exception handling in Java. >> Sir when to use means finally? Finally, when you want you should use finally when you have to do something irrespective of whether the exception occurred or not. >> So whether exception came or not came it does not matter. This this block of code will be executed. >> Okay sir. So >> usually this is done when we have a cleanup scenario. >> So try block will be executed every time. >> Yeah. >> Okay. Try block will be executed anytime every time. Okay. >> So you will execute this then if an exception came then it will go to the catch block according to whatever exception is being caught. >> Understood. Can you explain about wrapper classes? >> Yeah sure. So wrapper class there is a concept of primitive data type and uh primitive data types and non-primitive data types in Java. So primit primitive data types would include int, float, car, boolean etc. In Java, we have wrapper classes like this integer. For example, you see integer. So, this is a class that actually wraps an integer variable, int variable. Let me see if I have it. Okay, I can't find it. But what I want to tell you is that this integer class is a class that has a integer variable inside it. So that whenever you want to use it, you can use the class reference and it can behave the same as an integer that is called as a wrapper. Like you think about like like there's a chocolate, right? But on chocolate, we have put a wrapper on top of it. >> So it's like that. So integer class is a wrapper on top of an integer variable. That is the concept of wrapper classes. >> Sir, can you explain with example? Yeah, sure. So if I remove it, if I write let's say integer, A is equals to 10. Okay. >> Yes. >> And if I write int B is equals to 10 or let's say 12. And if I say print A and print B, run it in debug mode. Oh, I know. See, you see integer A, right? They have expanded. You can see a value. >> Yes, sir. >> See this value. So this integer class has a value or has a data member called as value which is of type int. Let me find for value. Uh value value 231 entries I don't even know where it is. >> Yeah you can see over here. >> Yeah. So it is so this integer class is wrapping over on top of this integer value int value >> that is what is happening okay now you know the pass by value pass by reference thing >> yes sir >> okay let me let me give you an example let's say public static increment in c m increment let's say int a whatever value will pass it will increment Okay, let's make it integer a. What is the problem over here? Okay, let's say if I write increment a uh okay, it does not return anything. Fine. Yeah, let me try. Just give me one second. I'm getting a phone call. Just give me one second, please. Yes, I'm back. So, we were writing this code, right? >> Yes. >> Okay. So, let's see how it's going to act or how it's going to behave. Okay. So, now we have A and now we have B. So you can see that the value for A did not become 11 and the value for B also did not become 12. >> Yes sir. >> Right. You see it it it should have become it should have changed right. We could have assumed that it could have changed right but it did not change >> right? Uh it did not change. It could have changed but it did not change but so so here in this case it is behaving like a primitive data type but usually what would happen is in other situations it would behave like a non-primitive data type. That is what is would happen in case of a rapper class. So we have the concept or rapper class primarily because of collections primarily because of collections and maps. That is the reason why we have a concept or wrapper class. >> Okay sir >> sir if uh uh then if we update uh B value then it will be updating no >> sorry >> B value. If we if we update the B value in uh increment uh function >> will it will it change it change the value? >> Can you repeat one more time >> sir? Uh in increment in increment function >> m >> if we update the value it will be changing in the output right? >> It should if we change the value should okay let me see if I write let's say 15 something like this. No sir, I'm asking about B value. >> B value. >> Yes, >> I mean how can we change the value for B in increment? Like we have passed it. Let's say if I write A equ= to 15 or A equals to let's say 20 then it won't change, right? Here we are passing the B variable, right? >> So >> here we are passing the B variable. So it won't be changing I guess. Okay. H it won't change. Also, one more thing, the value inside the integer class is a final. So, it will never change. Very much like an int variable. >> Okay. >> Any more questions? Hello sir, I have a question that can we construct a default constructor into parameterized constructor? >> Yeah, we can do that. For example, if I have this class, let's say, let me remove it. So if I have to create a demo class for now, I have to write code like this. demo d is equals to new demo right so this is a default constructor correct >> yes >> right so far it is correct but if I want to make it a parameterized constructor let's say public demo string let's say name where you're doing nothing but just printing name then you can you now you're mandated to write a name over here let's say shant which is my It's mandatory for you to write it. So this is now your default constructor. >> Yes. >> So now you have a parameterized parameterized constructor. >> Got it. >> Okay. Does default constructor nonparameterized constructor same? No, not same. By by default the constructor that is given by Java to you is a non-parameterized constructor but you can change it to whatever you want. Default constructor basically means any constructor. I mean or maybe you can say that Mickey. I think you can say that sir. Can you construct the default and parameterize at the same time? >> Yeah, you can do that. >> Default and parameterize, right? So, this is my you can write it. So, this is your default constructor. Okay. Hm. Okay. >> H. Any more questions? Any more questions? >> Hello sir. >> Yes. >> So can you explain what are the factory methods in Java? >> Factory methods are a concept or design uh it's a design pattern. It will take more time than we know but okay I can try. Just give me one second. So whenever we have to create an object of a class then that time we can make use of a design pattern called as factory design or factory design pattern. Okay. Let me give you an example. Suppose you have a interface called as shape. Okay. And this interface has only one function called as let's say void draw. Okay. Is it clear so far what I'm doing? >> Yes sir. Yes sir. >> Okay. So, so now that we have this interface, now let's say we have to we'll create some classes itself. I'll write it here only class circle implements shape. Okay. So I'll have to implement this function draw. Let's say I can write drawing a circle. Then I can write class rectangle implements shape. Then I have to implement it over here also. And here I can say draw and I can write drawing a rectangle. This so far clear. Yes sir. Yes sir. Okay. So far clear right? Okay. Now let's say I want to write a function or write a logic to create any shape. So I can write a shape factory shape factory. And I can write something like this. public static shape get shape string type and I can say if type dot equals ignore case let's say circle then return new circle else If type dot equals ignore case rectangle return new rectangle clear otherwise return null. So this is your factory method. Now if you have to create it let's say class factory example factory example and a main function shape s1 is equals to shape factory dot get shape and I can write circle so this will give me a circle s1 dot drawdraw then I can write shape s_ub_2 shape factor factory dot get shape rectangle then I can write s2 dot draw. If I run this code this is what we get. This is your factory method. Yes, static is mandatory sir. Sorry. >> Static cure is mandatory. >> Yeah. >> Thank you, sir. Thank you. Any more questions? Hello sir. >> Yes. >> So with the help of example can you please explain heap memory and stack memory? >> Heap and stack memory. It's very difficult to visualize it in front of I mean it's very difficult to show it on code but simply I'll tell you whenever any function is executed right it it is executed in the stack memory and heap memory whenever you create a data member that is stored in heap memory with the help of an example I mean I can show you where where goes what but I can't show you it to you in in code it's a concept >> so just give us examples >> that's what I'm saying right with the help of it will it's like For example, if I write it over here. So whatever uh I'll show you one second. Let me see. I can try. So you can see that I'm trying to create shape S1. Right. I'm trying to create shape S1. >> Right. >> Right. So it will go into this function get shape. Right. And it is getting triggered from the main function. So now I am inside the get shape function. You can see main is here. Then get shape is here. So this is my stack memory. All function executions are happening in stack and whatever variables are present over here they are or in heap memory. >> So so he memory stores temporary or permanent >> no temporary variables permanent not because garbage collector will come and delete it. Okay, sir. Sir H could you please differentiate string builder and string buffer in scenario of threading? >> String builder and string buffer are very there's only very very small difference. String buffer is thread safe. String builder is not. That's the only difference. I'll give you an example. Let me let me see if I can do that. Oh, Okay. String builder. Now I'll write for Okay. Okay, let's try to see what will happen. So if I run this code, okay, what happened? Add index out of exception. Uh, okay. Let me reduce the number. It's a 10th house. Okay. So let's see what happens. You see output.txt. How many lines are there in this file? 2 3 856. Correct. But how many lines should be should be there? 10,000 plus 20,000. So 30,000 lines should be there right? >> Yes sir. >> Right. So this problem happened because of multi-threading. So because there are two threads T1 and T2 and they both are trying to interact with the same string builder. Because of that reason they are not able to append at the same time. They are able to append at the same time but there is data inconsistency happening because of this. But if I replace this with the string buffer string buffer it will take le it will take more time but the data will be consistent. See now we are able to reach almost 30,000 lines. Why do we have THRE? Not sure but anyway. So there is less data inconsistency in this case. Let me make this a smaller value. Let's say like 15,000 maybe. See almost it reached. I need to make this even better. My multi-threaded code here is not that great actually. I need I can try make me write threads sleep one. Now this will take a lot of lot more time because I have added a thread sleeping logic. Let me make it smaller. Uh 0.5 can I add? No, I'll add one and I'll add two here. Two or let me two vice versa. One and two. Now this will take a lot of extra time but let's see if we can actually see the output here. Yeah, you can see that we got these many lines. So basically in string buffer when two threads are trying to modify the string same string buffer it is more thread safe. Are you understanding what I'm saying? >> Okay, sir. Thank you, sir. >> Any more questions? We have 10 minutes last. Any more questions? If there are no more questions then I can close the class honestly. Any more questions? All right, seems like there are no more questions then I think I we we can take a hard stop here. So, thanks for joining. I will see you next time. Thank
Original Description
In this video, we’ll cover some of the most important Java concepts every developer must know. Perfect for beginners and intermediate learners who want to strengthen their Java skills quickly and effectively.
🔥 What you’ll learn:
✅ Exception Handling – Try, Catch, Finally, Throw, User-Defined & Chained Exceptions
✅ Wrapper Classes & Autoboxing – Understanding conversions between primitives and objects
✅ Generics in Java – Basics, Advanced Generics, Wildcards & Usage with Collections
✅ Collections with Generics – Lists, Sets, Maps, and practical examples
✅ Mini Project – Apply everything you’ve learned in a hands-on Java project
By the end of this video, you’ll have a clear understanding of Java’s core concepts and the confidence to use them in real-world applications.
💡 Whether you’re preparing for interviews, coding exams, or building projects — this video will give you the boost you need!
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from GeeksforGeeks · GeeksforGeeks · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How I got into Walmart | Shailesh Sharma
GeeksforGeeks
Upgrade yourself In 29 Days | GeeksforGeeks
GeeksforGeeks
Learn AWS Fundamentals For Free
GeeksforGeeks
Conversation With Young Achievers | Meet the winners of Bi-Wizard Coding Contest | GeeksforGeeks
GeeksforGeeks
Meet The Winners Of Bi-Wizard Coding Contests | GeeksforGeeks
GeeksforGeeks
Interview Prep Strategies | PayPal
GeeksforGeeks
OLX Interview Preparation Strategies | Hukam Singh
GeeksforGeeks
Meet Some More Winners Of Bi-Wizard Coding Contests | GeeksforGeeks
GeeksforGeeks
Live Mock DSA
GeeksforGeeks
Microsoft Azure For Absolute Beginners
GeeksforGeeks
Python for Data Science | Data Science Master Bootcamp | Arpit Jain
GeeksforGeeks
Getting Started with Data Analysis | Data Science Master Bootcamp | Ashish Jangra
GeeksforGeeks
How to prepare theory subjects for SDE interviews | Geeks Summer Carnival 2022
GeeksforGeeks
Get Your Tickets To The Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
TED Talk Data Analysis Project | Data Science Master Bootcamp | Ashish Jangra
GeeksforGeeks
How I Secured AIR 9 in GATE'22 | Tushar
GeeksforGeeks
Learn Java Backend Development | Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
How to Recognize which Data Structure to use in a question | Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
Learn Data Structures and Algorithms | GeeksforGeeks
GeeksforGeeks
Interview experience at Flipkart | GeeksforGeeks
GeeksforGeeks
Lets Prepare for GATE'23 the Right Way | Sakshi Singhal | GeekSummerCarnival
GeeksforGeeks
Highest Paying Jobs in 2022 | Ishan Sharma | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Geeks Summer Carnival 2022 | 5th April- 11th April | GeeksforGeeks
GeeksforGeeks
Preparing for SDE interviews | Soham Mukherjee | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Full Stack Development with React & Node | Utkarsh Malik | Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
Introduction to Open Source and Roadmap to GSOC 2022 | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Web Scraping in Action | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Getting Hired at BITCS via GfG Job Portal | Get Hired With GeeksforGeeks
GeeksforGeeks
How to build a faster landing Page | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Geeks Summer Carnival | 5th To 11th April, 2022 | GeeksforGeeks
GeeksforGeeks
How to get ideas for Startup | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Journey from Tier 3 to JusPay | GeeksforGeeks
GeeksforGeeks
Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Dispelling Myths and Pre conceptions of Programming Languages
GeeksforGeeks
Must Do System Design Questions
GeeksforGeeks
Understanding Sorting Techniques in an hour | Keerti Purswani | Geeks Summer Carnival
GeeksforGeeks
Get Hired at NEC | Job-A-Thon 8
GeeksforGeeks
Journey from Tier 3 college to Microsoft | GeeksforGeeks
GeeksforGeeks
Get Hired with GeeksforGeeks at SuperK | Job A Thon 8
GeeksforGeeks
GeeksforGeeks: Redesigned
GeeksforGeeks
From Tier 3 to cracking multiple interviews | GeeksforGeeks
GeeksforGeeks
Live Mock DSA
GeeksforGeeks
Youtube Data Analysis | Ashish Jangra | GeeksforGeeks
GeeksforGeeks
DSA Self-Paced Course Preview | Sandeep Jain | GeeksforGeeks
GeeksforGeeks
GATE Live Classes | Prepare for GATE CS 2023 | GeeksforGeeks
GeeksforGeeks
Journey from JIIT to Adobe
GeeksforGeeks
Life Is Unfair Ft. Shonty badmash | LIVE Discord Session | A GeeksforGeeks Exclusive
GeeksforGeeks
Interview Experience at Google | Tech Dose
GeeksforGeeks
Live Mock DSA
GeeksforGeeks
Interview Experience @ Amazon | GeeksforGeeks
GeeksforGeeks
My journey through the tech world from India to US | Vidushi | GeeksforGeeks
GeeksforGeeks
Complete Interview Preparation Course | GeeksforGeeks
GeeksforGeeks
Live Mock DSA
GeeksforGeeks
Getting Hired at FiftyFive Technologies | Job-a-thon 9.0
GeeksforGeeks
GFG Karlo, Ho Jayega | GeeksforGeeks ft. Khaleel Ahmed
GeeksforGeeks
How I got job offers from 2 big companies : Arcesium & Microsoft | GeeksforGeeks
GeeksforGeeks
LINUX for Beginners | GFG x Itversity
GeeksforGeeks
My interview experience at Walmart | GeeksforGeeks
GeeksforGeeks
Get Hired at Speckyfox
GeeksforGeeks
Live Mock DSA
GeeksforGeeks
More on: ML Maths Basics
View skill →Related Reads
📰
📰
📰
📰
The system doesn't fail the day you break it.
It fails the day you finally touch it.
Dev.to AI
React Server Components vs Client Components in Next.js — A Practical Decision Framework
Dev.to · Aon infotech
Server Components vs Client Components: The Mental Model Shift Every Vite Developer Needs
Dev.to · Digital dev
Go context.Context Done Right: Cancellation, Timeouts, and Values
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI