COMP Objects and References

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Written by: Dr. JJ Shepherd
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Catie Welsh March 2,  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Java Syntax Primitive data types Operators Control statements.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
Methods We write methods in our programs for many reasons:
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.
CSC 212 Object-Oriented Programming and Java Part 2.
February 28, 2013 COMP Introduction to Programming Objects and References Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
COMP Primitive and Class Types Yi Hong May 14, 2015.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Written by: Dr. JJ Shepherd
COMP More About Arrays Yi Hong June 05, 2015.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Catie Welsh March 4,  Midterm on Monday, March 14th ◦ Closed books, no notes, no computer  No office hours during Spring Break ◦ However, I will.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
COMP 110 Some more about objects and references Luv Kohli October 10, 2008 MWF 2-2:50 pm Sitterson 014.
Java: Base Types All information has a type or class designation
COMP Review of Chapter 1 & 2
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Computer Organization and Design Pointers, Arrays and Strings in C
Objects as a programming concept
Java: Base Types All information has a type or class designation
COMP 110 Review for midterm exam
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
An Introduction to Java – Part I
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
Lesson 2: Building Blocks of Programming
Computers & Programming Languages
An Introduction to Java – Part II
Computers & Programming Languages
Arrays .
Today’s topics UML Diagramming review of terms
Building Java Programs
Recap Week 2 and 3.
Object Oriented Programming Review
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
In this class, we will cover:
Names of variables, functions, classes
References Revisted (Ch 5)
Classes and Objects Object Creation
Announcements Lab 5 due Wednesday at noon.
Presentation transcript:

COMP 110-001 Objects and References Yi Hong June 01, 2015

Today Objects and references More on classes

Review Classes Objects Instance variables Methods Return types Parameters and arguments

Variables of a Class Type Behave differently from variables of a primitive type Class types are reference types, a variable of a class type contains the memory address A variable of a primitive type contains the data value

Variables of a Primitive Type When declaring a variable, a certain amount of memory is assigned based on the declared primitive type What is in this memory? int age; double length; char letter; memory

Variables of a Primitive Type A data value is stored in the location assigned to a variable of a primitive type int sum; sum = 4; sum = sum + 1; Memory

Variables of a Primitive Type A data value is stored in the location assigned to a variable of a primitive type int sum; sum = 4; sum = sum + 1; 00000000 00000100 Memory

Variables of a Primitive Type A data value is stored in the location assigned to a variable of a primitive type int sum; sum = 4; sum = sum + 1; 00000000 00000100 00000000 00000101 Memory

Variables of a Class Type What about these variables? Student jack; String inputString; memory

Variables of a Class Type Contain the memory address of the object named by the variable NOT the object itself What is an address? The object’s location in the computer’s memory Object is stored in some other location in memory The address to this other location is called a reference to the object Class types are also called reference types

Example: Books Assume we have a class named Book vs. public class Book Book jacksBook = new Book(); Book apusBook = new Book(); vs. Book apusBook = jacksBook; public class Book { private name; private page; public void setName(); public void setPage(); }

Objects in Memory Memory jacksBook apusBook 2078 1056 2078 ? 2078 ? ? Book jacksBook; Book apusBook; jacksBook = new Book(); apusBook = new Book(); jacksBook.setName(“Java”); apusBook.setName(“Java”); jacksBook.setPage(137); apusBook.setPage(253); apusBook = jacksBook; apusBook.setPage(509); jacksBook is now on p. 509! jacksBook apusBook 2078 1056 2078 ? 2078 ? ? 2078 ? 1056 2078 Java ? 137 ? ? Java Java ? Java 253 137 Java 253 509

Remember Variables of a class type contain memory addresses NOT objects themselves

== vs. equals() for Strings String is a class type What happens if you have String s1 = new String(“Hello”); String s2 = new String(“Hello”); boolean strEqual = (s1 == s2); strEqual is false! Why? s1 and s2 store different addresses!

== vs. equals() for Strings What happens if you have String s1 = new String(“Hello”); String s2 = new String(“Hello”); boolean strEqual = (s1.equals(s2)); strEqual is true! Why? String’s .equals() method checks if all the characters in the two Strings are the same

Writing the .equals() method public class Book { private String name; private int page; public boolean equals(Book book) return (this.name.equals(book.name) && this.page == book.page); }

.equals() Every class has a default .equals() method if it is not explicitly written Does not necessarily do what you want You decide what it means for two objects of a specific class type to be considered equal Perhaps books are equal if the names and page numbers are equal Perhaps only if the names are equal Put this logic inside .equals() method

Call-by-value Java passes arguments to a method For primitive type, the parameter contains the value of its corresponding argument For class type, the reference (address) to the class object is passed to the parameters Call-by-reference It is possible to change the data in an object

Parameters of a Primitive Type public void increaseNum(int num) { num++; } public void doStuff() int x = 5; increaseNum(x); System.out.println(x); Prints 5. Why? num is local to increaseNum method; does not change x Parameters are local to the method

Parameters of a Class Type public void changeBook(Book book) { book = new Book(“Biology”); } public void doStuff() Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); Prints Java. Why? book is local to changeBook, does not change jacksBook Parameters are local to the method

Parameters of a Class Type public void changeBook(Book book) { book.setName(“Biology”); } public void doStuff() Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); Prints Biology. Why? book contains the same address as jacksBook! Parameters are local variables, but the reference is passed into the method

Next Class Lab 6