Open in App
Not now

# CharBuffer compareTo() method in Java

• Last Updated : 06 Sep, 2022

compareTo() method of java.nio.charBuffer class is used to compare one buffer to another. Two char buffers are compared by comparing their sequences of remaining elements lexicographically, without considering the starting position of each sequence within its corresponding buffer. Pairs of char elements are compared as if by invoking Char.compare(char, char). any other type of object can’t be compared to char buffer. Syntax:

`public int compareTo(CharBuffer that)`

Parameter: This method takes a charbuffer object as a parameter with which this buffer will be compared. Return Value: This method returns a negative integer, zero, or a positive integer as this buffer is less than, equal to, or greater than the given buffer. Below are the examples to illustrate the compareTo() method: Example 1: When both CharBuffer are equal.

## Java

 `// Java program to demonstrate``// compareTo() method``import` `java.nio.*;``import` `java.util.*;` `public` `class` `GFG {` `    ``public` `static` `void` `main(String[] args)``    ``{` `        ``// Declaring the capacity of the cb``        ``int` `capacity1 = ``3``;` `        ``// Creating the CharBuffer``        ``try` `{` `            ``// creating object of charbuffer cb``            ``// and allocating size capacity``            ``CharBuffer cb = CharBuffer.allocate(capacity1);` `            ``// putting the value in cb``            ``cb.put(``'a'``);``            ``cb.put(``'b'``);``            ``cb.put(``'c'``);` `            ``// rewind the float buffer``            ``cb.rewind();` `            ``// print the Charbuffer``            ``System.out.println("Charbuffer cb: "``                               ``+ Arrays.toString(cb.array()));` `            ``// creating object of floatbuffer cb1``            ``// and allocating size capacity``            ``CharBuffer cb1 = CharBuffer.allocate(capacity1);` `            ``// putting the value in cb1``            ``cb1.put(``'a'``);``            ``cb1.put(``'b'``);``            ``cb1.put(``'c'``);` `            ``// rewind the float buffer``            ``cb1.rewind();` `            ``// print the Charbuffer``            ``System.out.println("Charbuffer cb1: "``                               ``+ Arrays.toString(cb1.array()));` `            ``// compare both buffer and store the value into integer``            ``int` `i = cb.compareTo(cb1);` `            ``// if else condition``            ``if` `(i == ``0``)``                ``System.out.println("\nboth buffer are"``                                   ``+ "lexicographically equal");` `            ``else` `if` `(i >= ``0``)``                ``System.out.println("\ncb is lexicographically"``                                   ``+ "greater than cb1");` `            ``else``                ``System.out.println("\ncb is lexicographically"``                                   ``+ "less than cb1");``        ``}` `        ``catch` `(IllegalArgumentException e) {``            ``System.out.println("Exception ``throws` `: " + e);``        ``}` `        ``catch` `(ReadOnlyBufferException e) {``            ``System.out.println("Exception ``throws` `: " + e);``        ``}``    ``}``}`

Output:

```Charbuffer cb: [a, b, c]
Charbuffer cb1: [a, b, c]

both buffer arelexicographically equal```

Example 2: When this FloatBuffer is greater than the passed FloatBuffer

## Java

 `// Java program to demonstrate``// compareTo() method``import` `java.nio.*;``import` `java.util.*;` `public` `class` `GFG {` `    ``public` `static` `void` `main(String[] args)``    ``{` `        ``// Declaring the capacity of the cb``        ``int` `capacity1 = ``3``;` `        ``// Creating the CharBuffer``        ``try` `{` `            ``// creating object of floatbuffer cb``            ``// and allocating size capacity``            ``CharBuffer cb = CharBuffer.allocate(capacity1);` `            ``// putting the value in cb``            ``cb.put(``'g'``);``            ``cb.put(``'b'``);``            ``cb.put(``'c'``);` `            ``// rewind the float buffer``            ``cb.rewind();` `            ``// print the CharBuffer``            ``System.out.println("CharBuffer cb: "``                               ``+ Arrays.toString(cb.array()));` `            ``// creating object of floatbuffer cb1``            ``// and allocating size capacity``            ``CharBuffer cb1 = CharBuffer.allocate(capacity1);` `            ``// putting the value in cb1``            ``cb1.put(``'a'``);``            ``cb1.put(``'b'``);``            ``cb1.put(``'c'``);` `            ``// rewind the float buffer``            ``cb1.rewind();` `            ``// print the CharBuffer``            ``System.out.println("CharBuffer cb1: "``                               ``+ Arrays.toString(cb1.array()));` `            ``// compare both buffer and store``            ``// the value into integer``            ``int` `i = cb.compareTo(cb1);` `            ``// if else condition``            ``if` `(i == ``0``)``                ``System.out.println("\nboth buffer are"``                                   ``+ " lexicographically equal");` `            ``else` `if` `(i >= ``0``)``                ``System.out.println("\ncb is lexicographically"``                                   ``+ " greater than cb1");` `            ``else``                ``System.out.println("\ncb is lexicographically"``                                   ``+ " less than cb1");``        ``}` `        ``catch` `(IllegalArgumentException e) {``            ``System.out.println("Exception ``throws` `: " + e);``        ``}` `        ``catch` `(ReadOnlyBufferException e) {``            ``System.out.println("Exception ``throws` `: " + e);``        ``}``    ``}``}`

Output:

```CharBuffer cb: [g, b, c]
CharBuffer cb1: [a, b, c]

cb is lexicographically greater than cb1```

Example 3: When this FloatBuffer is less than the passed FloatBuffer

## Java

 `// Java program to demonstrate``// compareTo() method` `import` `java.nio.*;``import` `java.util.*;` `public` `class` `GFG {` `    ``public` `static` `void` `main(String[] args)``    ``{` `        ``// Declaring the capacity of the cb``        ``int` `capacity1 = ``3``;` `        ``// Creating the CharBuffer``        ``try` `{` `            ``// creating object of CharBuffer cb``            ``// and allocating size capacity``            ``CharBuffer cb = CharBuffer.allocate(capacity1);` `            ``// putting the value in cb``            ``cb.put(``'a'``);``            ``cb.put(``'b'``);``            ``cb.put(``'c'``);` `            ``// rewind the float buffer``            ``cb.rewind();` `            ``// print the CharBuffer``            ``System.out.println("CharBuffer cb: "``                               ``+ Arrays.toString(cb.array()));` `            ``// creating object of CharBuffer cb1``            ``// and allocating size capacity``            ``CharBuffer cb1 = CharBuffer.allocate(capacity1);` `            ``// putting the value in cb1``            ``cb1.put(``'g'``);``            ``cb1.put(``'b'``);``            ``cb1.put(``'c'``);` `            ``// rewind the float buffer``            ``cb1.rewind();` `            ``// print the CharBuffer``            ``System.out.println("CharBuffer cb1: "``                               ``+ Arrays.toString(cb1.array()));` `            ``// compare both buffer and store the value into integer``            ``int` `i = cb.compareTo(cb1);` `            ``// if else condition``            ``if` `(i == ``0``)``                ``System.out.println("\nboth buffer are"``                                   ``+ "lexicographically equal");``            ``else` `if` `(i >= ``0``)``                ``System.out.println("\ncb is lexicographically"``                                   ``+ "greater than cb1");``            ``else``                ``System.out.println("\ncb is lexicographically"``                                   ``+ "less than cb1");``        ``}` `        ``catch` `(IllegalArgumentException e) {``            ``System.out.println("Exception ``throws` `: " + e);``        ``}` `        ``catch` `(ReadOnlyBufferException e) {``            ``System.out.println("Exception ``throws` `: " + e);``        ``}``    ``}``}`

Output:

```CharBuffer cb: [a, b, c]
CharBuffer cb1: [g, b, c]

cb is lexicographicallyless than cb1```

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.5776964,"math_prob":0.74997497,"size":5575,"snap":"2022-40-2023-06","text_gpt3_token_len":1459,"char_repetition_ratio":0.17250045,"word_repetition_ratio":0.6878049,"special_character_ratio":0.28412557,"punctuation_ratio":0.21499014,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9918078,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-01-28T14:40:22Z\",\"WARC-Record-ID\":\"<urn:uuid:27c0b66d-087b-4e47-9c4e-4ea6c115f1d2>\",\"Content-Length\":\"184370\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fd6cd64e-1ef4-4d9e-aac9-f6f4d5e43e24>\",\"WARC-Concurrent-To\":\"<urn:uuid:3880a2e7-c53f-49bd-8980-6a2a5c5ff3dc>\",\"WARC-IP-Address\":\"23.218.218.77\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/charbuffer-compareto-method-in-java/\",\"WARC-Payload-Digest\":\"sha1:5M6FODLZPBYI7N2BE2PJPA4N3IJDYDIN\",\"WARC-Block-Digest\":\"sha1:VTITD2OVNFG2SXURG3J26AZ2CCQKFOTG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499634.11_warc_CC-MAIN-20230128121809-20230128151809-00248.warc.gz\"}"}