Java Integer String Representation

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the toString() method of the Integer class in Java. We will see how to use this method to get the String representation of an Integer object. We will also see some examples to understand its usage.


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-117754{{"`Java Integer String Representation`"}} java/classes_objects -.-> lab-117754{{"`Java Integer String Representation`"}} java/class_methods -.-> lab-117754{{"`Java Integer String Representation`"}} java/exceptions -.-> lab-117754{{"`Java Integer String Representation`"}} java/modifiers -.-> lab-117754{{"`Java Integer String Representation`"}} java/oop -.-> lab-117754{{"`Java Integer String Representation`"}} java/packages_api -.-> lab-117754{{"`Java Integer String Representation`"}} java/user_input -.-> lab-117754{{"`Java Integer String Representation`"}} java/wrapper_classes -.-> lab-117754{{"`Java Integer String Representation`"}} java/identifier -.-> lab-117754{{"`Java Integer String Representation`"}} java/arrays -.-> lab-117754{{"`Java Integer String Representation`"}} java/comments -.-> lab-117754{{"`Java Integer String Representation`"}} java/data_types -.-> lab-117754{{"`Java Integer String Representation`"}} java/operators -.-> lab-117754{{"`Java Integer String Representation`"}} java/output -.-> lab-117754{{"`Java Integer String Representation`"}} java/strings -.-> lab-117754{{"`Java Integer String Representation`"}} java/variables -.-> lab-117754{{"`Java Integer String Representation`"}} java/object_methods -.-> lab-117754{{"`Java Integer String Representation`"}} java/string_methods -.-> lab-117754{{"`Java Integer String Representation`"}} java/system_methods -.-> lab-117754{{"`Java Integer String Representation`"}} end

Set up the environment

Create a new Java file called IntegerToString.java.

public class IntegerToString {
    public static void main(String[] args) {
        // code to be added
    }
}

Convert an Integer object to String using toString()

In this step, we will create an integer object and then use the toString() method to convert that integer object into string.

public class IntegerToString {
    public static void main(String[] args) {
        // create an Integer object
        Integer num = 35;

        // convert integer to string
        String str = num.toString();

        // print the string
        System.out.println("String representation of the Integer: " + str);
    }
}

Output:

String representation of the Integer: 35

Convert a negative Integer to String using toString()

The toString() method also works for negative integers. In this step, we will convert a negative integer to its String representation.

public class IntegerToString {
    public static void main(String[] args) {
        // create a negative Integer object
        Integer num = -18;

        // convert negative integer to string
        String str = num.toString();

        // print the string
        System.out.println("String representation of the Integer: " + str);
    }
}

Output:

String representation of the Integer: -18

Convert an input integer to String using toString()

In this step, we will create an input integer using Scanner and then use the toString() method to get its String representation.

import java.util.Scanner;

public class IntegerToString {
    public static void main(String[] args) {
        // create scanner object
        Scanner sc = new Scanner(System.in);

        // take input integer from user
        System.out.print("Enter an integer: ");
        int num = sc.nextInt();

        // convert integer to string
        String str = Integer.toString(num);

        // print the string
        System.out.println("String representation of the entered integer: " + str);
    }
}

Output:

Enter an integer: 56
String representation of the entered integer: 56

Handle invalid input

In step 4, we did not handle the case when the input is not an integer. In this step, we will handle the exception when the input is not a valid integer.

import java.util.Scanner;

public class IntegerToString {
    public static void main(String[] args) {
        // create scanner object
        Scanner sc = new Scanner(System.in);

        try {
            // take input integer from user
            System.out.print("Enter an integer: ");
            int num = sc.nextInt();

            // convert integer to string
            String str = Integer.toString(num);

            // print the string
            System.out.println("String representation of the entered integer: " + str);
        } catch(Exception e) {
            System.out.println("Invalid input");
        }
    }
}

Output:

Enter an integer: 34
String representation of the entered integer: 34

Convert an integer to binary using toBinaryString()

In Java, we can use toBinaryString() method of Integer class to convert an integer to its binary string representation.

public class IntegerToString {
    public static void main(String[] args) {
        // create an integer
        int num = 10;

        // convert integer to binary string
        String binaryString = Integer.toBinaryString(num);

        // print binary string
        System.out.println("Binary string representation of the Integer: " + binaryString);
    }
}

Output:

Binary string representation of the Integer: 1010

Convert an integer to hexadecimal using toHexString()

In Java, we can use toHexString() method of Integer class to convert an integer to its hexadecimal string representation.

public class IntegerToString {
    public static void main(String[] args) {
        // create an integer
        int num = 255;

        // convert integer to hex string
        String hexString = Integer.toHexString(num);

        // print hex string
        System.out.println("Hexadecimal string representation of the Integer: " + hexString);
    }
}

Output:

Hexadecimal string representation of the Integer: ff

Summary

In this lab, we learned how to use the toString() method of the Integer class to convert an integer to a string representation. We also learned how to handle invalid input and convert an integer to a binary or hexadecimal string representation. This method is useful when we need to convert an integer into a string for use in different contexts.

Other Java Tutorials you may like