w3resource

Java: Extract the primitive type value from a given BigInteger value

Java Data Type: Exercise-14 with Solution

Extract Primitive Value from BigInteger

Write a Java program to extract the primitive type value from a given BigInteger value.

A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are byte, short, int, long, float, double, Boolean and char. BigInteger() translates the sign-magnitude representation of a BigInteger into a BigInteger. The sign is represented as an integer signum value: -1 for negative, 0 for zero, or 1 for positive. The magnitude is a byte array in big-endian byte-order: the most significant byte is in the zeroth element. A zero-length magnitude array is permissible, and will result in a BigInteger value of 0, whether signum is -1, 0 or 1.

Sample Solution:

Java Code:

import java.math.BigInteger;
public class Main {
    public static void main(String[] args) {
        BigInteger bigval = BigInteger.valueOf(Long.MAX_VALUE);
        System.out.println("\nBigInteger value: "+bigval);
		long val_Long = bigval.longValue();
		System.out.println("\nConvert the said BigInteger to an long value: "+val_Long);
        int val_Int = bigval.intValue();
		System.out.println("\nConvert the said BigInteger to an int value: "+val_Int);
        short val_Short = bigval.shortValue();
        System.out.println("\nConvert the said BigInteger to an short value: "+val_Short);
        byte val_Byte = bigval.byteValue();
        System.out.println("\nConvert the said BigInteger to byte: "+val_Byte);
        long val_ExactLong = bigval.longValueExact();
		System.out.println("\nConvert the said BigInteger to exact long: "+val_ExactLong);
    }
}

Sample Output:

BigInteger value: 9223372036854775807

Convert the said BigInteger to an long value: 9223372036854775807

Convert the said BigInteger to an int value: -1

Convert the said BigInteger to an short value: -1

Convert the said BigInteger to byte: -1

Convert the said BigInteger to exact long: 9223372036854775807

Flowchart:

Flowchart: Java Data Type Exercises - Extract the primitive type value from a given BigInteger value

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to compute the floor division and the floor modulus of the given dividend and divisor.
Next: Write a Java program to get the next floating-point adjacent in the direction of positive and negative infinity from a given float/double number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/datatypes/java-datatype-exercise-14.php