Java Programming Tips


Creating a Random BigInteger in Java

Java provides a custom class named BigInteger for handling very large integers (numbers which require more than 64 bits). BigInteger provides a constructor using which you can create a random BigInteger value. public BigInteger(int numBits, Random rnd) Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2^numBits – 1), inclusive. The uniformity […]

Using BigInteger in Java

For handling numbers, Java provides built-in types such as int and long. However these types are not useful for very large numbers. When you do mathematical operations with int or long types and if the resulting values are very large, the type variables will overflow causing errors in the program. Java provides a custom class […]

Creating an Inline Array in Java

The usual way of creating an array in Java is given below. In this example, we create a String array containing primary color names, public class InlineArrays { public static void main(String[] args) { String[] colors = {"red","green","blue"}; printColors(colors); } public static void printColors(String[] colors) { for (String c : colors) { System.out.println(c); } } […]

Accessing Outer Class Local Variables from Inner Class Methods

When you try to compile the following Java code, you will get this error, local variable a is accessed from within inner class; needs to be declared final public class Outer { public void test() { int a = 10; Runnable i = new Runnable() { @Override public void run() { int j = a […]