CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
-
Upload
reynard-lindsey -
Category
Documents
-
view
215 -
download
0
Transcript of CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
![Page 1: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/1.jpg)
CSC 212 –Data Structures
Lecture 2:
Primitives, References, & Classes
![Page 2: CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/12.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/13.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022082613/5697bfd41a28abf838cacae7/html5/thumbnails/14.jpg)
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