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.

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