程序代写代做代考 Java html COMP 250

COMP 250
INTRODUCTION TO COMPUTER SCIENCE
Week 4-2 : OOD5 the class Object
Giulia Alberini, Fall 2020

WHAT ARE WE GOING TO DO IN THIS VIDEO?
OOD5
 The Object class

Object CLASS

THE Object CLASS
 Object is the only class in java without a superclass. All other classes have
one and only one direct superclass.
 In the absence of any other specific superclass, every class is implicitly a
subclass of Object.
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

METHODS FROM Object
Here are some of the methods from the Object class:
https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html

HIERARCHY FOR OUR EXAMPLES
Object + hashCode(): int
+ toString(): String
+ equals(o: Object): boolean
extends (automatic)
Animal
extends
extends extends extends
Dog
Poodle
Beagle
Doberman

Object + hashCode(): int
+ toString(): String
+ equals(o: Object): boolean

hashCode()- RETURN VALUE
It returns a 32 bit integer associated to this object.
 “typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language”.
 Use of hashCode() method : Returns a hash value that is used to search object in a collection.

hashCode()- REQUIREMENTS
“Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer.”
 If o1.equals(o2) is true, then o1.hashCode()==o2.hashCode() should also be true.
Note that the converse does not need to hold!
https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode–

EXAMPLE
Object
+ hashCode(): int
+ toString(): String
+ equals(o: Object): boolean
extends (automatic)
String
+ hashCode(): int
+ toString(): String
+ equals(s: Object): boolean
The class String overrides hashCode()

EXAMPLE
The method hashCode() from the class String
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

Object
+ hashCode(): int
+ toString(): String
+ equals(o: Object): boolean

toString()
Returns a string representation of the object.
It is recommended that all subclasses override this method.
 The toString() method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character ‘@’, and the unsigned hexadecimal representation of the hash code of the object.

EXAMPLE
System.out.println( new Object() );
What does this print?
java.lang.Object@7852e922
package + class name
32 bit integer represented in hexadecimal

EXAMPLE
+ hashCode(): int
+ equals(o: Object): boolean
extends (automatic)
String
+ hashCode(): int
+ equals(s: Object): boolean
+ toString(): String
Object
Returns the following:
className + “@” +
Integer.toHexString(hashCode())
toString() is overridden in the class String
Returns the object itself
+ toString(): String

EXAMPLE
+ hashCode(): int
+ equals(o: Object): boolean
extends (automatic)
+ toString(): String
Object
Returns the following:
className + “@” +
Integer.toHexString(hashCode())
toString() is overridden in the class Animal
Returns… depends on your implementation!
Animal
– birth: Date + eat()
+ toString(): String

Object
+ hashCode(): int
+ toString(): String
+ equals(o: Object): boolean

https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html
equals()
see MATH 240

equals() – IMPLEMENTATION
For any non-null reference values obj1 and obj2,
obj1.equals(obj2) returnstrue if and only if
obj1 == obj2 has value true

EXAMPLES
Object
+ hashCode(): int
+ toString(): String
+ equals(o: Object): boolean
extends (automatic)
Animal
– birth: Date
+ eat()
+ equals(o: Object): boolean
Animal overrides the equals() method

EXAMPLES
Object
+ hashCode(): int
+ toString(): String
+ equals(o: Object): boolean
extends (automatic)
Animal
– birth: Date
+ eat()
+ equals(a: Animal): boolean
Animal overloads the equals() method

EXAMPLES
Object
+ hashCode(): int
+ toString(): String
+ equals(o: Object): boolean
extends (automatic)
String
+ hashCode(): int
+ toString(): String
+ equals(s: Object): boolean
String overrides the equals() method

equals() FROM String

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
EXAMPLES
Object
+ hashCode(): int
+ toString(): String
+ equals(o: Object): boolean
extends (automatic)
ArrayList inherits an overridden version of the equals() method
ArrayList :
+ equals(s: Object): Boolean :

equals() FROM List
https://docs.oracle.com/javase/8/docs/api/java/util/List.html

TO LOOK FORWARD TO
 We will be talking more about interfaces like List in a couple of weeks!

In the next video: Modifiers and Inheritance Type Conversion