Charset compareTo() in Java

compareTo(): This method is available in java.nio.charset.Charset class of Java.

Syntax:

int java.nio.charset.Charset.compareTo(Charset that)

This method takes one argument of type Charset as its parameter. This method compares this charset to another.

Note: Charsets are ordered by their canonical names, without regard to case.

Parameters: One parameter is required for this method.

that: The charset to which this charset is to be compared.

Returns: A negative integer, zero, or a positive integer as this charset is less than, equal to, or greater than the specified charset.

For Example:

Charset cs = Charset.forName("UTF-8")

Charset that = Charset.forName("UTF-8")

cs.compareTo(that) = > It returns true.

Approach

Java

import java.nio.charset.Charset;

public class CharsetcomapareTo {
    public static void main(String[] args) {

        Charset cs = Charset.forName("UTF-8");

        Charset that = Charset.forName("UTF-8");
        System.out.println(cs.compareTo(that));

    }
}

Output:

0

No comments:

Post a Comment