Java Integer toUnsignedString Method

JavaJavaBeginner
Practice Now

Introduction

In Java, the Integer.toUnsignedString() method is used to convert an integer value into its equivalent unsigned String representation. This method returns the unsigned base 10 equivalent of the integer value passed as a parameter as a String.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL 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/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/user_input("`User Input`") 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/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/classes_objects -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/class_methods -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/modifiers -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/oop -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/user_input -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/wrapper_classes -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/identifier -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/arrays -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/data_types -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/operators -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/output -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/strings -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/variables -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/string_methods -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} java/system_methods -.-> lab-117758{{"`Java Integer toUnsignedString Method`"}} end

Declare the UnsignedString class

In the UnsignedString.java file, declare the UnsignedString class:

public class UnsignedString {

}

Declare the main method

Declare the main method inside the UnsignedString class:

public static void main(String[] args) {

}

Declare an integer variable

Declare an integer variable inside the main method. This variable will hold the integer value that we want to convert to an unsigned String representation.

int num = 2147483647;

Convert integer to unsigned String

Use the Integer.toUnsignedString() method to convert the integer value to its equivalent unsigned String representation:

String unsignedNum = Integer.toUnsignedString(num);

Print the unsigned String

Print the unsigned String to the console:

System.out.println("Unsigned String value of " + num + " is " + unsignedNum);

Compile and run the program

Compile the UnsignedString.java file using the following command:

javac UnsignedString.java

Run the program using the following command:

java UnsignedString

Test with different values

Modify the value of num to test the Integer.toUnsignedString() method with different values.

int num = -2147483648;
String unsignedNum = Integer.toUnsignedString(num);
System.out.println("Unsigned String value of " + num + " is " + unsignedNum);

Test with user input

Modify the program to accept user input:

Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer value: ");
int num = sc.nextInt();
String unsignedNum = Integer.toUnsignedString(num);
System.out.println("Unsigned String value of " + num + " is " + unsignedNum);

Compile and run the program with user input

Compile and run the program with the following commands:

javac UnsignedString.java
java UnsignedString

When prompted, enter an integer value to test the Integer.toUnsignedString() method.

Summary

In this lab, we learned how to use the Integer.toUnsignedString() method in Java to convert an integer value to its equivalent unsigned String representation. We also learned how to test the method with different values and user input.

Other Java Tutorials you may like