Download presentation
Presentation is loading. Please wait.
1
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.
2
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.
3
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
4
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
5
Reference Assignment For object references, assignment copies the memory location: bishop2 = bishop1; Before bishop1 bishop2 After bishop1 bishop2
6
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.
7
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
8
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
9
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
10
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.
11
ParameterTester.java ParameterTester.java demonstrates the effects of passing various types of parameters.
12
Num.java Num.java represents a single integer as an object.
13
Passing by value
14
Passing by reference Tell the superman story:
15
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.
16
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
17
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.