LTH
LTH

Reputation: 1839

What are the Comparable.compareTo interface semantics?

I don't understand this question. Is it asking the for the method's signature, which is:

public int compareTo(Object o)

or is it: compareTo() returns negative numbers, 0, and positive numbers respectively indicating whether the calling object is less than, equal to, or greater than the specified object?

Upvotes: 0

Views: 905

Answers (3)

duffymo
duffymo

Reputation: 308753

If the target is less than the object that's passed in, the return value is negative; zero if equal; positive if greater than.

It's pretty clear if you read the javadocs for Comparable:

int compareTo(T o)

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

So

int order;
order = "oranges".compareTo("apples"); // greater than zero
order = "oranges".compareTo("oranges"); // zero
order = "oranges".compareTo("plums"); // less than zero

Upvotes: 2

Chris Cashwell
Chris Cashwell

Reputation: 22859

From Wikipedia:

In computer science, the term semantics refers to the meaning of languages, as opposed to their form (syntax). According to Euzenat, semantics "provides the rules for interpreting the syntax which do not provide the meaning directly but constrains the possible interpretations of what is declared." In other words, semantics is about interpretation of an expression. Additionally, the term is applied to certain types of data structures specifically designed and used for representing information content.

Basically, he's asking you what the meaning of input to compareTo(...) and its output are.

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

The instructor is looking for that second answer -- what the return value means. That's the semantics ("meaning") of a method. The signature is the "syntax".

Upvotes: 3

Related Questions