Reading 2 - Basic Java
Reading 2 - Basic Java
Snapshot diagrams
Java Collections
Software in 6.005
Correct today and correct in the Communicating clearly with future Designed to accommodate
unknown future. programmers, including future you. change without rewriting.
You can also look at Getting Started: Learning Java (../../getting-started/java.html) as an alternative resource.
This reading and other resources will frequently refer you to the Java API documentation
(//docs.oracle.com/javase/8/docs/api/) which describes all the classes built in to Java.
Language basics
You should be able to answer the questions on the Questions and Exercises pages for all four of the langage
basics topics.
Questions: Variables
(//docs.oracle.com/javase/tutorial/java/nutsandbolts/QandE/questions_variables.html)
https://ocw.mit.edu/ans7870/6/6.005/s16/classes/02-basic-java/ 1/11
9/12/23, 8:48 PM Reading 2: Basic Java
Questions: Operators
(//docs.oracle.com/javase/tutorial/java/nutsandbolts/QandE/questions_operators.html)
Questions: Expressions, Statements, Blocks
(//docs.oracle.com/javase/tutorial/java/nutsandbolts/QandE/questions_expressions.html)
Questions: Control Flow
(//docs.oracle.com/javase/tutorial/java/nutsandbolts/QandE/questions_flow.html)
Note that each Questions and Exercises page has a link at the bottom to solutions.
Also check your understanding by answering some questions about how the basics of Java compare to the
basics of Python:
Don’t worry if you find the Number wrapper classes confusing. They are.
You should be able to answer the questions on both Questions and Exercises pages.
You should be able to answer the questions on the first two Questions and Exercises pages.
Don’t worry if you don’t understand everything in Nested Classes and Enum Types right now. You can go
back to those constructs later in the semester when we see them in class.
Hello, world!
You should be able to create a new HelloWorldApp.java file, enter the code from that tutorial page, and
compile and run the program to see Hello World! on the console.
Snapshot diagrams
It will be useful for us to draw pictures of what’s happening at runtime, in order to understand subtle
questions. Snapshot diagrams represent the internal state of a program at runtime – its stack (methods in
progress and their local variables) and its heap (objects that currently exist).
https://ocw.mit.edu/ans7870/6/6.005/s16/classes/02-basic-java/ 2/11
9/12/23, 8:48 PM Reading 2: Basic Java
To talk to each other through pictures (in class and in team meetings)
To illustrate concepts like primitive types vs. object types, immutable values vs. immutable references,
pointer aliasing, stack vs. heap, abstractions vs. concrete representations.
To help explain your design for your team project (with each other and with your TA).
To pave the way for richer design notations in subsequent courses. For example, snapshot diagrams
generalize into object models in 6.170.
Although the diagrams in this course use examples from Java, the notation can be applied to any modern
programming language, e.g., Python, Javascript, C++, Ruby.
Primitive values
Primitive values are represented by bare constants. The
incoming arrow is a reference to the value from a variable
or an object field.
Object values
An object value is a circle labeled by its
type. When we want to show more detail,
we write field names inside it, with arrows
pointing out to their values. For still more
detail, the fields can include their declared
types. Some people prefer to write x:int
instead of int x , but both are fine.
When you assign to a variable or a field, you’re changing where the variable’s arrow points. You can
point it to a different value.
When you assign to the contents of a mutable value – such as an array or list – you’re changing
references inside that value.
String s = "a";
s = s + "b";
https://ocw.mit.edu/ans7870/6/6.005/s16/classes/02-basic-java/ 3/11
9/12/23, 8:48 PM Reading 2: Basic Java
Immutable objects (intended by their designer to always represent the same value) are denoted in a snapshot
diagram by a double border, like the String objects in our diagram.
Mutable values
By contrast, StringBuilder (//docs.oracle.com/javase/8/docs/api/?
java/lang/StringBuilder.html) (another built-in Java class) is a mutable
object that represents a string of characters, and it has methods that
change the value of the object:
These two snapshot diagrams look very different, which is good: the difference between mutability and
immutability will play an important role in making our code safe from bugs .
Immutable references
Java also gives us immutable references: variables that are assigned once and never reassigned. To make a
reference immutable, declare it with the keyword final :
final int n = 5;
If the Java compiler isn’t convinced that your final variable will only be
assigned once at runtime, then it will produce a compiler error. So final
gives you static checking for immutable references.
Notice that we can have an immutable reference to a mutable value (for example: final StringBuilder sb
) whose value can change even though we’re pointing to the same object.
We can also have a mutable reference to an immutable value (like String s ), where the value of the
variable can change because it can be re-pointed to a different object.
Java Collections
The very first Language Basics tutorial discussed arrays
(//docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) , which are fixed-length containers for a
sequence of objects or primitive values. Java provides a number of more powerful and flexible tools for
managing collections of objects: the Java Collections Framework .
List<String>
0 1 2
"Barcelona"
"Boston" "Bogotá"
Set<Integer>
1024
-7 42
https://ocw.mit.edu/ans7870/6/6.005/s16/classes/02-basic-java/ 5/11
9/12/23, 8:48 PM Reading 2: Basic Java
Turtle
Literals
Python provides convenient syntax for creating lists:
And maps:
But this creates an array , not a List . We can use a provided utility function
(//docs.oracle.com/javase/8/docs/api/?java/util/Arrays.html) to create a List from the array:
A List created with Arrays.asList does come with a restriction: its length is fixed.
https://ocw.mit.edu/ans7870/6/6.005/s16/classes/02-basic-java/ 6/11
9/12/23, 8:48 PM Reading 2: Basic Java
Because of the way generics work, we cannot create a collection of primitive types. For example, Set<int>
does not work. However, as we saw earlier, int s have an Integer wrapper we can use (e.g.
Set<Integer> numbers ).
In order to make it easier to use collections of these wrapper types, Java does some automatic conversion. If
we have declared List<Integer> sequence , this code works:
List , Set , and Map are all interfaces : they define how these respective types work, but they don’t
provide implementation code. There are several advantages, but one potential advantage is that we, the
users of these types, get to choose different implementations in different situations.
If the generic type parameters are the same on the left and right, Java can infer what’s going on and save us
some typing:
Unfortunately, this ability to choose is also a burden: we didn’t care how Python lists worked, why should we
care whether our Java lists are ArrayLists or LinkedLists ? Since the only difference is performance, for
6.005 we don’t .
https://ocw.mit.edu/ans7870/6/6.005/s16/classes/02-basic-java/ 7/11
9/12/23, 8:48 PM Reading 2: Basic Java
Iteration
So maybe we have:
In Python:
Java provides a similar syntax for iterating over the items in List s and Set s.
We can’t iterate over Map s themselves this way, but we can iterate over the keys as we did in Python:
https://ocw.mit.edu/ans7870/6/6.005/s16/classes/02-basic-java/ 8/11
9/12/23, 8:48 PM Reading 2: Basic Java
Under the hood this kind of for loop uses an Iterator (//docs.oracle.com/javase/8/docs/api/?
java/util/Iterator.html) , a design pattern we’ll see later in the class.
Unless we actually need the index value ii , this code is verbose and has more places for bugs to hide.
Avoid.
API stands for application programming interface . If you want to program an app that talks to Facebook,
Facebook publishes an API (more than one, in fact, for different languages and frameworks) you can program
against. The Java API is a large set of generally useful tools for programming pretty much anything.
https://ocw.mit.edu/ans7870/6/6.005/s16/classes/02-basic-java/ 9/11
9/12/23, 8:48 PM Reading 2: Basic Java
Next up: a description of the class . Sometimes these descriptions are a little obtuse, but this is the first
place you should go to understand a class.
The method signature : we see the return type, the method name, and the parameters. We also see
exceptions . For now, those usually mean errors the method can run into.
The full description .
Parameters : descriptions of the method arguments.
And a description of what the method returns .
https://ocw.mit.edu/ans7870/6/6.005/s16/classes/02-basic-java/ 10/11
9/12/23, 8:48 PM Reading 2: Basic Java
Specifications
These detailed descriptions are specifications . They allow us to use tools like String , Map , or
BufferedReader without having to read or understand the code that implements them.
Reading, writing, understanding, and analyzing specifications will be one of our first major undertakings in
6.005, starting in a few classes.
https://ocw.mit.edu/ans7870/6/6.005/s16/classes/02-basic-java/ 11/11