Download presentation
Presentation is loading. Please wait.
Published byReynard Lindsey Modified over 9 years ago
1
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes
2
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
3
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’ value Record any output Need to work slowly and methodically
4
Data Types Java has 7+1 primitive data types boolean, char, int, long, float, double, String * Only types that work with Java operators E.g., +, -, %, &&, ||, |, >=, <, … Computers also use these types natively Simplifies how they work and are used
5
Primitive Types Primitive variables are simple to use Each variable is “box” holding its value Assignments copy values Updates only to the local variable But primitives of limited usefulness Finite range of possible values Each variable has 1 value at a time
6
Primitives Example int x = 5; int sum = 0; for (int i=0; i < x; i++) { sum = sum + x; }
7
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 letter Usually use interior capitals to highlight words ClassNameGoesHere is used as the type
8
Using Classes Normally use instances of a class Class instantiated using new command public class Kitty {... } Kitty kat = new Kitty(...); Each instantiated object is unique: Kitty cat = new Kitty(...); Kitty tiger = new Kitty(...); cat = new Kitty(...);
9
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 control Do not equal object, but refer to object TV kat = new TV(...); kat
10
Using References Assignments alias references Makes variables refer to same instance new is only way to create an instance Kitty cat, tiger; cat = new Kitty(...); tiger = new Kitty(...); tiger = cat; cat tiger
11
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 = tiger tiger = cat; cat = kat; cat tiger kat
12
Arrays Arrays are special object type Array variables are references in own right Arrays must refer to instance before use int[] bob = new int[30]; Each entry in array is like its own variable bob’s entries are primitive variables Following array’s entries are references Car[] parkingLot = new Car[300]; Can’t use entry in Car until it refers to instance
13
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]?
14
Before Next Lecture… Review basic Java syntax and loops Will 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, & methods May want to review any old notes & handouts
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.