Prev | Next |
Numbers
When it comes to working with numbers, we use primitive types such as byte, short, int, long, float and double. A pure object oriented language does not support primitive types. In other words, in an object oriented world everything is an object (of some type defined by a class). However, primitive types are convenient to use and hence Java supports them. There are classes equivalent to each of the primitive types as listed below.
- byte and Byte
- short and Short
- int and Integer
- long and Long
- float and Float
- double and Double
All these classes are derived from the parent class Number and hence these classes are referred to as Number types.
Using the class instance of these types clearly has some advantages. For example, the Integer class offers a variety of methods that can manipulate the integer.
The static method parseInt() is used to convert a String to an Integer, provided it is possible.
int x = Integer.parseInt("23"); // x = 23 Integer y = Integer.parseInt("23"); // y = 23 int z = Integer.parseInt("23ab"); // z = 23 int w = Integer.parseInt("ab23"); // throws NumberFormatException
The toString() method can be used to convert an Integer to a String. There is also a static toString() method that accomplishes the same.
Integer x = new Integer(19); // x = 19 String str = x.toString(); // str = "19" String str = Integer.toString(19); // str = "19"
There are a bunch of methods that supports bitwise operations such as left rotation, right rotation, finding the highest and lowest bit position with a 1, changing to other number representations such as to float, double, long, etc.
In a similar fashion, the other Number types also provide several such utility operations. Please refer to Java docs for complete information.
Auto-boxing and Auto-unboxing
As you can observe assigning an Integer object to a variable of primitive type int is possible. Java takes care of the making this conversion in an implicit manner. This feature is called auto-boxing/auto-unboxing.
- When a primtive number type is implicitly converted to its equivalent Number type, it is called as auto-boxing. For example, int to Integer.
- When a Number type is implicitly converted to its equivalent primitive type, it is called as auto-unboxing. For example, Float to float.
Math
The Math class provides wide variety of methods (most of them are static) to perform several mathematical computations. Usage of some of them are shown below.
double d = Math.abs(-3.5); // d = 3.5 int c = Math.ceil(3.5); // c = 4 int f = Math.floor(3.5); // f = 3 float sr = Math.sqrt(9.0); // sr = 3.0 double cr = Math.cbrt(27.0); // cr = 3.0 int p = Math.power(3,4); // p = 81 (3^4) float max = Math.max(5.1,3.9); // max = 5.1 long min = Math.min(5,3); // min = 3
- There are methods to perform trignometric operations sin, cos, tan and their inverses asin, acos, atan.
- The toDegrees() and toRadians() methods can be used to convert the angle from one form to the other.
- The log() and log10() methods compute the natural logarithm (to base e) and logarithm to base 10 respectively.
- The random() method returns a double value in the range 0.0 and 1.0. This number can be multiplied by 10 or 100 and rounded off to get an integer if integer is what is needed.
Random
When a few random numbers are required, Math.random() method suffices. If a series of random numbers need to be generated, it is better to use Random class from java.util package. The following example demonstrates how random numbers can be generated.
import java.util.Random; public class RandomTest { public static void main(String[] args) { Random rand = new Random(); int i = rand.nextInt(); double d = rand.nextDouble(); float f = rand.nextFloat(); long l = rand.nextLong(); } }
The code below shows how random numbers can be generated between 0 to 99.
import java.util.Random; public class RandomRange { public static void main(String[] args) { Random rand = new Random(); int x = rand.nextInt(100); } }