Java Integer toUnsignedLong Method

JavaJavaBeginner
Practice Now

Introduction

Java toUnsignedLong() method is used to convert an integer value into it's equivalent long value after an unsigned conversion. The method returns the long equivalent of the integer value by unsigned conversion. This lab will guide you through the steps to understand how the toUnsignedLong() method works and how you can use it in your Java code.


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/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/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/scope -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/classes_objects -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/class_methods -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/modifiers -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/oop -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/packages_api -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/user_input -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/wrapper_classes -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/identifier -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/arrays -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/data_types -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/operators -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/output -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/strings -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/variables -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/string_methods -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} java/system_methods -.-> lab-117756{{"`Java Integer toUnsignedLong Method`"}} end

Create a Java class file

Create a Java source file ToUnsignedLong.java in the ~/project directory using the following command in the terminal:

touch ~/project/ToUnsignedLong.java

After creating the file, open it in your preferred text editor.

Add Code to Accept User Input

Add the following code to accept integer input from the user:

import java.util.Scanner;

public class ToUnsignedLong {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter an integer value: ");
        int num = input.nextInt();
    }
}

The above code uses the Scanner class to accept integer input from the user and stores it in the variable num.

Convert Integer to Unsigned Long

Add the following code to convert the integer value to it's equivalent unsigned long value using the toUnsignedLong() method:

import java.util.Scanner;

public class ToUnsignedLong {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter an integer value: ");
        int num = input.nextInt();

        long ulong = Integer.toUnsignedLong(num);

        System.out.println("Unsigned long value is: " + ulong);
    }
}

The above code uses the toUnsignedLong() method to convert the integer value to it's equivalent unsigned long value and stores it in the variable ulong. The converted value is then printed to the console.

Test the Code

Compile and run the code using the following commands in the terminal:

javac ~/project/ToUnsignedLong.java
java ToUnsignedLong

After running the code, enter an integer value to test the method.

Try Negative Integer Input

Test the code with negative integer input:

import java.util.Scanner;

public class ToUnsignedLong {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter an integer value: ");
        int num = input.nextInt();

        long ulong = Integer.toUnsignedLong(num);

        System.out.println("Unsigned long value is: " + ulong);
    }
}

The toUnsignedLong() method handles negative integer input by returning it's equivalent unsigned long value. Test the code with a negative integer input and observe the output.

Use Predefined Integer Values

Add the following code to use predefined integer values and convert them to their equivalent unsigned long values:

public class ToUnsignedLong {
    public static void main(String[] args) {
        int a = 10;
        int b = -10;
        int c = Integer.MAX_VALUE;
        int d = Integer.MIN_VALUE;

        long ulongA = Integer.toUnsignedLong(a);
        long ulongB = Integer.toUnsignedLong(b);
        long ulongC = Integer.toUnsignedLong(c);
        long ulongD = Integer.toUnsignedLong(d);

        System.out.println("ULong value for Integer 10 is: " + ulongA);
        System.out.println("ULong value for Integer -10 is: " + ulongB);
        System.out.println("ULong value for Integer.MAX_VALUE is: " + ulongC);
        System.out.println("ULong value for Integer.MIN_VALUE is: " + ulongD);
    }
}

The above code uses predefined integer values and converts them to their equivalent unsigned long values using the toUnsignedLong() method. The converted values are then printed to the console.

Test the Code

Compile and run the code using the following commands in the terminal:

javac ~/project/ToUnsignedLong.java
java ToUnsignedLong

Observe the output for the converted values.

Use Integer.parseInt() Method

Add the following code to use the parseInt() method to accept integer input as a string:

public class ToUnsignedLong {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter an integer value: ");
        String strNum = input.nextLine();

        int num = Integer.parseInt(strNum);
        long ulong = Integer.toUnsignedLong(num);

        System.out.println("Unsigned long value is: " + ulong);
    }
}

The above code accepts the integer input as a string using the nextLine() method. The parseInt() method parses the string and returns the integer value. The toUnsignedLong() method then converts the integer value to it's equivalent unsigned long value which is then printed to the console.

Test the Code

Compile and run the code using the following commands in the terminal:

javac ~/project/ToUnsignedLong.java
java ToUnsignedLong

Enter an integer value to test the code. Observe the output for the converted value.

Summary

In this lab, you learned about the Java toUnsignedLong() method which is used to convert integer values to their equivalent unsigned long values using unsigned conversion. You also learned how to use the toUnsignedLong() method in Java code by following the step-by-step procedure. You can now easily convert integer values to their equivalent unsigned long values using this method in your Java programs.

Other Java Tutorials you may like