Equality
By default,
equality in Python is just is
(equality of
identity).
In Java, ==
returns true if
the two operands "hold the same value." If the operands
are of object type, the value held by the variable is
the memory address of its object. Hence, for object
types, ==
is equality of identity, just
as with Python's is
.
In object-oriented languages, there are are three steps to properly defininig equality.
- If you are comparing yourself to yourself, return true.
- If you are comparing yourself to an object of
a different type, return false. This is the
species test. Python's
isinstance
function and Java'sinstanceof
operator will take care of this. - Usually, you will declare objects equal if they have the same state.
We will define equality for these Point classes.
Point.py
# Author: Morrison
# Date created: 31 Mar 2022
# Date last modified: 31 Mar 2022
# Program: Driver.py
#
import math
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __str__(self):
return f"({self.x}, {self.y})"
def __eq__(self, other):
return False
def reflection_across_X(self):
return Point(self.x, -self.y)
def distance_to(self, other):
return math.hypot(self.x - other.x, self.y - other.y)
Driver.py
# Author: Morrison
# Date created: 31 Mar 2022
# Date last modified: 31 Mar 2022
# Program: Driver.py
#
from Point import Point
def main():
p = Point(3,4)
print(p)
origin = Point()
print(p.reflection_across_X())
print(p.distance_to(origin))
q = Point(3,4)
print(p == q)
main()
Point.java
public class Point
{
private final int x;
private final int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public Point()
{
this(0,0); //calls sibling constructor
}
public Point reflectionAcrossX()
{
return new Point(x, -y);
}
@Override
public String toString()
{
return String.format("(%s, %s)", x, y);
}
public double distanceTo(Point that)
{
return Math.hypot(x - that.x, y - that.y);
}
}
/**************************************************
* Author: Morrison
* Date: 31 Mar 2022
**************************************************/
public class Driver
{
public static void main(String[] args)
{
Point p = new Point(3,4);
System.out.println(p);
System.out.println(p.reflectionAcrossX());
Point origin = new Point();
System.out.println(p.distanceTo(origin));
}
}
Instance Land and Static Land
When you create an instance of a class for the first time, the
class loader swings into action. It begins by creating the
static area of memory; this area is shared by all
instances of the class you create. Things that go in there are marked
static
.
Static items can be marked private
or
public
. If they are public
, they can
be invoked by the class name.
instance land -| static land -|g -|r -|e -|a -|s -|e -| state variables -| static variables instance methods -| static methods -| -----------------------------------------------
Items in instance land have access to the static area; note the "rungs" on the wall for instance land. Items in the static area have no access to instance land, since every instance of the class can have different state. Things that are static are "instance invariant".
This includes the main
method. The main method can
only access state variables via an instance of the class if it is present
in the class.
Math and static