Java Character toCodePoint Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the toCodePoint() method of the Character class in Java which will convert the specified surrogate pairs to its supplementary code point value.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/classes_objects -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/class_methods -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/modifiers -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/oop -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/wrapper_classes -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/identifier -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/arrays -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/data_types -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/operators -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/output -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/strings -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/variables -.-> lab-117578{{"`Java Character toCodePoint Method`"}} java/system_methods -.-> lab-117578{{"`Java Character toCodePoint Method`"}} end

Create a Java class

Create a Java class CharToCodePoint.

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

    }
}

Declare character values to convert

Declare variables, highOne, lowOne, highTwo and lowTwo, with char values to convert to codepoint.

public class CharToCodePoint {
    public static void main(String[] args) {
        char highOne = '\udd6f';
        char lowOne = '\udc7e';

        char highTwo = 'B';
        char lowTwo = 'c';
    }
}

Convert surrogate pairs into codepoints

Convert surrogate pair variables declared in step 2 to codepoints using the toCodePoint() method.

public class CharToCodePoint {
    public static void main(String[] args) {
        char highOne = '\udd6f';
        char lowOne = '\udc7e';

        char highTwo = 'B';
        char lowTwo = 'c';

        int codePointOne = Character.toCodePoint(highOne, lowOne);
        int codePointTwo = Character.toCodePoint(highTwo, lowTwo);
    }
}

Print the codepoints

Print the variables, codePointOne and codePointTwo, in the console.

public class CharToCodePoint {
    public static void main(String[] args) {
        char highOne = '\udd6f';
        char lowOne = '\udc7e';

        char highTwo = 'B';
        char lowTwo = 'c';

        int codePointOne = Character.toCodePoint(highOne, lowOne);
        int codePointTwo = Character.toCodePoint(highTwo, lowTwo);

        System.out.println("Surrogate Pair converted to codePointOne : " + codePointOne);
        System.out.println("Surrogate Pair converted to codePointTwo : " + codePointTwo);
    }
}

Compile and run code

Compile and run the code in the terminal.

javac CharToCodePoint.java
java CharToCodePoint

Modify the input values

Change the input values in step 2 to check the conversion of codepoint for different surrogate pairs.

public class CharToCodePoint {
    public static void main(String[] args) {
        char highOne = '\ud800';
        char lowOne = '\udc00';

        char highTwo = 'A';
        char lowTwo = 'b';

        int codePointOne = Character.toCodePoint(highOne, lowOne);
        int codePointTwo = Character.toCodePoint(highTwo, lowTwo);

        System.out.println("Surrogate Pair converted to codePointOne : " + codePointOne);
        System.out.println("Surrogate Pair converted to codePointTwo : " + codePointTwo);
    }
}

Compile and run modified code

Compile and run the modified code in the terminal.

javac CharToCodePoint.java
java CharToCodePoint

Summary

In this lab, you have learned how to use the toCodePoint() method of the Character class in Java to change the specified surrogate pairs to its supplementary code point value.

Other Java Tutorials you may like