Lecture 9

well is been a while since I uploaded last course of Jonathan Shewchuk (you´re probably missing him :)), here I am with a next lecture in his series… this time about … STACK FRAMES

0:19 today we gonna learn about how Java stores stuff in memory, if you’re an embedded programmer I bet you’re interested in this kind of things, even if Java is not so used in the “embedded world”

As you may have noticed the words “THE STACK AND THE HEAP” written in big and capital letters on the table, are suggestive

0:40  the heap stores all the objects and … all the class variables, those which are declared static


1:26 the STACK stores all of the local variables, including parameters

it looks like the discussion about stack and local variables will be steered towards recursion

2:15 when a method is called Java creates a stack frame for that method another good lesson to learn, in case it wasn’t already known

3:20 the JVM maintains an entire stack … of stack frames

the example showing the memory layout (of both stack and heap) is very useful and practical, but it was better (it is just a personal opinion and you do not have to take it as a reference) if also the code which is run could be revealed (because it is quite hard to follow Mr Shewchuk)

07:02  a discussion about this parameter will start, it is a local variable, it is never kept inside an object,  it is some kind of a starting point for a method to find the object that it is operating on

07:52 main does not have this parameter, because main is a static method

10:31 when a method ends the stack free for that method is deleted, it is not garbage collection …

garbage collection is something that happens to the heap objects only

11:52 a good thing to bear in mind is the difference between some local variables, which are destroyed after method ends, but the objects that they were pointing to not

there is the threat that an object which is not pointed by any other variable, cannot be reached anymore, so it may provoke a memory leak …  but in case of Java it will be garbage collected

12:40 in the java.lang library there is a method Thread.dumpstack() which prints a list of stack methods, just to check the stack frame

14:13 Parameter passing … in Java all parameters are passed by value, very different than how things are done in C and also C++

this implies two things: those parameters do not refer to the original variables, but to some copies of them and the functions that are using those parameters are not allowed to change the original values, they just act on some copies

very useful the example with different method calls in order to show how the stack frame changes during those calls

this is quite similar to the notorious example of swap function applied three times (in the context of C++) with value parameters, with pointer parameters and with reference parameters (in C is almost the same except the fact that reference parameters cannot be used)

first example with doNothing is similar to value call in C or C++, it has no effect

second one with set3 has the following result: the reference gets copied but not also the object that it points to; this was because ib object is created on the stack, but if this is created on the heap, using new, and then changed by set3 function, this change will remain valid also after function ends

next example is badset4… what happens when this is executed, a new stack frame is created on the stack, ib points to a new created object, not the original one, but another one created inside badset4. Basically the newly created object is changed not the one that we intended. Another bad thing is that the newly created object is not referenced anymore.

23:40 for those of you who has ever studied a language who has something called pass by referenceset3 is the closest example to pass by reference that you can get in Java

24:28 Binary search for recursive methods… useful in searching in an already sorted array.

The idea is to return the index of the search value, in case this is not found a failure is returned. The concept is quite simple and is similar to the way of searching in a phone book, or some other ordered list: check the middle element, if this bigger than the searched element, continue the search in the left half, if it is bigger continue to search in the right one, and the algorithm continues recursively until the searched element is found.

33:51 a very important thing, in general in case of algorithms, is the execution time, how fats are they?

36:10 the stack will be drawn while the binary search algorithm is executing

40:38 what’s interesting about the stack frames is that each one has its own independent set of local variables, and during code execution which one is actually referred? Always you can access the local variables on top of the stack at any given time

honestly the part consisting in returning a variable from a stack frame to the one below it, I didn’t get it! how can it be this one:

this bsearch returns the 4 to the other bsearch without even storing it

43:10 a tough point concerning stacks, maybe the toughest one is concerning stack space, as an embedded programmer you’re always prone to this eternal limitation: STACK OVERFLOW, this is a run-time error (you cannot or you hardly detect it during program developing phase)

the idea is that an operating system can offer to a program a space for several thousands of stack frames

45:40 Scope and recursion: what is scope? we said it hundreds of times on this blog – the portion of code that can access the variable.

How many types of scopes are in Java?

1. Class variables – their scope is everywhere in the class, they are just overwritten by local variables

2. Fully qualified class variables, are those for which their scope is indicated in their declaration, like: System.out(you know that it is out variable from System class)

3. Instance variables: their scope is the body of non-static methods where they are defined. (Class variables are always associated with static variables and instance variables are associated with non-static variables, same thing can be extended for functions)

4. Those have also their correspondents: fully qualified instance variables

5. Local variables – parameters, their only scope is the block that surrounds them. They are only available for the topmost stack frame recursion, meaning that they can overwrite any other type of variable

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: