- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How does the Java compareTo() method, compare strings?
The compareTo() method compares the Number object that invoked the method to the argument. It is possible to compare Byte, Long, Integer, etc.
However, two different types cannot be compared, both the argument and the Number object invoking the method should be of the same type.
Example
public class Test { public static void main(String args[]) { Integer x = 5; System.out.println(x.compareTo(3)); System.out.println(x.compareTo(5)); System.out.println(x.compareTo(8)); } }
Output
1 0 -1
Advertisements