Regarding the sentence "compareTo"

0

I have a question about the compareTo statement.

If I have:

if(x.compareTo(y)==0)

I understand that I am asking if the value of x is equal to and . But if I want to ask if they are different? Should I place a denial forward?

if(!(x.compareTo(y)==0))

I do not understand how to ask that question.

The second thing that causes me noise is when I want to ask if x is greater than or equal to and . Should I write it as:

if(x.compareTo(y)>0 && x.compareTo(y)==0)

Or is there a more summarized form?

Thank you very much.

    
asked by Jazzy 12.06.2018 в 20:06
source

1 answer

2

The compareTo returns 3 possible outcomes 1, 0, and -1. For the cases that you give of example, return 1 if 'x' is greater than 'y', return 0 if 'x' is equal to 'y' and return -1 if 'x' is less than 'y'. So to know if 'x' is greater than or equal to 'and' you should ask:

if (x.compareTo(y) >= 0) { ... }
    
answered by 12.06.2018 в 20:16