Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1.

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1."— Presentation transcript:

1 CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu 1

2 Agenda Announcements –Cell phones off & name signs out Next week –No new lab, but… hand in lab 1 (and if you haven’t finished, do exercises) –Monday: review and Q&A –Wednesday: first exam Today –variables –terminology –maybe class definitions – but most likely Friday

3 Last time we saw… –how to instantiate a class to create an object new example1.Terrarium() –how to call a method on a newly created object: new example1.Terrarium().add(new example1.Caterpillar())

4 Last time: Object communication To put an example1.Caterpillar object inside an example1.Terrarium object: > new example1.Terrarium().add(new example1.Caterpillar())

5 Expression evaluation evaluating new example1.Terrarium() –causes an object to be created and initialized –produces a value

6 (part of) memory 107 108 109 110 111 112 113 114 115

7 evaluating a ‘new’ expression used available used 107 108 109 110 111 112 113 114 115 When evaluating an expression like ‘new example1.Terrarium()’, the operator ‘new’ first determines the size of the object to be created (let us say it is four byte for the sake of this example)

8 evaluating a ‘new’ expression used reserved by ‘new’ available used 107 108 109 110 111 112 113 114 115 When evaluating an expression like ‘new example1.Terrarium()’, the operator ‘new’ first determines the size of the object to be created (let us say it is four bytes for the sake of this example) Next, new must secure a contiguous block of memory four bytes large, to store the representation of the object.

9 evaluating a ‘new’ expression used 10101010 available used 107 108 109 110 111 112 113 114 115 When evaluating an expression like ‘new example1.Terrarium()’, the operator ‘new’ first determines the size of the object to be created (let us say it is four byte for the sake of this example) Next, new must secure a contiguous block of memory four bytes large, to store the representation of the object. Bit strings representing the object are written into the reserved memory locations.

10 evaluating a ‘new’ expression used 10101010 available used 107 108 109 110 111 112 113 114 115 When evaluating an expression like ‘new example1.Terrarium()’, the operator ‘new’ first determines the size of the object to be created (let us say it is four byte for the sake of this example) Next, new must secure a contiguous block of memory four bytes large, to store the representation of the object. Bit strings representing the object are written into the reserved memory locations. The starting address of the block of memory holding the object’s representation is the value of the ‘new’ expression. This address is called a ‘reference’.

11 evaluating a ‘new’ expression 107 108 109 110 111 112 113 114 115 A similar thing happens when we evaluate another ‘new’ expression like ‘new example1.Caterpillar()’. used available used

12 reserved by ‘new’ available used evaluating a ‘new’ expression 107 108 109 110 111 112 113 114 115 A similar thing happens when we evaluate another ‘new’ expression like ‘new example1.Caterpillar()’. Supposing that an example1.Caterpillar object occupies two bytes of memory, new reserves a contiguous block of two bytes…

13 used 11110000 available used evaluating a ‘new’ expression 107 108 109 110 111 112 113 114 115 A similar thing happens when we evaluate another ‘new’ expression like ‘new example1.Caterpillar()’. Supposing that an example1.Caterpillar object occupies two bytes of memory, new reserves a contiguous block of two bytes, writes bit strings representing the object to those memory locations, and the starting address of this block of memory is the value of the ‘new’ expression.

14 Where do objects come from? (The “birds and bees” talk) Programmer writes a program in a high-level language like Java: example1.Terrarium.java Computers don’t understand programs expressed in high-level languages  ?

15 Compilation A compiler translates program to an equivalent low-level form that a computer can understand example1.Terrarium.class Compiler translates Programmer writes a program in a high-level language like Java: example1.Terrarium.java

16 Runtime Runtime refers to the time during which a program is executing, or running. Compiler translates

17 Objects exist only at runtime Objects do not exist while the programmer writes the program, except in their minds. Compiler translates

18 Huh? If objects are the basic building blocks of object-oriented programs, and programmers don’t directly manipulate objects, what do programmers write? They write class definitions. Objects are instances of classes. Classes are instantiated only at runtime.

19 How do we make the Caterpillar move? We call “start()” on the Caterpillar. How do we do this? Can we write the following? new example1.Terrarium().add(new example1.Caterpillar()) new example1.Caterpillar().start() We can, but it won’t do what we want. Q: what happens?

20 A: Two example1.Caterpillar objects are created. –One is placed in the example1.Terrarium, and is NOT “started”. –The other is NOT put into the example1.Terrarium, but is “started”. We cannot refer to the Caterpillar we added to the Terrarium, because: –we have no reference to it after adding it. The value of an expression is lost if it is not –used right away, or –remembered.

21 We want to write something like this: t. add( c ) c. start() (notice ‘c’ used in two places) To do this, we need a variable

22 A variable is: (at its most basic) a storage location in memory for example, location 120: 116 117 118 119 120 121 122 123 124 space for a variable

23 A variable has: a name a location a type a value a scope a lifetime  in the HLL (Java)  in memory  representation scheme/size  contents We’ll discuss these later

24 Why are types important? Recall the discussion from early in the semester about how to interpret a string like “01001101” – the proper interpretation depends on the encoding scheme (e.g. two’s complement or IEEE754) The type of a variable tells the compiler, among other things, which encoding schemed to use to read/write data from/to the variable. It also tells us what methods we can call.

25 Why are names important? The location of a variable in memory is not known until runtime. The name of a variable allows us to refer to the variable without knowing where in memory the variable might end up. Names make programs easier to write and to read/understand.

26 Variable declaration A variable declaration consists of two basic things: –the type of the variable –the name of the variable Java insists that all variables be declared before use (not all languages require this).

27 The variable declaration Consists minimally of: type & name What is a type? A class is a type. –Remember: objects are instantiated from classes. Examples of variable declarations: example1.Terrariumt; example1.Caterpillar c;

28 Naming conventions CamelCase ( camel hump at start of each word ) –lower case letter to start –letter or digit follows –each new word, except first, capitalized Examples: t myTerrarium theVeryHungryCaterpillar

29 Syntax (a little more formal) variable declarations appear in many places in Java programs a variable declaration in Java –ALWAYS has: type identifer In our example –type identifier ; –semicolon is a terminator

30 We have a variable… … so now what? After we declare a variable, we need to assign a value to it. We do that using an assignment statement. Unlike an expression, a statement has no value.

31 assignment statement variable = expression ; ‘=’ is the ASSIGNMENT OPERATOR Example: t = new example1.Terrarium();

32 Syntax example1.Terrarium t; t = new example1.Terrarium(); example1.Caterpillar c; c = new example1.Caterpillar(); t.add(c); c.start();


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1."

Similar presentations


Ads by Google