- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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
- Related Articles
- How to compare two objects in JavaScript?
- How to compare two JavaScript Date Objects?
- Compare two Strings in Java
- Compare Two Java int Arrays in Java
- Compare array of objects - JavaScript
- Compare two strings lexicographically in Java.
- Compare two file paths in Java
- Compare Two Java long Arrays
- Compare Two Java double arrays
- Compare the two Strings lexicographically in Java
- How to compare two arrays in Java?
- How to compare two dates in Java?
- How to Compare two String in Java?
- Compare two Strings lexicographically in Java programming
- Program to Compare two strings in Java

Advertisements