• Keine Ergebnisse gefunden

Hinweise und Bemerkungen zum 1. Übungsblatt der Vorlesung

N/A
N/A
Protected

Academic year: 2022

Aktie "Hinweise und Bemerkungen zum 1. Übungsblatt der Vorlesung"

Copied!
3
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. Markus Reitz

Technische Universität Kaiserslautern Fachbereich Informatik AG Softwaretechnik

Hinweise und Bemerkungen zum 1. Übungsblatt der Vorlesung

„Fortgeschrittene Aspekte objektorientierter Programmierung (SS 2008)“

Aufgabe 1 Identität vs. Gleichheit von Objekten

a) Es ergibt sich die Ausgabe a1==a2: true b1==b2: false a1.equals(a2): true b1.equals(b2): true

b) Bei einem Stringvergleich mittelsequals-Methode müssen im Worst-Case-Szenario alle Zeichen der zu verglei- chenden Strings betrachtet werden. Somit ist die Worst-Case-KomplexitätO(n).

c) Die Lösung besteht in der Verwendung derintern-Methode, welche den String in den internen Stringpool einfügt und dadurch sicherstellt, daß jeder String nur genau einmal vorhanden ist. Somit können Stringvergleiche mittels einfachem Referenzvergleich durchgeführt werden, da die (eindeutigen) Repräsentanten verglichen werden.

S t r i n g a1="A" . i n t e r n ( ) ; S t r i n g a2="A" . i n t e r n ( ) ;

S t r i n g b1=new S t r i n g ( "B" ) . i n t e r n ( ) ; S t r i n g b2=new S t r i n g ( "B" ) . i n t e r n ( ) ; S y s t e m . o u t . p r i n t l n ( " a1==a2 :"+( a1==a2 ) ) ; S y s t e m . o u t . p r i n t l n ( " b1==b2 :"+( b1==b2 ) ) ;

S y s t e m . o u t . p r i n t l n ( " a1 . e q u a l s ( a2 ) :"+( a1 . e q u a l s ( a2 ) ) ) ; S y s t e m . o u t . p r i n t l n ( " b1 . e q u a l s ( b2 ) :"+( b1 . e q u a l s ( b2 ) ) ) ;

Damit ergibt sich die Ausgabe a1==a2: true

b1==b2: true

a1.equals(a2): true b1.equals(b2): true

d) String istimmutable, d.h. ein String kann nicht verändert werden. Somit kann die durch den Stringpool hergestellte Eindeutigkeit niemals zerstört werden.

Aufgabe 2 equals & hashCode

a) Die JDK-Beschreibung derequals-Methode lautet wie folgt:

Indicates whether some other object is „equal to“ this one. Theequalsmethod implements an equivalence relation on non-null object references:

• It is reflexive: for any non-null reference value x,x.equals(x)should return true.

• It is symmetric: for any non-null reference values x and y,x.equals(y)should return true if and only if y.equals(x)returns true.

• It is transitive: for any non-null reference values x, y, and z, ifx.equals(y)returns true andy.equals(z) returns true, thenx.equals(z)should return true.

(2)

• It is consistent: for any non-null reference values x and y, multiple invocations ofx.equals(y)consistently return true or consistently return false, provided no information used inequalscomparisons on the objects is modified.

• For any non-null reference value x,x.equals(null)should return false.

The equals method for classObjectimplements the most discriminating possible equivalence relation on objects;

that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).

Note that it is generally necessary to override thehashCodemethod whenever this method is overridden, so as to maintain the general contract for thehashCodemethod, which states that equal objects must have equal hash codes.

Die JDK-Beschreibung derhashCode-Methode lautet wie folgt:

Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided byjava.util.Hashtable.

The general contract ofhashCodeis:

• Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCodemethod must consistently return the same integer, provided no information used inequalscom- parisons on the object is modified. This integer need not remain consistent from one execution of an applica- tion to another execution of the same application.

• If two objects are equal according to theequals(Object)method, then calling thehashCodemethod on each of the two objects must produce the same integer result.

• It is not required that if two objects are unequal according to theequals(java.lang.Object) method, then calling thehashCodemethod on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

As much as is reasonably practical, thehashCodemethod defined by classObjectdoes return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java(TM) programming language.)

b) Die Implementierung verletzt die geforderte Eigenschaft (siehe Beschreibung derequals-Methode in der vorher- gehenden Teilaufgabe) der Transitivität. Das Codefragment

. . .

Employee e m p l o y e e 1=new Employee ( " J o h n " , " Doe " , 4 2 ) ; Employee e m p l o y e e 2=new Employee ( " J o h n " , " Doe " , 1 7 0 1 ) ; P e r s o n p e r s o n=new P e r s o n ( " J o h n " , " Doe " ) ; S y s t e m . o u t . p r i n t l n ( e m p l o y e e 1 . e q u a l s ( p e r s o n ) ) ; S y s t e m . o u t . p r i n t l n ( p e r s o n . e q u a l s ( e m p l o y e e 2 ) ) ; S y s t e m . o u t . p r i n t l n ( e m p l o y e e 1 . e q u a l s ( e m p l o y e e 2 ) ) ;

. . .

liefert die Ausgabe true

true false

Gemäß Transitivitätseigenschaft vonequals(siehe API Dokumentation) müßte jedoch gelten a∼b∧b∼c→a∼c

c) Erzwingen der Transitivitätseigenschaft durch Verhinderung eines über Subtypen hinwegreichenden Vergleichs (Slice Vergleich). Statt einem Vergleich mittelsinstanceofwerden die beiden betreffendenClassObjekte mit- einander verglichen.

d) Die Person-Instanz

P e r s o n p e r s o n=new P e r s o n ( "Max" , " M ü l l e r " ) ;

ist durch die Änderung nicht mehr gleich der Mitarbeiter-Instanz

M i t a r b e i t e r m i t a r b e i t e r=new M i t a r b e i t e r ( "Max" , " M ü l l e r " , 1 7 0 1 ) ;

2

(3)

Allgemein gesagt: „[...] equals() methods that perform semantically different comparisons in a class hierarchy and allow mixed-type comparison can never be transitive.“ (siehehttp://www.angelikalanger.com/Articles/

JavaSolutions/SecretsOfEquals/Equals.html).

3

Referenzen

ÄHNLICHE DOKUMENTE

[r]

Prove the properties of the muiltiset ordering following definition 8.22 on slide 284..

Prove the properties of the muiltiset ordering following definition 8.22 on slide 284..

Betrachte Beispiel 3.12 von Folie 169, die Arithmetik der

(1) Man beachte, dass die partiellen Ableitungen im Gegensatz zu den gew¨ ohn- lichen Ableitungen nicht durch Striche (oder Punkte im Falle der zeitlichen Ableitung)

Wir werden in diesem Abschnitt einige wichtige Anwendungen der Taylorschen Formel behandeln: Das totale Differenzial als lineare N¨ aherung, die Fehlerrechnung, die Theorie der

[r]

• Lokale Variablen sind nur im eigenen Funktionsrumpf sichtbar, nicht in den aufgerufenen Funktionen.. • Damit die aufgerufenen Hilfsfunktionen auf b zugreifen können, muss b