Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to Objects and Classes in Java: Lecture 9 from CSMC 131 Spring 2007 - Prof. B, Study notes of Computer Science

A lecture transcript from csmc 131 spring 2007, focusing on the introduction of objects and classes in java. The lecture covers the basics of objects, their relationship with classes, and the creation of objects. It also discusses the java heap and garbage collection. Questions and answers related to the notions of system, pseudo-code, and object-oriented programming are included.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-xih
koofers-user-xih 🇺🇸

5

(1)

10 documents

1 / 20

Related documents


Partial preview of the text

Download Introduction to Objects and Classes in Java: Lecture 9 from CSMC 131 Spring 2007 - Prof. B and more Study notes Computer Science in PDF only on Docsity! CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) Lecture 9: Introduction to Objects and Classes Last time: 1. Precedence and short-circuiting 2. Type casting 3. Software lifecycle Today: 1. Pseudo-code (from last lecture) 2. Objects and classes 3. Heaps 4. Garbage Collection CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) 1 Questions What is System in System.out.println()? Why use str.equals(“cat”) to compare equality of String str and “cat”? Is the similarity of the notations System.out.println() str.equals() sc.nextInt() important, or coincidental? CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) 4 Example of Objects Bank account State: account number, type, owner, balance, etc. Operations: deposit, withdrawal, transfer, balance inquiry, etc. Student State: name, ID, date of birth, major... Operations: lookup grades, lookup class, compute tuition, compute age, … Scanner State: stream to read from (e.g System.in) Operations: get an int, double, byte, … String State: sequence of characters Operations: comparison test, equality test, (much more), … CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) 5 Sample Student Object Kerry KeenanName 444230695ID 06-22-1987DOB CMSCMajor State Methods getAge date → age getGrades semester → grades etc. etc. CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) 6 Accessing State / Methods If o is an object v is an instance variable of the object m is a method of the object Then o.v is how to access the data in v o.m is how to invoke m So System is an object, with out an instance variable out is also an object, with println a method System.out.println is how to access this method! Suppose str is a String str is an object! Methods of this object: equals, compareTo, etc. str.equals, str.compareTo, etc. invokes these objects CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) 9 Classes “Blueprints” (“templates”) for objects Classes include specifications of Instance variables (including types, etc.) to include in objects Implementations of methods to include in objects Classes can include other information also, as will be seen later static methods / instance variables public / private methods, instance variables And so on CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) 10 Student Class Example Conceptually: Instance variables: String name int ID int dateOfBirth String major Methods getAge getGrades etc. The actual class implementation will include code for the methods This describes a blueprint for student objects How do we create objects from such a class (blueprint)? CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) 11 How Are Objects Created? In Java: using new Recall: Scanner sc = new Scanner (System.in); Invoking new: creates fresh copies of instance variables in the “heap” returns the address where the fresh variables are stored Heap? Address? CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) 14 Object Creation New space allocated in heap to store instance variables Reference (= address) to this space is returned Scanner sc = new (…); Allocated Heap Heap Stack Free Heap sc CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) 15 Strings Are Objects Oh really? Where is new in String name = “Narita”; ? Java provides it! String is special because it is used so often Java automatically “fills in” new You can too: String name = new String(“Narita”); CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) 16 In Java, 9 Sorts of Variables 8 primitively typed Types are the 8 built-ins (int, byte, double, etc.) Storage allocated on stack based on type Value stored in stack e.g. int x Reference typed Types are classes Storage allocated on stack to hold one memory address (typically, one word) What is stored in stack is reference to heap, where actual data is stored e.g. Scanner sc = new Scanner (System.in);
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved