Java Classes Aliases & Null & This

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
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.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
Java Software Solutions
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
Chapter 5: Enhancing Classes
Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
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.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
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.
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Chapter 5: Enhancing Classes
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
ECE122 Feb. 22, Any question on Vehicle sample code?
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
SEEM Java – Basic Introduction, Classes and Objects.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Chapter VII: Arrays.
Chapter 5: Enhancing Classes
Variables in Java A variable holds either
User-Written Functions
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Primer 1: Types, Classes and Operators
Chapter 10: Void Functions
Chapter 5: Enhancing Classes
Creating Objects & String Class
Using local variable without initialization is an error.
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.
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
More Object Oriented Programming
Dynamic Memory Allocation
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
CSI 1102 Introduction to Software Design
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Classes, Encapsulation, Methods and Constructors (Continued)
Chapter 5: Enhancing Classes
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Defining Classes and Methods
Pointers Pointers point to memory locations
Java Programming Language
Defining Classes and Methods
C Programming Pointers
Defining Classes and Methods
Outline Creating Objects The String Class The Random and Math Classes
Classes and Objects Object Creation
SPL – PS3 C++ Classes.
CMSC 202 Constructors Version 9/10.
String Objects & its Methods
Presentation transcript:

Java Classes Aliases & Null & This Object references play an important role in a program. An object and an object reference variable are two different things. An object reference variable stores the address of an object even though we do not know the address ourselves. When we use the dot operator to call an object’s method, we are really using the address in the reference variable to the find the object, look up the method, and invoke it.

Parameters Primitives are Passed by Value Formal parameter variables become a copy of the actual parameter The method cannot alter the value of the actual parameter variable Objects are Passed by Reference Values that are passed to a method or constructor are called actual parameters or arguments while the variables in the parameter list of the method declarations are called formal parameters. When an actual parameter is of a primitive type, its value cannot be changed in the method because only the value of the variable is sent to the method.

ChessPiece bishop1 = new ChessPiece(); References An object reference variable holds the memory address of an object Rather than dealing with arbitrary addresses, we often depict a reference graphically as a “pointer” to an object ChessPiece bishop1 = new ChessPiece(); Objects are passed by reference. That means that when an object is passed to a method, the method has the ability to change the sate of the object. When an actual parameter is an object, it is the object reference that is actually the parameter. When the method receives the reference and stores it in a formal parameter variable, the method now has the ability to change the state of the object because it knows where the object exists in memory. The formal parameter is now an alias of the actual parameter. bishop1

Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: num2 = num1; Before num1 5 num2 12 After num1 5 num2

Reference Assignment For object references, assignment copies the memory location: bishop2 = bishop1; Before bishop1 bishop2 After bishop1 bishop2

Aliases Two or more references that refer to the same object are called aliases of each other One object (and its data) can be accessed using different reference variables Aliases can be useful, but should be managed carefully Changing the object’s state (its variables) through one reference changes it for all of its aliases Another way to think of references is to think of a superhero with his or her secret identify. Let’s take Superman as an example. Clark Kent was just another name for the person; he was still Superman the whole time. They were the same guy with the same superpowers, the same attributes and the same ability to perform cool superhero actions. Therefore, “Clark Kent” was simple a reference created to refer to the same guy. If Superman dyed his hair blonde because it was trendy, then both Clark Kent and Superman will have blonde hair. Clark Kent and Superman are two names that reference the same guy, so any changes will be reflected in all the references.

Testing Objects for Equality The == operator compares object references for equality, returning true if the references are aliases of each other bishop1 == bishop2 A method called equals is defined for all objects, but unless we redefine it when we write a class, it has the same semantics as the == operator bishop1.equals(bishop2) We can redefine the equals method to return true under whatever conditions we think are appropriate

Objects as Parameters Parameters in a Java method are passed by value This means that a copy of the actual parameter (the value passed in) is stored into the formal parameter (in the method header) Passing parameters is therefore similar to an assignment statement When an object is passed to a method, the actual parameter (method call) and the formal parameter (method header) become aliases of each other

Passing Objects to Methods What you do with a parameter inside a method may or may not have a permanent effect (outside the method) We will look an example Note the difference between changing the reference and changing the object that the reference points to

ParameterPassing.java ParameterPassing.java sets up three variables (one primitive and two objects) to serve as actual parameters to the changeValues method. It prints their values before and after call the method.

ParameterTester.java ParameterTester.java demonstrates the effects of passing various types of parameters.

Num.java Num.java represents a single integer as an object.

Passing by value

Passing by reference Tell the superman story:

The null Reference An object reference variable that does not currently point to an object is called a null reference The reserved word null can be used to explicitly set a null reference: name = null; or to check to see if a reference is currently null: if (name == null) System.out.println ("Invalid"); A nullPointerException error occurs if you attempt to perform a method on a reference variable that is null. To prevent getting a nullPonterExcpetion error, you can perform a null check. By comparing the reference variable using the == or the != comparator. Do not use the equals method to perform a null check. You’ll get a nullPonterExpception if you do.

The null Reference An object reference variable declared at the class level (an instance variable) is automatically initialized to null The programmer must carefully ensure that an object reference variable refers to a valid object before it is used Attempting to follow a null reference causes a NullPointerException to be thrown Usually a compiler will check to see if a local variable is being used without being initialized

The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through which the method is being executed