cast java.math.BigInteger to java.lang.Long

ww‮.w‬lautturi.com
cast java.math.BigInteger to java.lang.Long

BigInteger can be bigger than Long. If so, then the ".longValue()" will return an exception.

/**
 * @author lautturi.com 
 * Java example: convert biginteger to long in java
 */

import java.math.BigInteger;
import java.util.*;

public class Lautturi {

	public static void main(String[] args) {
		BigInteger bigInteger = new BigInteger(Long.MAX_VALUE + "");
		Long l = bigInteger.longValue();
		
		System.out.println("BigIntegerValue: " + bigInteger);
        System.out.println("LongValue: " + bigInteger.longValue());
	}
}

output:

BigIntegerValue: 9223372036854775807
LongValue: 9223372036854775807
Created Time:2017-10-01 16:54:47  Author:lautturi