Java Character Code Point to String

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about the Java toString(int codePoint) method of the Character class. This method is used to convert a char value into a String object. We will write some code examples to illustrate how this method works and provide you with a better understanding of the concept.


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/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") 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/object_methods("`Object Methods`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117588{{"`Java Character Code Point to String`"}} java/classes_objects -.-> lab-117588{{"`Java Character Code Point to String`"}} java/class_methods -.-> lab-117588{{"`Java Character Code Point to String`"}} java/modifiers -.-> lab-117588{{"`Java Character Code Point to String`"}} java/oop -.-> lab-117588{{"`Java Character Code Point to String`"}} java/packages_api -.-> lab-117588{{"`Java Character Code Point to String`"}} java/user_input -.-> lab-117588{{"`Java Character Code Point to String`"}} java/wrapper_classes -.-> lab-117588{{"`Java Character Code Point to String`"}} java/identifier -.-> lab-117588{{"`Java Character Code Point to String`"}} java/arrays -.-> lab-117588{{"`Java Character Code Point to String`"}} java/comments -.-> lab-117588{{"`Java Character Code Point to String`"}} java/data_types -.-> lab-117588{{"`Java Character Code Point to String`"}} java/operators -.-> lab-117588{{"`Java Character Code Point to String`"}} java/output -.-> lab-117588{{"`Java Character Code Point to String`"}} java/strings -.-> lab-117588{{"`Java Character Code Point to String`"}} java/variables -.-> lab-117588{{"`Java Character Code Point to String`"}} java/object_methods -.-> lab-117588{{"`Java Character Code Point to String`"}} java/string_methods -.-> lab-117588{{"`Java Character Code Point to String`"}} java/system_methods -.-> lab-117588{{"`Java Character Code Point to String`"}} end

Creating a new Java file

First, we need to create a Java file to write our code. Open the terminal and create a file named "CharToString.java" in the "project" directory using the following command:

touch ~/project/CharToString.java

This will create an empty Java file.

Writing the code

Now that we have created the Java file, we will write code that converts a char value into a String object using the toString(int codePoint) method.

public class CharToString {
    public static void main(String[] args) {
        //Creating a char variable
        char ch = 'a';

        //Converting char to String
        String str = Character.toString(ch);

        //Printing the String value
        System.out.println("The String value is: " + str);
    }
}

Compiling the code

To compile the CharToString.java file, open the terminal and navigate to the "project" directory using the following command:

cd ~/project

Once you are in the directory, run the following command to compile the Java file:

javac CharToString.java

This will create a .class file in the same directory.

Running the code

After compiling the Java file, we can run the program using the following command:

java CharToString

This will output the following result:

The String value is: a

The output shows that the char value 'a' has been successfully converted to a String object using the toString(int codePoint) method.

User Input Example

Now, let's create a program to take a user input and convert it to a String using toString(int codePoint) method.

import java.util.Scanner;

public class CharToString {
    public static void main(String[] args) {
        //Taking User Input
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = sc.next().charAt(0);

        //Converting char to String
        String str = Character.toString(ch);

        //Printing the String value
        System.out.println("Character " + ch + " converted to String: " + str);
    }
}

Compiling the code

To compile the updated CharToString.java file, open the terminal and navigate to the "project" directory using the following command:

cd ~/project

Once you are in the directory, run the following command to compile the Java file:

javac CharToString.java

This will create a .class file in the same directory.

Running the code

After compiling the updated Java file, we can run the program using the following command:

java CharToString

This will prompt the user to enter a character. Enter any character as input, and it will output the String representation of that character.

Running the code with a Unicode code point

Now, we will create a program to convert a Unicode code point to its equivalent String object.

public class CharToString {
    public static void main(String[] args) {
        //Creating a Unicode code point variable
        int unicodeCodePoint = 9998;

        //Converting Unicode code point to String
        String str = Character.toString(unicodeCodePoint);

        //Printing the String value
        System.out.println("The String value of Unicode code point " + unicodeCodePoint +
            " is: " + str);
    }
}

Compiling the code

To compile the updated CharToString.java file, open the terminal and navigate to the "project" directory using the following command:

cd ~/project

Once you are in the directory, run the following command to compile the Java file:

javac CharToString.java

This will create a .class file in the same directory.

Running the code

After compiling the code, we can run the program using the following command:

java CharToString

This will output the following result:

The String value of Unicode code point 9998 is: ✎

This example shows that the toString(int codePoint) method can handle the conversion of Unicode code points to String values.

Summary

In this lab, you learned how to convert char values and Unicode code points to their equivalent String values using the toString(int codePoint) method of the Character class. We created example programs that demonstrated how this method works and compiles, and we ran them successfully in the terminal of Ubuntu system.

Other Java Tutorials you may like