Studyon Minte9.com
Fundamentals (Docs)

Study

Numbers



When working with numbers, most of the time you use the primitive types in your code. For example:

	int i = 500;
	float b = 3.65f;
	byte c = 0xff;

There are, however, reasons to use objects in place of primitives, and the Java platform provides wrapper classes 
for each of the primitive data types.

	Integer x, y;

All of the numeric wrapper classes are subclasses of the abstract class Number

	Number -> Byte, Double, Float, Integer, Short, Long

There are three reasons that you might use a Number object rather than a primitive:

	- As an argument of a method that expects an object
	- To use constants defined by the class, such as MIN_VALUE and MAX_VALUE
	- To use class methods for converting values to and from other primitive types

Methods Implemented by all Subclasses of Number are:

	byte btyeValue()
	int intValue()
	...
	// Converts the value of this Number object to the primitive data type returned

	int compareTo(Byte anotherByte)
	int compareTo(Float anotherFloat)
	int compareTo(Integer anotherInteger)
	...
	// Compares this Number object to the argument

	boolean equals(Object obj)
	// Determines whether this number object is equal to the argument


Conversion Methods, Integer Class
	
	static Integer decode(String s) // decodes a string into an integer
	static Integer parseInt(String s) // returns an integer (decimal only)
	String toSting() // returns a String object representing the value of this Integer
	...


Formatting Numeric

The java.io package includes a PrintStream class that has two formatting methods that you can use to replace 
print and println. 
These methods, format and printf, are equivalent to one another.

	int i = 435;
        System.out.format("The value of i is: %d%n", i); // The value of i is: 435
	System.out.printf("The value of i is: %d%n", i); // The value of i is: 435

The %d specifies that the single variable is a decimal integer. 
The %n is a platform-independent newline character.


Math class

The Java programming language supports basic arithmetic with its arithmetic operators: +, -, *, /, and %. 
The Math class in the java.lang package provides methods and constants for doing more advanced 
mathematical computation.

Using the static import language feature, you don't have to write Math in front of every math function:

	import static java.lang.Math.*;

This allows you to invoke the Math class methods by their simple names. For example:
	
	int angle = 0;
        System.out.println(cos(angle)); // 1.0

The Math class includes two constants:

	- Math.E, which is the base of natural logarithms
	- Math.PI, which is the ratio of the circumference of a circle to its diameter


Basic methods

	int abs(int i)
	double ceil(double d)
	double floor(double d)
	int round(float f)
	int min(int arg1, int arg2)
	int max(int arg1, int arg2)
	...
	

	double a = -1.000;
        System.out.printf("The absolute value of %.3f is %.3f%n", a, Math.abs(a));
            // The absolute value of -1.000 is 1.000
        
        double b = 11.11;
        System.out.printf("The ceil of %.2f is %.0f%n", b, Math.ceil(b));
            // The ceil of 11.11 is 12
        System.out.printf("The floor of %.2f is %.0f%n", b, Math.floor(b));
            // The floor of 11.11 is 11
        System.out.printf("The rint of %.2f is %.0f%n", b, Math.rint(b)); // closest in value to the argument
            // The rint of 11.11 is 11
        
        int c = 10, d = 20;
        System.out.printf("The max of %d and %d is %d%n", c, d, Math.max(c, d));
            // The max of 10 and 20 is 20

Exponential and Logarithmic Methods

	double exp(double d)
	double log(double d)
	double pow(double base, double exponent)
	double sqrt(double d)

	double x = 2;
        System.out.printf("The value of e: %.4f%n", Math.E);
            // The value of e: 2.7183
        System.out.printf("exp(%.3f) is %.3f%n", x, Math.exp(x));
            // exp(2.000) is 7.389
            // Returns the base of the natural logarithms, e, to the power of the argument.
        
        int y = 2;
        int z = 3;
        //System.out.printf("pow(%d, %d) is %d%n", y, z, Math.pow(y, z));
            // error
        System.out.printf("pow(%d, %d) is %.2f%n", y, z, Math.pow(y, z));
            // 8.00
        
        double w = 9;
        System.out.printf("sqrt(%.2f) is %.2f%n", w, Math.sqrt(w));
            // sqrt(9.00) is 3.00


Random Numbers

The random() method returns a pseudo-randomly selected number between 0.0 and 1.0. 

	int number = (int)(Math.random() * 10);

Using Math.random works well when you need to generate a single random number.
If you need to generate a series of random numbers, you should create an instance of java.util.Random and 
invoke methods on that object to generate numbers.


http://docs.oracle.com/javase/tutorial/java/data/index.html