Converting Character to String

JavaJavaBeginner
Practice Now

Introduction

Java toString() method is used to convert Character Object into String. In this lab, you will learn how to use the Character toString() method in Java programming.


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/exceptions("`Exceptions`") 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-117584{{"`Converting Character to String`"}} java/classes_objects -.-> lab-117584{{"`Converting Character to String`"}} java/class_methods -.-> lab-117584{{"`Converting Character to String`"}} java/exceptions -.-> lab-117584{{"`Converting Character to String`"}} java/modifiers -.-> lab-117584{{"`Converting Character to String`"}} java/oop -.-> lab-117584{{"`Converting Character to String`"}} java/packages_api -.-> lab-117584{{"`Converting Character to String`"}} java/user_input -.-> lab-117584{{"`Converting Character to String`"}} java/wrapper_classes -.-> lab-117584{{"`Converting Character to String`"}} java/identifier -.-> lab-117584{{"`Converting Character to String`"}} java/arrays -.-> lab-117584{{"`Converting Character to String`"}} java/comments -.-> lab-117584{{"`Converting Character to String`"}} java/data_types -.-> lab-117584{{"`Converting Character to String`"}} java/operators -.-> lab-117584{{"`Converting Character to String`"}} java/output -.-> lab-117584{{"`Converting Character to String`"}} java/strings -.-> lab-117584{{"`Converting Character to String`"}} java/variables -.-> lab-117584{{"`Converting Character to String`"}} java/object_methods -.-> lab-117584{{"`Converting Character to String`"}} java/string_methods -.-> lab-117584{{"`Converting Character to String`"}} java/system_methods -.-> lab-117584{{"`Converting Character to String`"}} end

Set up Your Work Environment

Create a new Java file named CharToString.java in the ~/project directory using the command:

cd ~/project
touch CharToString.java

Open the file CharToString.java in a text editor of your choice.

Create a Character Object

In this step, you will create a Character object and initialize it with a value.

// CharToString.java

public class CharToString {
    public static void main(String[] args) {
        // create Character object
        Character ch = 'a';
    }
}

In this example, we created a Character object named ch and assigned the value 'a' to it.

Call the toString() Method

In this step, you will use the toString() method to convert the Character object into a String.

// CharToString.java

public class CharToString {
    public static void main(String[] args) {
        // create Character object
        Character ch = 'a';

        // convert Character object to String
        String str = ch.toString();
    }
}

In this example, we called the toString() method on the Character object ch and assigned the returned String value to a new variable named str.

Output the Result

In this step, you will output the result of the toString() method.

// CharToString.java

public class CharToString {
    public static void main(String[] args) {
        // create Character object
        Character ch = 'a';

        // convert Character object to String
        String str = ch.toString();

        // output the result
        System.out.println("The String representation of the Character object is: " + str);
    }
}

In this example, we used the System.out.println() method to output the result of the toString() method.

User Input Example

In this step, you will create an example that allows the user to input a Character value and then converts it to a String.

// CharToString.java

import java.util.Scanner;

public class CharToString {
    public static void main(String[] args) {
        try {
            System.out.print("Enter a Character value: ");
            Scanner sc = new Scanner(System.in);
            Character ch = sc.next().charAt(0);
            String str = ch.toString();

            System.out.println("The String representation of the Character object is: " + str);
        }
        catch(Exception e) {
            System.out.println("Invalid input!!");
        }
    }
}

In this example, we used Scanner to get input from the user and then converted the input Character value to a String using the toString() method.

After writing the above code, save and close the file.

Compile and Run the Code

To compile the code, run the following command from the ~/project directory:

javac CharToString.java

To run the code, run the following command:

java CharToString

Summary

Congratulations! You have successfully learned how to use the Java toString() method to convert a Character object into a String. Now you know how to create a Character object, call the toString() method, and output the result. You have also learned how to create an example that allows users to input a Character value and then convert it to a String.

Other Java Tutorials you may like