1 Working with References. 2 References Every object variable is a reference to an object Also true when an object is passed as an argument When the object.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Java Review Interface, Casting, Generics, Iterator.
Computer Science 209 Software Development Equality and Comparisons.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Logic & program control part 2: Simple selection structures.
Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Lists Introduction to Computing Science and Programming I.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
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.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
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.
Miscellaneous OOP topics Java review continued. Simple data types & wrapper classes Simple data types are the built-in types provided as part of the Java.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
Intro to Generic Programming Templates and Vectors.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
ECE122 Feb. 22, Any question on Vehicle sample code?
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 3, page 1 Sun Certified Java 1.4 Programmer Chapter 3 Notes Gary Lance
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
As you arrive… FIRST: Grab the handout SECOND: Snarf the code for today’s class THIRD: Explain what’s going on in this code (It’s Example 1 in the code.
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.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Today’s lecture Review of chapter 8 Go over examples.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Winter 2006CISC121 - Prof. McLeod1 Stuff We had better discuss a midterm date… –27 Feb. to 3 March or –6 to 10 March.
Memory Management in Java Mr. Gerb Computer Science 4.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
The Object-Oriented Thought Process Chapter 03
Phil Tayco Slide version 1.0 Created Sep 18, 2017
CSC 205 Java Programming II
Interfaces.
Using local variable without initialization is an error.
Defining Classes II.
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Java Programming Language
CSC 113: Computer programming II
slides created by Ethan Apter
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
Building Java Programs
slides created by Ethan Apter
Java Classes Aliases & Null & This
ArrayLists 22-Feb-19.
Java Programming Language
slides created by Ethan Apter and Marty Stepp
CMPE212 – Reminders Assignment 2 due next Friday.
Review for Midterm 3.
CMSC 202 Constructors Version 9/10.
Chapter 4 Test Review First day
Presentation transcript:

1 Working with References

2 References Every object variable is a reference to an object Also true when an object is passed as an argument When the object is used, the reference is “followed” to find the actual object

3 null References A variable that does not currently point to anything is called a null reference  e.g. String name; // name is null We can check if a variable contains a null reference using the null identifier  e.g. if(name != null){…} Java doesn’t like null references  … but they can be hard to find for instance variables

4 null References Look at StudentReferenceTest.java Note:  Compiler catches null reference in the local variable  null references are never allowed in local variables  … but the compiler can not catch the null reference in the instance variables it doesn’t know “where the reference has been”

5 null References Three situations can arise:  local variable null … compile error  null reference printed/passed … this can be missed initially  null reference used to call a method … this gets a null pointer exception null objects don’t have defined methods

6 The Picture Student s1 = new Student( , “uid”); s1.setFirstName(“Sam”); s1 Student studentNumber = firstName = “Sam” lastName = null...

7 Aliases Student s1 = new Student( , “uid”); s1.setFirstName(“Sam”); Student s2 = s1; s1 s2 Student studentNumber = firstName = “Sam” lastName = null...

8 Aliases s2.setFirstName(“Pat”); System.out.println(s1.getFirstName()); \\prints “Pat” s1 s2 Student studentNumber = firstName = “Pat” lastName = null...

9 Copying If we really do want to copy an object, it has to be done manually Create a new instance, and copy the relevant data over Not an issue if there are no methods that change the object… i.e. for immutable objects  e.g. String objects

10 Immutable Object Reference For strings: String s1 = "Sam"; String s2 = s1; s2 = "Pat"; System.out.println(s1); System.out.println(s2);

11 Changeable Object Reference For students: Student s1 = new Student( ,"u1"); s1.setFirstName("Sam"); Student s2 = s1; s2.setFirstName("Pat"); System.out.println(s1.getFirstName()); System.out.println(s2.getFirstName());

12 The clone Method Many classes contain a clone() method  this returns a copy of the object  i.e. a new object with the relevant info copied e.g. public Student clone() { Student s = new Student(studentNumber, userid); //… copy the rest of the relevant data return s; }

13 Equality and References Compare references not objects Student s1 = new Student( ,”uid”); Student s2 = new Student( ,”uid”); Student s3 = s1; \\now s1==s3 and s1!=s2 s1 s2 Student “uid” s3 Student “uid”

14 The equals Method Many classes define an equals method “equal” depends on the class For students: public boolean equals(Student s) { return studentNumber == s.studentNumber; }

15 The compareTo Method Used for more general comparison: a.compareTo(b) should return:  a negative int if a<b  0 if a==b  a positive int if a>b Used by the built-in sorts  one call to compareTo gives all the info needed about the relative order For students… not 100% clear  studentNumber?  But not all objects are sortable

16 The this Reference It is often convenient/necessary to explicitly refer to members of the current object  e.g. studentNumber == s.studentNumber  This can be confusing… same variable name The special identifier this refers to the object that the code is defining  e.g. this.studentNumber == s.studentNumber  This is more clear

17 Using this in Constructors In constructors, we need to pass a formal parameter to fill a data member public Student(long stunum, …){ studentNumber = stunum; … } Using this can clarify the situation: public Student(long studentNumber, …){ this.studentNumber = studentNumber; … }