Convert Char to String

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to convert a character char to a string String in Java.


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/object_methods("`Object Methods`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117419{{"`Convert Char to String`"}} java/classes_objects -.-> lab-117419{{"`Convert Char to String`"}} java/class_methods -.-> lab-117419{{"`Convert Char to String`"}} java/modifiers -.-> lab-117419{{"`Convert Char to String`"}} java/oop -.-> lab-117419{{"`Convert Char to String`"}} java/wrapper_classes -.-> lab-117419{{"`Convert Char to String`"}} java/identifier -.-> lab-117419{{"`Convert Char to String`"}} java/arrays -.-> lab-117419{{"`Convert Char to String`"}} java/data_types -.-> lab-117419{{"`Convert Char to String`"}} java/operators -.-> lab-117419{{"`Convert Char to String`"}} java/output -.-> lab-117419{{"`Convert Char to String`"}} java/strings -.-> lab-117419{{"`Convert Char to String`"}} java/variables -.-> lab-117419{{"`Convert Char to String`"}} java/object_methods -.-> lab-117419{{"`Convert Char to String`"}} java/string_methods -.-> lab-117419{{"`Convert Char to String`"}} java/system_methods -.-> lab-117419{{"`Convert Char to String`"}} end

Convert Char to String Using valueOf() Method

  • Open your terminal and create a Java file using the below command:

    touch ConvertCharToString.java
  • Open the file using a text editor and paste the below code:

    public class ConvertCharToString {
      public static void main(String[] args) {
        char ch = 's';
        System.out.println(ch);
        String str = String.valueOf(ch);
        System.out.println(str);
        System.out.println(str.getClass().getName());
      }
    }
  • Compile the Java code using the below command in the terminal:

    javac ConvertCharToString.java
  • Run the Java code using the below command in the terminal:

    java ConvertCharToString
  • The output will be:

    s
    s
    java.lang.String

Convert Char to String Using toString() Method

  • Open the same ConvertCharToString.java file in the text editor and replace the main method with the below code:

    public static void main(String[] args){
      char ch = 's';
      System.out.println(ch);
      String str = Character.toString(ch);
      System.out.println(str);
      System.out.println(str.getClass().getName());
    }
  • Compile the Java code using the below command in the terminal:

    javac ConvertCharToString.java
  • Run the Java code using the below command in the terminal:

    java ConvertCharToString
  • The output will be:

    s
    s
    java.lang.String

Convert Char to String Using + Operator

  • Open the same ConvertCharToString.java file in the text editor and replace the main method with the below code:

    public static void main(String[] args){
      char ch = 's';
      System.out.println(ch);
      String str = ""+ch;
      System.out.println(str);
      System.out.println(str.getClass().getName());
    }
  • Compile the Java code using the below command in the terminal:

    javac ConvertCharToString.java
  • Run the Java code using the below command in the terminal:

    java ConvertCharToString
  • The output will be:

    s
    s
    java.lang.String

Summary

In this lab, you have learned how to convert a character char to a string String in Java using different methods. You can use either valueOf() or toString() methods or plus (+) operator to convert a character to a string.

Other Java Tutorials you may like