csc 212 – data structures lecture 2: primitives, references, & classes

14
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Upload: reynard-lindsey

Post on 18-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

CSC 212 –Data Structures

Lecture 2:

Primitives, References, & Classes

Page 2: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Announcements

Still need a note-taker; please see me if you are interested in the $50

If you need more review of Java, talk to me and/or seek out additional resources

Page 3: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Tracing Code

Vital skill for writing and debugging Still my favorite way to debug

Quickest way to find common, simple bugs Good way to understand how code works Execute line-by-line just like the computer

Update variables’ valueRecord any outputNeed to work slowly and methodically

Page 4: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Data Types

Java has 7+1 primitive data types boolean, char, int, long, float, double, String*

Only types that work with Java operatorsE.g., +, -, %, &&, ||, |, >=, <, …

Computers also use these types nativelySimplifies how they work and are used

Page 5: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Primitive Types

Primitive variables are simple to use Each variable is “box” holding its value

Assignments copy valuesUpdates only to the local variable

But primitives of limited usefulnessFinite range of possible valuesEach variable has 1 value at a time

Page 6: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Primitives Example

int x = 5;int sum = 0;for (int i=0; i < x; i++) { sum = sum + x;}

Page 7: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Classes

In real world, must describe more than just primitives

Java classes define these additional types Classes begin with:

public class ClassNameGoesHere {Usually start with capital letterUsually use interior capitals to highlight words

ClassNameGoesHere is used as the type

Page 8: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Using Classes

Normally use instances of a class Class instantiated using new commandpublic class Kitty { ... }Kitty kat = new Kitty(...);

Each instantiated object is unique:Kitty cat = new Kitty(...);Kitty tiger = new Kitty(...);cat = new Kitty(...);

Page 9: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Reference Variables

Variables of class type called references Must be assigned to instance before using

Special value, null, marks that reference is not currently assigned to any instance

References similar to a remote controlDo not equal object, but refer to objectTV kat = new TV(...);

kat kat

Page 10: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Using References

Assignments alias referencesMakes variables refer to same instancenew is only way to create an instance

Kitty cat, tiger;cat = new Kitty(...);tiger = new Kitty(...);tiger = cat;

cat

tiger

Page 11: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Aliasing

Aliased variables refer to same instance Can make change using either variable Both variables will see changes that are made Assignments only to one reference, however

Kitty cat, tiger, kat;cat = new Kitty(...);tiger = new Kitty(...);kat = tigertiger = cat;cat = kat;

cat

tiger

kat

Page 12: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Arrays

Arrays are special object typeArray variables are references in own rightArrays must refer to instance before useint[] bob = new int[30];

Each entry in array is like its own variablebob’s entries are primitive variablesFollowing array’s entries are referencesCar[] parkingLot = new Car[300];

Can’t use entry in Car until it refers to instance

Page 13: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Tracing Example

int max = 0;Car[] parkingLot = new Car[4];for (int i = 0; i < 2; i++) { parkingLot[i] = new Car(...);}

Could we use:parkingLot[0]?parkingLot[3]?parkingLot[4]?

Page 14: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Before Next Lecture…

Review basic Java syntax and loopsWill not go over this in class (sorry!)Book contains a good review of this

Look over week #1 homework and see if you have questions

Friday’s lecture will cover constructors, fields, & methodsMay want to review any old notes & handouts