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.

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