
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compare Two Objects of Character Type in Java
To compare two objects of Character type in Java, use the compareTo() method.
Firstly, we have created two Character type.
Character one = new Character('m'); Character two = new Character('k');
Now, to compare them, we have used the compareTo() method.
int res = one.compareTo(two);
The following is the example that compares character objects.
Example
public class Demo { public static void main(String []args) { Character one = new Character('m'); Character two = new Character('k'); System.out.println("Character object 1: "+one); System.out.println("Character object 2: "+two); int res = one.compareTo(two); if (res == 0) { System.out.println("Both are equal!"); } else if (res < 0) { System.out.println("Character 1 is less than Character 2"); } else if (res > 0) { System.out.println("Character 1 is less than Character 2"); } } }
Output
Character object 1: m Character object 2: k Character 1 is less than Character 2
Advertisements