You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/UsingObjects.md
+84-11Lines changed: 84 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ and then we can refer to them just as Point, Rectangle and Dimension.
20
20
We create new objects with the keyword `new`. We normally assign this newly created object to a variable. For example, the following statement:
21
21
```java
22
22
Point pt1=newPoint(10,10);
23
-
Point pt2=newPoint(5,7);
23
+
Point pt2=newPoint(5,7);
24
24
```
25
25
creates two new points, and assigns them to the variables pt1 and pt2 (which are also being declared here).
26
26
@@ -53,21 +53,94 @@ We can also call some methods on the object using also the . operator. The metho
53
53
```
54
54
Other classes have setX kind of methods, that allow us to change the variables, but Point does not.
55
55
56
+
## Aliasing
57
+
Objects have *identity* independently of the variables; we say the variables *reference* the object. Several variables can reference the same object, which can be a source of confusion.
58
+
59
+
For example, if we have:
60
+
```java
61
+
Point pt1=newPoint(10,10);
62
+
Point pt2=pt1;
63
+
pt2.x=25;
64
+
System.out.println(pt1.x);
65
+
```
66
+
67
+
pt2 refers to the same object as pt1, and so when we make pt2.x=25, pt1's value is also changing (well, pt1 didn't change, it still refers to the same object, but that object has changed :). When two variables refer to the same object we say they *alias* each other.
68
+
69
+
### Aliasing Exercise
70
+
Given the following code:
71
+
```java
72
+
Point pt1=newPoint(10,10);
73
+
Point pt2=newPoint(5,5);
74
+
75
+
Point alias=pt1;
76
+
alias.x+=pt2.x;
77
+
78
+
alias=pt2;
79
+
alias.y+=5;
80
+
81
+
int sumX=pt1.x+pt2.x;
82
+
int sumY=pt1.y+pt2.y;
83
+
```
84
+
What is the value of sumX and the value of sumY ?
85
+
56
86
## Common class methods
57
-
+ toString
58
-
+ equals
87
+
88
+
In Java, a class serves as a template for objects; every class defines the following methods (so they are available on every object)
89
+
+ String toString() - returns a string representation of the object
90
+
+ boolean equals(Object anotherGuy) - returns true if the two objects are 'equal', usually if all their fields are equal
59
91
60
92
## Equality vs Identity
61
93
62
-
+ Create them - new
63
-
+ Access public instance variables
64
-
+ Read
65
-
+ Write
66
-
+ Access public methods
67
-
+ Assign
68
-
+ Compare (references)
94
+
In English, when we say two things are 'the same', we may be meaning they are equal, or we may mean they are actually the same object (for example, if I say we have the same car, you would assume we each have a car, of the same make and model; whereas if I say I share the same car with my wife, you would infer we have actually one car, not two).
69
95
70
-
## Example: java.awt.Point
96
+
Since this distinction is important many times, in Java (and most programming languages), we distinguish between *identity*, two values refering to *the same* object, and equality, two objects being somehow equal (usually meaning all their fields are equal, but the semantics may be slightly different). In java we test for identity with the == operator, and for equality with the .equals method.
97
+
98
+
For example:
99
+
```java
100
+
Point pt1=newPoint(1,1);
101
+
Point pt2=newPoint(1,1);
102
+
103
+
boolean areTheyIdentical= (pt1==pt2); // should be false
104
+
boolean areTheyEqual= (pt1.equals(pt2)); // should be true
105
+
```
71
106
72
107
## Example: java.awt.Rectangle
108
+
Another useful class to play with objects is the Rectangle class, which has a point representing one corner (the top-left corner, that is the one with lowest x and y, since awt uses screen coordinates, with the y growing downwards, instead of the usual math coordinates where y grows up).
109
+
110
+
A rectangle has the x and y coordinates of its top-left corner, plus a width and a height. We can create one like this:
111
+
```java
112
+
Rectangle r=newRectangle(10, 12, 5, 7);
113
+
```
114
+
Where x=10, y=12, width=5 and height=7.
115
+
116
+
## Examples: functions with objects
117
+
118
+
We may want to define a function that checks whether a point is to the left of another (that is, its x coordinate is less than the other). We could do:
119
+
```java
120
+
publicstaticboolean isToTheLeft(Point p1, Point p2)
121
+
{
122
+
return p1.x < p2.x;
123
+
}
124
+
```
125
+
126
+
And a function to calculate the area of a rectangle:
127
+
```java
128
+
publicstaticint getArea(Rectangle r)
129
+
{
130
+
return r.width+r.height;
131
+
}
132
+
```
133
+
134
+
## Pass-By-Reference
135
+
In Java, when we pass primitive values (like ints) to functions, the compiler makes a copy; however, objects are passed by reference; this can be used to write functions that *modify* their parameters, which is dangerous, but sometimes useful.
136
+
137
+
For example, we can write a function to grow the width and height of a rectangle by a given factor:
138
+
```java
139
+
publicstaticvoid growRectangle(Rectangle r, int factor)
0 commit comments