CS 61B Resources
Fall 2015 Section Information
Discussion 116: Monday, 3-4 PM (3113 Etcheverry)
Discussion 117: Tuesday, 5-6 PM (9 Evans)
Lab 017: Thursday, 5-7 PM (275 Soda)
Office Hours: Monday, 4-5:45 PM (B6 Evans)
Q: How can I learn Git?
The Pro Git guide by Scott Chacon & Ben Straub on the official Git site is pretty awesome. Reading it (plus lots of experimenting) was actually how I learned Git myself.
As I taught multiple semesters of students to use Git in CS 61B, I incrementally wrote and revised my own condensed guide to Using Git. It only contains the basics of Git that I thought were most important for students to learn first. Many parts of it are inspired by the Pro Git guide, and I highly encourage more advanced students to go ahead and read the Pro Git guide for more details of how Git works under the covers and a survey of other features.
Fun Fact: One of my earliest blog posts from December 2013 was the very first draft of what would eventually become the Using Git guide. You can really see how much has changed in subsequent versions.
But honestly, the ultimate best way to learn Git and become comfortable with it is to use it for your own projects! If you don't know the answer to a question, don't be clueless! Look at the Git man pages and experiment!
Q: How do I use Java's
Serializable
interface?
Let's first answer the question of what the
Serializable
interface even allows you to do by considering a common use case for it. Let's say that we want to create some Java program that is able to maintain state across different distinct executions.
One way we could achieve this effect is by saving all our important instance variables in files that we read in/out of for each execution. This - while being completely doable - is, however, in most simple cases made unnecessary by a built-in Java functionality called
Serializable
. Any class that implements
the Serializable
interface can convert its state into some form of information that can be saved into an output file then read back in just as easily.
I think the best way to learn is by experimentation, so take a look at this example called
Main.java
, which serializes a simple Cat
class. Download it and try out the following commands.
$ javac Main.java
$ java Main
$ java Main Joey
$ java Main
$ java Main Daniel
Perhaps at this point, you're wondering, "What in the world?! How was that possible?!" (If you're not, pretend you are at this point to humor me.) Here's where you should take a look at the code to see how simple it is to use
Serializable
!
Q: Is Java pass-by-value or pass-by-reference?
Java is pass-by-value, and understanding this will help in making sense of why some functions modify their parameters while others do not. I suggest reading the first two answers on Stack Overflow. Also, this comment is particularly helpful:
"Think about it this way. Someone has the address of Ann Arbor, MI on a slip of paper called
annArborLocation
. You copy it down on a piece of paper called myDestination
. You can drive to myDestination
and plant a tree. You may have changed something about the city at that location, but it doesn't change the LAT/LON that was written on either paper. You can change the LAT/LON on myDestination
but it doesn't change annArborLocation
." (source)
If you come from a Python background, check out this post for information on how Python is pass-by-object-reference. Hopefully, this will help illustrate the differences/similarities between how the two languages work.
Q: Why can't I access a variable outside of the loop it was created in?
All variables created inside a loop will be lost once the program exits the loop. This phenomenon is best explained by variable scope in Java. A local variable has a scope that limits it within the class, method, and loop (if applicable) it was declared in. The local scope of a variable is limited by the smallest scope that applies to it.
Scope is important because variables can only be used within their scope. Let's work through some examples. Firstly, if a variable is defined within a class but outside of any methods or loops, it can be accessed throughout the class.
Secondly, if a variable is defined within a class method but outside of any loops, it exists throughout the whole method (after it was initialized), but other methods do not have direct access to the variable. (The variable being passed into another method as a parameter does not count because Java is pass-by-value. If you have questions about this comment, reference the question above.)
Finally, we get to the loop example. If a variable has been created within a loop, its most limiting scope is the loop. Thus, it can only be used within the loop. I thought this page was a helpful read on variable scope.