Docsity

Null, Null Pointer Exception, Call By Reference - Lecture Slides | ESE 112, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
1
Introduction to Programming
ith J f B i
w
ith
J
ava,
f
or
B
eg
i
nner
s
“
Has a
”
Relationship
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
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
ESE112 2
“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
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
ESE112 3
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;
num
n;
bldgName = b;
}
public String getLocation(){
return num + " " + bldgName;
}
}
ESE112 4
pf3

Partial preview of the text

Download Null, Null Pointer Exception, Call By Reference - Lecture Slides | ESE 112 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Introduction to Programming

ith J^ f^ B^ iwith Java, for Beginners “Has a” RelationshipHas 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 Counteron the heap, and initializes it „ c actually allocates space to hold a^

reference^ to a CounterCounter• 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 ESE^

2

“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 typeDormRoomDormRoom „ The UML diagrams^ show instance variables and methodsof Freshman and DormRoom object: „ UML(Universal Modeling Language) industry standard used todescribe classes in OOP ESE

3

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;num n;bldgName = b;} public String getLocation(){return num + " " + bldgName;}}ESE

4

A DormRoom on the Heap > DormRoom room = new DormRoom(208, "Hill");> room.getLocation()"208 Hill" ESE

5

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;}} ESE

6

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() ESE

7

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" ESE

8