#26 Stack And Heap in Java
ฝัง
- เผยแพร่เมื่อ 7 ก.พ. 2025
- Check out our courses:
Java Full Stack and Spring AI - go.telusko.com...
Coupon: TELUSKO10 (10% Discount)
DevOps with AWS: From Basics to Mastery : go.telusko.com...
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : go.telusko.com...
Coupon: TELUSKO20 (20% Discount)
Udemy Courses:
Spring: go.telusko.com...
Java:- go.telusko.com...
Java Spring:- go.telusko.com...
Java For Programmers:- go.telusko.com...
Python : go.telusko.com...
Git : go.telusko.com...
Docker : go.telusko.com...
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusk...
In this lecture we are discussing:
1) Memory management in java and typed of categories to store data in memory.
2)What is stack ?
3)What is heap ?
4) where instance variable and local variable is allocated?
#1
In java we are managing our managing our memory using two data structure stack and heap.
two categories to store data in memory:
1) Stack data structure
2) heap data structure
#2
Stack is data structure which follow LIFO(Last in First Out) principle.
#3
heap is data structure which is used to store data in form of object and object is a collection of data and method.
#4
class Student{
int instVariable; //store in object and object created in heap
public static void main(String []args){
int localVariable=0; //local variable store in stack frame of method in stack area.
Student st=new Student(); //st is reference variable store address of new Student() object
}
public int add(int num1,int num2){
return num1+num2;
}
}
-- instance variable stored in object and object are in heap therefore we can say that instance variable created in heap.
-- method declaration and definition is present in object but actual implementation present in stack frame of stack.
-- local variable created in stack during call of method.
-- stack frame of called method is present in stack area.
-- st is reference variable created inside the current stack frame and it is reference variable store address of student object created in heap area.
-- new Student() create object in heap area and pointed by his reference variable.
Github repo : github.com/nav...
Java:- bit.ly/JavaUde...
Spring:- bit.ly/SpringU...
More Learning :
Java :- bit.ly/3x6rr0N
Python :- bit.ly/3GRc7JX
Django :- bit.ly/3MmoJK6
JavaScript :- bit.ly/3tiAlHo
Node JS :- bit.ly/3GT4liq
Rest Api :-bit.ly/3MjhZwt
Servlet :- bit.ly/3Q7eA7k
Spring Framework :- bit.ly/3xi7buh
Design Patterns in Java :- bit.ly/3MocXiq
Docker :- bit.ly/3xjWzLA
Blockchain Tutorial :- bit.ly/3NSbOkc
Corda Tutorial:- bit.ly/3thbUKa
Hyperledger Fabric :- bit.ly/38RZCRB
NoSQL Tutorial :- bit.ly/3aJpRuc
Mysql Tutorial :- bit.ly/3thpr4L
Data Structures using Java :- bit.ly/3MuJa7S
Git Tutorial :- bit.ly/3NXyCPu
Donation:
PayPal Id : navinreddy20
www.telusko.com
I just love you sir! I was confused for so long about heap and stack. Now its clear! U are a great teacher.
The best explanation anyone could ever give. As soon as I have a question in my mind, you call out that point and explain as if I asked the question in person. Just shows how perfectly the flow of the content is.
I agree with u 100%
Really Nice explained. I am following Telusko since my collage time(2018). And now I am the Senior Software Developer in reputated Product company. Big Thanks Goes to Mr Naveen. Thank you Sir!
I would like to request if you can make the playlist on AWS for Java Developers. It would be very helpful.
The best Java teacher I've ever encountered in my life. You are awesome sir. The understanding of the concept is sooo seamless. It's like a miracle. Thanks soo much.
In Java, methods do not each have their own stack; instead, each thread has its own stack. When a method is called, a new stack frame is pushed onto the thread’s stack, which includes the method’s local variables and parameters. As methods complete, their stack frames are popped off the stack. This means that while each method call has its own space on the stack for its local variables and parameters, the stack itself is shared by all method calls made by the thread.
Each thread in a Java application has its own stack, which is separate from other threads’ stacks. This ensures that each thread’s method calls and local variables do not interfere with those of other threads.
Well explained dear sir.Not only India but also all over the world students get benefited by your lecture,. This is a masterpiece that ever created by you.💜💜💜💜💜,,,,Love from Bangladesh dear sir.
Thank you for the video :) I just started going into interviews again and this helped me get back to speed on the topic cause I had forgotten so much stuff throughout the year as a junior dev
Stack:
- Store local variables
- Address of obj ( obj store in HEAP )
Heap:
- Store obj. Have 2 sections: instance variables & .... (add(),....)
Every method have own stack
No, we don't have multiple stacks (in a single thread), those are called frames. Those frames contain multiple data structures for storing local variables, operands, return addresses, constants.
😂
Yes.. every method doesn't hav its own stack!
Stack frames are created for each method call n removed once the method completes
A Request - It would be helpful for new learners If you could tag a link to the video or the document that contains right & complete information of this particular topic!
So you are correcting the teacher? These things happen
Thanks for this sir, i like when you are describing how things work using a sketch.
maybe it's just me but reading documentation that just only have text explanation is really confusing and to imagining it was really hard.
Same
Watching 2 hours before java developer interview
How was the interview, brother
@@CSSHANUSG cracked it but they didn’t ask me stack and heap ;)
@@MohdZaid-kv7yn that's great....🔥 thanks brother
100 times better than those paid courses
you are really great,this content is really helpfull and thanks for the notes that you are providing in description.
Thank you Navin for providing quality content like this.
I became a diehard fan for you sir ❤ love you and love your teachings sir
Thank You SIR ✨
This is my status today 🫴 ✨
Thank You SIR 💫
When i asked my sir about instance variable he gave me such lame ans that I gave up on every being able to understanding it. Thanks!!
you explain everything clearly and perfect. thanks for sharing your precious knowledge.
this channel gives trustworthy content where other's just do for content sake
Best explication about stack and heap, thankyou
You are just Amazing !! Sir , I am enjoying this java course . Thank you sir ❤
sir give us a quetion bank to practice this concepts, all quetions from basic to advance, so we can practice and implement the concepts or logic. your lectures are very easy to understand!! they are very helpful thank you!!!😃😃😃
Best course imo. Can you please create exercises so we can test our knowledge and skills so far? Thank you
Navin sir it's totally clear thanks a lot and sir i have to join your Java backend course but Saturday is my college sir and it's 3rd year bca 7 to 12 time is not convenient sir
You are a life saver! Thank You
wonderfully explained thank you.
U are a great teacher
From Egypt Thank You Very Much
a small correction. every method doesn't create its own stack. they create a stack frame in the same stack. every thread creates its own stack.
Thank you so much 😊
Thanks for the explanation. I have two questions. You said the methods of objects in heap are only definitions and the values of the method are in stack. Why? How the connection between method definition in heap and its values in stack is built off, so that the JVM knows how to find the values?
Simply awesome thank you 🌼
Excellent explanation
public class Main
{
String s = "ABC";
public static void main(String[] args) {
Main obj = new Main() ;
System.out.println(obj.s);
}
}
in this above code String object is created in heap and also of class.
So 2 objects in heap.
But isn't the string object Abc resides inside class object in the heap?that's why we are calling it obj.s
Simply awesome
great explanation . love it
Thank you so much for this explanation!!!! :)
maraming salamat tol
Thank you, it was so educative to me
Hi sir , i have a question . We all know that if we wanna create an object we need to use the new keyword for that , if we don't it won't use a different part of memory ! But the question is , so why we can create a copy constructor in a class without using that new keyword and still have a different area of memory which is not gonna effect the main object ?
I would be glad if you answer me , and thanks for your great teaching skills !
Great video sir 😊 understand concepts very well ! love you sir 🥳
Love you Naveen sir, from Pakistan.
excellent video.
Keep it up.
best explanation sirr
really good explanation, even if my mother tongue is Russian
Java does not have multiple call stacks per thread. Each thread has a single call stack that tracks method invocations and local variables.
Where static variables are stored?
static int in = 10;
Splendid!
I understand clearly, it's not an object. It's a reference type.like Car c = new Car() c is reference type, correct or not
is the link between the heap and the stack like pointers in c++? so the reference variable you explain is in the heap?
Question when obj is referenced to 101. Are relating to the hashcode?
Sir with reference to memory allocation in java can u plzz specify which of the variables have static memory allocation and which one has dynamic allocation and why
Great way of delivery Sir. Really motivating to learn. Keep on Rocking Sir :-)
Amazing video.. thank you.
thank you sir
incredible
wow , these tutorials are of next level for sure. thank you sir.
Shouldn't obj be created after DATA in the stack? I mean should not OBJ be above data?
excellent !!
nice and clear, thanks!
i am comming from pw skills to watch full memory alliocaion
If you guide us in Hindi we understand it more clearly
Super
I have one doubt what if the class is static ... we cant create instance right at the case how is memory allocated
nicely explained! by the way, what program do you use to draw?
I have a doubt. When we write a local variable in Java. It automatically allocates memory or it allocates after compilation? Please someone address this.
One question - Exactly when is a stack created? When the method is created of when the method is called?
I think stack memory allocated at run time. So it created when mehod is called. Thats why we get stackoverflow error at runtime instead of compile time.
To be more specific the entire program has a stack. But when you call a method, each method creates a stack-frame in the stack.
Where the memory will be allocated for static members and static methods sir
Stack Area
Will the 'add' method stack be reused for the 2 different objects or will it create a new 'add' method stack for obj1?
the new obj1 will reuse the add method which is there in the stack
@@Krishna_scriptpublic class Main
{
String s = "ABC";
public static void main(String[] args) {
Main obj = new Main() ;
System.out.println(obj.s);
}
}
in this above code String object is created in heap and also of class.
So 2 objects in heap.
But isn't the string object Abc resides inside class object in the heap?that's why we are calling it obj.s
An instance variable is a variable that is specific to a certain object. It is declared within the curly braces of the class but outside of any method. The value of an instance variable can be changed by any method in the class, but it is not accessible from outside the class
But sir methods of objects are stored in method area😢
I have a dooubt that where are static variables stored
Sir how to get input from users in java
Which device is he using for drawing?
Hi, I have one doubt. What about the add method in stack? If we create two objects then the is there two add stack?
both objects having same address only. if car1 =car2
class Car
{
String name;
int cost;
double mileage;
}
public class PassByReference {
public static void main(String arg[])
{
Car car1 =new Car();
System.out.println(car1.name);
System.out.println(car1.cost);
System.out.println(car1.mileage);
car1.name="bmw";
car1.cost=1_00_0000;
car1.mileage=17.5;
System.out.println(car1.name);
System.out.println(car1.cost);
System.out.println(car1.mileage);
Car car2=car1;
System.out.println(car2.name);
System.out.println(car2.cost);
System.out.println(car2.mileage);
car2.name="honda";
car2.cost=1_00_000;
car2.mileage=18.5;
System.out.println(car2.name);
System.out.println(car2.cost);
System.out.println(car2.mileage);
System.out.println(car1.name);
System.out.println(car1.cost);
System.out.println(car1.mileage);
System.out.println( car1.hashCode());
System.out.println( car2.hashCode());
System.out.println(car1);
System.out.println(car2);
}
}
Wow!
Could you replicate this video for python too?
Thier values is null
❤❤❤
3302 Mollie Port
Each method has own stack or stack-frame? 🙄
Scope
But damaging one phone affect the user heart
Thier only one main entry function
Entry point
tqsmomg
You beauty !!
bro you just detaching from the topic itself...I just want to know the difference you are way too much describing things
How the code is correct....add method will return 7 not 12.
same name haha
Your teaching style not so good . Please explain more clearly
please speak English sir
poor explanation , very poor