Charset compareTo() method in Java with Examples Last Updated : 28 Mar, 2019 Comments Improve Suggest changes Like Article Like Report The compareTo() method is a built-in method of the java.nio.charset compares two charsets with each other. A comparison is done by their canonical names, without regard to case. Syntax: public final int compareTo?(Charset second) Parameters: The function accepts a single mandatory parameter second which specifies the charset to be compared with. Return Value: The function returns an integer value which can be negative, positive or zero after comparing both the offsets. Below is the implementation of the above function: Program 1: Java // Java program to demonstrate // the above function import java.nio.charset.Charset; import java.util.Iterator; import java.util.Map; public class GFG { public static void main(String[] args) { // First charset Charset first = Charset.forName("UTF16"); // Second charset Charset second = Charset.forName("UTF-8"); // Compares and print System.out.println(first.compareTo(second)); } } Output: -7 Program 2: Java // Java program to demonstrate // the above function import java.nio.charset.Charset; import java.util.Iterator; import java.util.Map; public class GFG { public static void main(String[] args) { // First charset Charset first = Charset.forName("ISO-2022-CN"); // Second charset Charset second = Charset.forName("ISO-2022-CN"); System.out.println(first.compareTo(second)); } } Output: 0 Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/charset/Charset.html#compareTo-java.nio.charset.Charset- Comment More infoAdvertise with us Next Article Charset compareTo() method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java-Functions Java-NIO package Java-Charset Practice Tags : Java Similar Reads Byte compareTo() method in Java with examples The compareTo() method of Byte class is a built in method in Java which is used to compare the given Byte type object with the instance of Byte invoking the compareTo() method. Syntax ByteObject.compareTo(Byte a) Parameters: It takes a Byte type object a as input which is to be compared with the ins 2 min read Calendar compareTo() Method in Java with Examples The add(Calendar Calendar2) method of Calendar class is used to compare the time values or the millisecond offsets of this Calendar object with the passed Calendar object. Syntax: public int compareTo(Calendar Calendar2) Parameters: The method takes one parameter Calendar2 of Calendar object type an 2 min read Java File Class compareTo() Method with Examples The compareTo() function compares two pathnames lexicographically. In Java, the method may be used to sort files. This type of activity is dependent on the system on which JVM is installed. When comparing pathnames on Unix systems, the alphabetic case matters, while it doesn't on Windows. Syntax: pu 1 min read Java Guava | Chars.compare() method with Examples Chars.compare() method of Guava's Chars Class is used to compare the two specified char values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative. Syntax: public static int compa 2 min read Boolean compareTo() method in Java with examples The compareTo() method of Boolean class is a built in method in Java which is used to compare the given Boolean instance with the current instance. Syntax: BooleanObject.compareTo(Boolean a) Parameters: It takes a Boolean value a as parameter which is to be compared with the current instance. Return 2 min read Like