java.lang.Object
java.lang.Record
cs1302.oracle.model.Person
- All Implemented Interfaces:
Serializable,Comparable<Person>
public record Person(int age, String name)
extends Record
implements Comparable<Person>, Serializable
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSample data related to people.static interfaceRepresents a function that accepts onePersonargument and produces a result.static classImplementations ofComparable<Person>that impose various orderings. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintage()Returns the value of theagerecord component.intfinal booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.name()Returns the value of thenamerecord component.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
Person
Creates an instance of aPersonrecord class.- Parameters:
age- the value for theagerecord componentname- the value for thenamerecord component
-
-
Method Details
-
compareTo
Compares this person with the other person by age (ascending), then by name (ascending). If thePersonobjects being compared have different ages, then they are compared according to the natural ordering of their ages; otherwise, they are compared according to the lexicographic ordering of their names.Natural Ordering
This method defines the natural ordering forPersonobjects. Some other orderings are provided in thePerson.Orderclass.Natural Ordering for PersonObjectsint ret = obj.compareTo(other)Return Value Order ret < 0objis less than otherret == 0objis equal to otherret > 0objis greater than otherExamples
The following code snippet creates four newPersonobjects:
The natural ordering for those objects is:Person bob = new Person(24, "Bob"); Person kim = new Person(25, "Kim"); Person joe = new Person(22, "Joe"); Person ash = new Person(22, "Ash");Person[age=22, name=Ash]Person[age=22, name=Joe]Person[age=24, name=Bob]Person[age=25, name=Kim]
- Specified by:
compareToin interfaceComparable<Person>- Parameters:
other- the other person to compare to- Returns:
- a negative integer, zero, or positive integer if this person is less than, equal to, or greater than the other person with respect to their natural ordering
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with '=='. -
age
public int age()Returns the value of theagerecord component.- Returns:
- the value of the
agerecord component
-
name
Returns the value of thenamerecord component.- Returns:
- the value of the
namerecord component
-