Docsity

Java Programming for Beginners: Understanding References, Null, and Composition, Study notes of Computer Science

Toggle sidebar
bg1
1
Introduction to Programming
with Java, for Beginners
“Has a” Relationship
Null, NullPointerException
Call by reference
References Recap
The value of reference variable is
Either “null” or a “heap address”
null means currently not pointing at any location
Counter c = new Counter();
new Counter() allocates space for the actual Counter
on the heap, and initializes it
cactually allocates space to hold a reference to a
Counter
•c is placed on the stack and existent as long as
¾Method does not return (if declared in a method)
¾Dr. J Interactions pane is not reset (if declared in Interactions
pane)
The assignment makes crefer to the new Counter
“Has a” Relationship or Composition
An object of type A has an instance variable which is an
object whose type is B. (A “has a” B)
E.g: A Freshman object whose room is of reference type
DormRoom
The UML diagrams show instance variables and methods
of Freshman and DormRoom object:
UML(Universal Modeling Language) industry standard used to
describe classes in OOP
DormRoom Code
> DormRoom room = new DormRoom(208, "Hill");
> room.getLocation()
"208 Hill"
public class DormRoom{
private int num;
private String bldgName;
public DormRoom(int n, String b){
num = n;
bldgName = b;
}
public String getLocation(){
return num + " " + bldgName;
}
}
pf3

Partial preview of the text

Download Java Programming for Beginners: Understanding References, Null, and Composition and more Study notes Computer Science in PDF only on Docsity!

Introduction to Programming

with Java, for Beginners

“Has a” RelationshipNull, NullPointerExceptionCall by reference

References Recap

„

The value of reference variable is

„ Either “null” or a “heap address” „ null

means currently not pointing at any location

„

Counter c = new Counter();

„ new Counter()

allocates space for the actual Counter

on the heap, and initializes it „ c

actually allocates space to hold a

reference

to a

Counter

  • c is placed on the stack and existent as long as

¾^

Method does not return (if declared in a method) ¾^

Dr. J Interactions pane is not reset (if declared in Interactionspane)

„ The assignment makes

c

refer

to

the new Counter

“Has a” Relationship or Composition

„

An object of type A has an instance variable which is anobject whose type is B. (A “has a” B) „

E.g: A Freshman object whose room is of reference typeDormRoom „

The UML diagrams

show instance variables and methods

of Freshman and DormRoom object:^ „

UML(Universal Modeling Language) industry standard used todescribe classes in OOP

DormRoom Code

DormRoom room = new DormRoom(208, "Hill");> room.getLocation()"208 Hill"

public class DormRoom{

private int num;private String bldgName;public DormRoom(int n, String b){

num = n;bldgName = b; } public String getLocation(){

return num + " " + bldgName; } }

A DormRoom on the Heap

DormRoom room = new DormRoom(208, "Hill");> room.getLocation()"208 Hill"

Freshman Code^ > DormRoom room = new DormRoom(208, "Hill");> Freshman f = new Freshman("jo", room);> f.getName()"jo"> (f.getRoom()).getLocation()"208 Hill"

public class Freshman{

private String name;private DormRoom room;public Freshman(String n, DormRoom r){

name = n;room = r; } public String getName(){ return name;}public DormRoom getRoom(){ return room;} }

Note on Operators

Dot operator and parentheses for methodcalls have same precedence

Associativity L to R

f.getRoom().getLocation() is equivalent to(f.getRoom()).getLocation()

A Freshman on the Heap

DormRoom room = new DormRoom(208, "Hill");> Freshman f = new Freshman("jo", room);> f.getName()"jo"> f.getRoom().getLocation()"208 Hill"