Lecture 14 | Programming Methodology (Stanford)
Why Stack Overflow recommends it
Stack Overflow users link it when answering Java questions about stack versus heap memory, thread safety of local variables, pass-by-value and garbage collection, because it shows what happens in memory in plain terms.
Recommended by 2 people across 2 different months, 2012 to 2015.
What it is
Lecture 14 of Stanford's CS106A (Programming Methodology), taught by Professor Mehran Sahami in 2008. It explains how a running Java program uses memory, covering the stack, the heap, stack frames for method calls, and how objects and references are laid out.
What people said
Recommends kosa · 2012-10 · Stack Overflow · on “Why are local variables thread safe in Java”
When you create a thread it will have its own call stack [LINK] created. Two threads will have two stacks and one thread never shares its stack with other thread. All local variables defined in your program will be allocated memory in stack (As Jatin commented, memory here means, reference-value for objects and value for primitive types) (Each method call by a thread creates a stack frame on its own stack). As soon as method execution is completed by this thread, stack frame will be removed. There is great lecture by Stanford professor in youtube [this video] which may help you in understanding this concept.
Recommends kosa · 2012-10 · Stack Overflow · on “How do stack traces get generated?”
Every thread will have its own stack . Each method call creates a stack frame. If something wrong happened in code of any method, that will propagated to caller method. This way JVM can trace which method generated error and what is the call hierarchy. If you observe the stack trace properly, you will see the method where error occured at top and the hierarchy in bottom. There is a great lecture in [this video] by a Stanford professor to understand how does it work. I would suggest watching it. NOTE: This is theory. If you would like to know how API works, @Peter Lawrey answer may help you.
Recommends kosa · 2012-10 · Stack Overflow · on “Maximum number of local variables in a Java Method”
There is no upper bound in defining number of local variables. If you define too many variables which couldn't fit in a stack frame (or) JVM couldn't allocate a stack frame for that size, it will throw StackOverflowError and exit. There is good lecture [this video] by a stanford professor which may help you.
Recommends Predrag Maric · 2015-01 · Stack Overflow · on “when we create an object ,do creation of object and the execution of constructor happens at the same time?”
When you call new , memory is allocated on the heap for the object, including it's variables which are initialized to default values. If values are supplied as constructor parameters, those values are used. Once the constructor is finished with initialization, the variable to which it is assigned is created on the stack, with address of the object in the heap as its value. I suggest you watch this Stanford lecture [this video] where this is explained in just enough details so that anyone can understand it.
Cites kosa · 2013-01 · Stack Overflow · on “Memory Management for local objects in java”
It is not guaranteed that object will be GCed as soon as method call is done, but object will become as eligible for GC and on next GC run it may be collected and memory will be free. EDIT: Yes you are correct. You don't need to set it to null . Local variables will be created on stack and stack will be removed as soon as method is completed. So, paint will go away from memory and new Paint() object will be on heap without any references, which makes above object as eligible for GC. See this [this video] .
Cites T.J. · 2013-03 · Stack Overflow · on “this reference inside the construstor”
I am watching Programming Methodology (Stanford) (CS106A) course on Java . In lecture 14 [this video] Professor Sahami told about memory allocation in Java for functions and object on stack and heap . He told that for any method called on an object , a stack is allocated and argument list and this reference is allocated space on stacke . Through this refernece stored, Java is able to refer to the correct instance variables of an object . but for constructor no this reference is stored along with argument list as object is being constructed. My ques is if constructor don't have have this refernece then how we can use it inside constructor for ex public class foo { private int i; public foo(int i)
Cites Michał Żbikowski · 2014-09 · Stack Overflow · on “Is Java "pass-by-reference" or "pass-by-value"?”
Throughout all the answers we see that Java pass-by-value or rather as @Gevorg wrote: "pass-by-copy-of-the-variable-value" and this is the idea that we should have in mind all the time. I am focusing on examples that helped me understand the idea and it is rather addendum to previous answers. From [1] In Java you always are passing arguments by copy; that is you're always creating a new instance of the value inside the function. But there are certain behaviors that can make you think you're passing by reference. Passing by copy: When a variable is passed to a method/function, a copy is made (sometimes we hear that when you pass primitives, you're making copies). Passing by reference: When a