#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

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

  • @arjunthazhatha1513
    @arjunthazhatha1513 ปีที่แล้ว +56

    I just love you sir! I was confused for so long about heap and stack. Now its clear! U are a great teacher.

  • @thefriendlymartian7317
    @thefriendlymartian7317 11 หลายเดือนก่อน +5

    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.

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

      I agree with u 100%

  • @kapiljain359
    @kapiljain359 ปีที่แล้ว +11

    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.

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

    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.

  • @sachin-t5g
    @sachin-t5g 11 วันที่ผ่านมา +1

    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.

  • @mehedishuvo6257
    @mehedishuvo6257 10 หลายเดือนก่อน +1

    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.

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

    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

  • @conqdat
    @conqdat 10 หลายเดือนก่อน +3

    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

  • @piotr2951
    @piotr2951 9 หลายเดือนก่อน +30

    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.

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

      😂

    • @khadeejahannah
      @khadeejahannah 4 หลายเดือนก่อน +2

      Yes.. every method doesn't hav its own stack!
      Stack frames are created for each method call n removed once the method completes

    • @ankitmeshram8461
      @ankitmeshram8461 4 หลายเดือนก่อน +1

      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!

    • @vyesdon
      @vyesdon 2 วันที่ผ่านมา

      So you are correcting the teacher? These things happen

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

    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.

  • @MohdZaid-kv7yn
    @MohdZaid-kv7yn ปีที่แล้ว +15

    Watching 2 hours before java developer interview

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

      How was the interview, brother

    • @MohdZaid-kv7yn
      @MohdZaid-kv7yn 6 หลายเดือนก่อน +5

      @@CSSHANUSG cracked it but they didn’t ask me stack and heap ;)

    • @CSSHANUSG
      @CSSHANUSG 6 หลายเดือนก่อน +1

      @@MohdZaid-kv7yn that's great....🔥 thanks brother

  • @ajuSiuuu
    @ajuSiuuu 6 หลายเดือนก่อน +1

    100 times better than those paid courses

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

    you are really great,this content is really helpfull and thanks for the notes that you are providing in description.

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

    Thank you Navin for providing quality content like this.

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

    I became a diehard fan for you sir ❤ love you and love your teachings sir

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

      Thank You SIR ✨
      This is my status today 🫴 ✨
      Thank You SIR 💫

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

    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!!

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

    you explain everything clearly and perfect. thanks for sharing your precious knowledge.

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

    this channel gives trustworthy content where other's just do for content sake

  • @LamNGUYEN-r8u5n
    @LamNGUYEN-r8u5n 15 วันที่ผ่านมา

    Best explication about stack and heap, thankyou

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

    You are just Amazing !! Sir , I am enjoying this java course . Thank you sir ❤

  • @Prabhakartambulkar
    @Prabhakartambulkar 6 หลายเดือนก่อน +1

    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!!!😃😃😃

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

    Best course imo. Can you please create exercises so we can test our knowledge and skills so far? Thank you

  • @Coding-Just
    @Coding-Just ปีที่แล้ว

    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

  • @Shubham-si6hd
    @Shubham-si6hd ปีที่แล้ว +1

    You are a life saver! Thank You

  • @bharathsajan8926
    @bharathsajan8926 8 หลายเดือนก่อน +2

    wonderfully explained thank you.

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

    U are a great teacher

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

    From Egypt Thank You Very Much

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

    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.

  • @bhuwanbhandari6862
    @bhuwanbhandari6862 16 วันที่ผ่านมา

    Thank you so much 😊

  • @dakaniii2221
    @dakaniii2221 28 วันที่ผ่านมา

    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?

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

    Simply awesome thank you 🌼

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

    Excellent explanation

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

    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

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

    Simply awesome

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

    great explanation . love it

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

    Thank you so much for this explanation!!!! :)

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

    maraming salamat tol

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

    Thank you, it was so educative to me

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

    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 !

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

    Great video sir 😊 understand concepts very well ! love you sir 🥳

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

    Love you Naveen sir, from Pakistan.

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

    excellent video.
    Keep it up.

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

    best explanation sirr

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

    really good explanation, even if my mother tongue is Russian

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

    Java does not have multiple call stacks per thread. Each thread has a single call stack that tracks method invocations and local variables.

  • @srinivasmb-re8km
    @srinivasmb-re8km 2 ปีที่แล้ว +3

    Where static variables are stored?
    static int in = 10;

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

    Splendid!

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

    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

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

    is the link between the heap and the stack like pointers in c++? so the reference variable you explain is in the heap?

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

    Question when obj is referenced to 101. Are relating to the hashcode?

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

    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

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

    Great way of delivery Sir. Really motivating to learn. Keep on Rocking Sir :-)

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

    Amazing video.. thank you.

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

    thank you sir

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

    incredible

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

    wow , these tutorials are of next level for sure. thank you sir.

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

    Shouldn't obj be created after DATA in the stack? I mean should not OBJ be above data?

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

    excellent !!

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

    nice and clear, thanks!

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

    i am comming from pw skills to watch full memory alliocaion

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

    If you guide us in Hindi we understand it more clearly

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

    Super

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

    I have one doubt what if the class is static ... we cant create instance right at the case how is memory allocated

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

    nicely explained! by the way, what program do you use to draw?

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

    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.

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

    One question - Exactly when is a stack created? When the method is created of when the method is called?

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

      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.

    • @banujan.b2479
      @banujan.b2479 ปีที่แล้ว

      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.

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

    Where the memory will be allocated for static members and static methods sir

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

    Will the 'add' method stack be reused for the 2 different objects or will it create a new 'add' method stack for obj1?

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

      the new obj1 will reuse the add method which is there in the stack

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

      ​@@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

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

    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

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

    But sir methods of objects are stored in method area😢

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

    I have a dooubt that where are static variables stored

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

    Sir how to get input from users in java

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

    Which device is he using for drawing?

  • @-RimuruTempest
    @-RimuruTempest ปีที่แล้ว

    Hi, I have one doubt. What about the add method in stack? If we create two objects then the is there two add stack?

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

    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);
    }
    }

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

    Wow!

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

    Could you replicate this video for python too?

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

    Thier values is null

  • @Sohel-fo2rr
    @Sohel-fo2rr 2 หลายเดือนก่อน

    ❤❤❤

  • @JohnHall-s9v
    @JohnHall-s9v 4 หลายเดือนก่อน

    3302 Mollie Port

  • @banujan.b2479
    @banujan.b2479 ปีที่แล้ว

    Each method has own stack or stack-frame? 🙄

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

    Scope

  • @RamSingh-bi9tc
    @RamSingh-bi9tc 7 หลายเดือนก่อน

    But damaging one phone affect the user heart

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

    Thier only one main entry function

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

    Entry point

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

    tqsmomg

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

    You beauty !!

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

    bro you just detaching from the topic itself...I just want to know the difference you are way too much describing things

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

    How the code is correct....add method will return 7 not 12.

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

    same name haha

  • @mr.islam777
    @mr.islam777 6 หลายเดือนก่อน

    Your teaching style not so good . Please explain more clearly

  • @khanhgiapham-mi4hg
    @khanhgiapham-mi4hg ปีที่แล้ว

    please speak English sir

  • @DipeshNamdev-q5r
    @DipeshNamdev-q5r หลายเดือนก่อน

    poor explanation , very poor