Skip to content

Commit 83e6f3c

Browse files
committed
using objects
1 parent 0b86701 commit 83e6f3c

1 file changed

Lines changed: 84 additions & 11 deletions

File tree

content/UsingObjects.md

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ and then we can refer to them just as Point, Rectangle and Dimension.
2020
We create new objects with the keyword `new`. We normally assign this newly created object to a variable. For example, the following statement:
2121
```java
2222
Point pt1=new Point(10,10);
23-
Point pt2=new Point(5,7 );
23+
Point pt2=new Point(5,7);
2424
```
2525
creates two new points, and assigns them to the variables pt1 and pt2 (which are also being declared here).
2626

@@ -53,21 +53,94 @@ We can also call some methods on the object using also the . operator. The metho
5353
```
5454
Other classes have setX kind of methods, that allow us to change the variables, but Point does not.
5555

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=new Point(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=new Point(10,10);
73+
Point pt2=new Point(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+
5686
## 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
5991

6092
## Equality vs Identity
6193

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).
6995

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=new Point(1,1);
101+
Point pt2=new Point(1,1);
102+
103+
boolean areTheyIdentical= (pt1==pt2); // should be false
104+
boolean areTheyEqual= (pt1.equals(pt2)); // should be true
105+
```
71106

72107
## 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=new Rectangle(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+
public static boolean 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+
public static int 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+
public static void growRectangle(Rectangle r, int factor)
140+
{
141+
r.width *= factor;
142+
r.height *= factor;
143+
}
144+
```
145+
73146

0 commit comments

Comments
 (0)