16 - Memory Representations and Abstract Data...

Post on 07-Apr-2020

6 views 0 download

Transcript of 16 - Memory Representations and Abstract Data...

Memory Representations and the List Abstract Data Type

COMP110 - Lecture 16

Announcements

• WS02 is This Week’s Assignment

• Second half deadline extended to Sunday at 11:59pm

• Office Hours are Whiteboard Only for Part 2

• Why?

• It is valuable practice for understanding loops and arrays without relying on the computer.

MT0 Score

# of Tickets

Sweet Spot

0 1-3 4-6 7-9 10-15 16+

Today

• We’ll build a halloween costume idea generator and learn about memory representations of our data.

Data Representations• Binary

• Numbers

• Characters

• Strings

• Arrays

• Objects (Groups of primitives)

How is data physically represented in memory?

• Memory is a vast bank of nano-scale transistors and capacitors holding billions of “bits” of information.

• A bit can have two values:

• 0 or 1

• A byte is 8 bits.

• We tend to imagine memory looks like this. Each address holds one byte.

01234567

Addresses 8 through 1015

10231022102110201019101810171016

The Beauty of Abstraction

• We’re about to look under the hood at how values are represented in memory …

• … yet we didn’t need to know this to write Java programs.

• Good abstractions allow us to code at larger scales (integers) without sweating the details (integers are just bytes). 0

1234567

Addresses 8 through 1015

10231022102110201019101810171016

If all memory can hold are 0s and 1s, how do programs work

with numbers and Strings?

• What do these variables look like in memory?

• int[]a=newint[]{85,78,67};

• Strings=“UNC”;

• We interpret groups of bits using a base 2 numeral system.

• “binary”

• This allows us to represent more useful values.0

010000110000000001001110000000000101010100000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

0 0 0 0 0 0 0 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 0

110100

Base Ten:

Binary:

0 0 0 0 0 0 0 1

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 1

110100

Base Ten:

Binary:

0 0 0 0 0 0 1 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 2

110100

Base Ten:

Binary:

0 0 0 0 0 0 1 1

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 3

110100

Base Ten:

Binary:

0 0 0 0 0 1 0 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 4

110100

Base Ten:

Binary:

0 0 0 0 0 1 0 1

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 5

110100

Base Ten:

Binary:

0 0 0 0 0 1 1 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 6

110100

Base Ten:

Binary:

0 0 0 0 0 1 1 1

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 7

110100

Base Ten:

Binary:

0 0 0 0 1 0 0 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 8

110100

Base Ten:

Binary:

0 0 0 0 1 0 0 1

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 9

110100

Base Ten:

Binary:

0 0 0 0 1 0 1 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 1 0

110100

Base Ten:

Binary:

0 0 0 0 1 0 1 1

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 1 1

110100

Base Ten:

Binary:

0 0 0 0 1 1 0 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 1 2

110100

Base Ten:

Binary:

Convert this value from binary

to an integer.

0

010000110100111001010101

1234567

Addresses 8 through 1015

10231022102110201019101810171016

0 1 0 1 0 1 0 1

1248163264128

Every “place” with a 1, add up the number below it.

Convert this value from binary

to an integer.

0

0100001101001110

851234567

Addresses 8 through 1015

10231022102110201019101810171016

0 1 0 1 0 1 0 1

1248163264128

Every “place” with a 1, add up the number below it.

This is 85.

Java has 4 types of integers

Type # of bits # of bytes Min Max

int 32 4 -2,147,483,648 2,147,483,647

byte 8 1 -128 127

short 16 2 -32768 32767

long 64 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807

Here’s roughly what memory looks like after we declare

and assign some variables…

0

010000110100111001010101

1234567

Addresses 8 through 1015

10231022102110201019101810171016

bytea,b,c;a=85;b=78;c=67;

cba

What if we declare int instead of byte?

0

0100111000000000000000000000000001010101000000000000000000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

inta,b,c;a=85;b=78;c=67;

b

a

Should I worry about an int using 4 bytes?

0

0100111000000000000000000000000001010101000000000000000000000000

1234567

Addresses 8 through 7,999,999,992

8,000,000,000

b

a

W.W.B.G.D.?

Hey Bill, you have $82,000,000,000 dollars. Do you worry yourself when

something costs $10 vs. $40?

7,999,999,9997,999,999,9987,999,999,9977,999,999,9967,999,999,9957,999,999,9947,999,999,993

As a programmer living in 2016, you’re a byte billionaire. Live it up.

What about float and double?

0

0000000000000000

YOUWILL

LEARNIN

COMP411

1234567

Addresses 8 through 1015

10231022102110201019101810171016

floata,b,c;a=85.0;b=78.0;c=67.0;

What about character values?

0

010000110000000001000011000000000100111000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

chara,b,c;a=85;b=78;c=67; a

b

c

Hands-on1. In PrimitiveDataTypes.java’s main method, uncomment:

PrimitiveDataTypesRunner.charExample();

2. In the charExample method, after the 3 char variables are initialized, print out the following using System.out.println:

3. “The chars are: “ + a + b + c

4. Check-in on pollev.com/comp110 when you see what printed.

5. Already done? Try changing the numbers assigned to each char to spell ‘ABC’.

An ASCII Table is a char encoding table

American Standard Code for Information Interchange

85

78

67

U

N

C

What about character values?

0

010000110000000001000011000000000100111000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

chara,b,c;a='U';b='N';c='C'; a

b

c

It’s not often you work with char values directly, but you can surround single letters in single quotes when you do.

U N C

A Stringis an array of chars

85 78 67

“ ”

What is a String?

0

010000110000000001000011000000000100111000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

char[]_s=newchar[]{'U','N','C'};

s

• String is a class. Each String instance has an instance variable storing array of characters like the one above.

Why do we have to write new char[3]?

0

010000110000000001000011000000000100111000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

s

• When an array is initialized, we must specify its type and length.

• This is how the computer knows how much memory to allocate for the array.

• char (2 bytes per) array with a length of 3? Allocate 6 bytes of memory.

How does array indexing work?

0

010000110000000001000011000000000100111000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

s• Java keeps track of the first address of

byte 0 in each array.

• When an index is requested, Java computes how many bytes away from the beginning address it is.

• sbegin is 1017

• s[n] address is:sbegin + n * bytesper element

• s[0] is 1017 + 0 * 2 => 1017 • s[1] is 1017 + 1 * 2 => 1019 • s[2] is 1017 + 2 * 2 => 1021

And that’s the magic behind…

0

010000110000000001000011000000000100111000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

char[]s=newnewchar[]{‘U’,'N','C'};

short[]a=newshort[]{85,78,67};

• In physical memory, these two arrays would look exactly the same in terms of 1s and 0s.

• The meaning we assign to binary values is what makes all the difference.

How are objects stored?• Imagine a class with two instance variables:

classPoint{privateint_x,_y;

publicPoint(intx,inty){//elided}}

• When we construct a new instance, we allocate memory for each instance variable:

Pointp=newPoint(1,2);0

0000001000000000000000000000000000000001000000000000000000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

p

p._x

p._y

Abstract Data Types• The language’s primitive types (int, double, boolean, char) are a

language’s “atoms”

• We as programmers combine primitives into objects to model specific ideas of our apps and problems

• i.e. a Profile object has a String name, an int age, and a boolean isPublic

• Over time, programmers have realized some concepts are generally useful for organizing the data and objects in our programs.

• We generally call these concepts Abstract Data Types

• An ADT is a model for organizing data with specific characteristics and capabilities.

Abstract Data Types• Some of the most common and useful ADTs

• List - a sequence of ordered values where values can occur more than once.

• Set - an unordered set of values with no repeated values.

• Associative Array - “Dictionary” like an array, but the indexing is up to you. Index could be a String.

• Others: Graph, Tree

• We’ll explore and implement ADTs over the coming weeks.

Back to Halloween Costumes

• We want to pick randomly from a List of adjectives and a List of nouns.

• Users can add more ideas to both lists. We don’t know how many will be in each list at the same time.

• Why can’t we just use an array for each list?

My favorite UNC costume:Cherie Berry

Wouldn’t it be nice…• If we had a List of String values we could just add to without

worrying about how many we have?

• At this point in the semester, we can build a StringList class to do just that!

• We’ll have a class with the following methods:

• void add(String value) - Add a value to our List.

• String get(int index) - Get the String held at an index in the List.

• int size() - Returns the # of Strings in the List.

Follow-Along: StringList

• Open StringList.java and StringListRunner.java

• Try running the runner.

// Implement your _values resize logic here. if (_count == _values.length) {

// Step 1. int newLength = _values.length * 2; String[] _newValues = new String[newLength];

// Step 2. int i = 0; while (i < _values.length) { _newValues[i] = _values[i]; i++; }

// Step 3. _values = _newValues; }

Changing Gears: Looping

• General form of a while loop statement: while(<booleantestcondition>){<whilebody:codetorepeat>}

• A while loop statement can be used in any method’s body.

• The test condition must be a booleanexpression

• A while loop will repeat the code in its body again and again while the test condition is true.

while Loop Statement

49

How do we write a while loop that will loop N times?

• When we’ve needed to loop some specific number of times, we’ve followed this pattern:

inti=0;

while(i<N){<codetorepeat>i++;}

50

Initialize “counter variable”

Bound the loop on that variable

Modify the variable’s value

• Looping with a counter variable is so common, many programming languages have shorthand syntax for this.

• Java (and many others) have a for loop:

for(<initcounter>;<bound>;<modifycounter>){<forbody:codetorepeat>}

• We can rewrite the previous while loop as a for loop:

for(inti=0;i<N;i++){<forbody:codetorepeat>}

for Loop Statement

51

• The following while loop:

inti=0;while(i<N){<whilebody:codetorepeat>i++;}

• Can be written as a for loop:

for(inti=0;i<N;i++){<forbody:codetorepeat>}

whileandfor Loops Can Be Equivalent

52

for(inti=0;i<N;i++){<forbody:codetorepeat>}

for Loop Syntax

53

1. The counter variable initialization happens first and only once.

2. The test condition is checked. If true, go on. If false, jump past loop.

3. The code within the for body is run.

4. The counter variable is modified.

5. Jump to step 2.

Hands-on• Rewrite the while loop in the

add method of StringList to be a for loop, instead.

• Check-in on PollEv when you’re done!

• The following while loop:

inti=0;while(i<N){<body>i++;}

• Can be written as a for loop:

for(inti=0;i<N;i++){<body>}

Let’s rewrite that while loop as a for loop

// Implement your _values resize logic here. if (_count == _values.length) {

// Step 1. int newLength = _values.length * 2; String[] _newValues = new String[newLength];

// Step 2. for (int i = 0; i < _values.length; i++) { _newValues[i] = _values[i]; }

// Step 3. _values = _newValues; }