Thursday 16 August 2012

Example for usage of Big Integer: Use of Big Integer to do factorial.


import java.math.BigInteger;
import java.util.ArrayList;


public class TextFactorial {

public static synchronized BigInteger bigNumber(int num){

ArrayList list = new ArrayList();
list.add(BigInteger.valueOf(1));

for (int i=list.size();i<(num+1);i++){
BigInteger lastfact = (BigInteger)list.get(i-1);
BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(i));
list.add(nextfact);
}
return (BigInteger)list.get(num);

}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int num = 23;
System.out.println(TextFactorial.bigNumber(num));
}

}


Big Integer: 任意无限大。 As long as computer support, it could support. 

Java by Example Study Note - Chapter 2


Java by Example 

Study Note - Chapter 2

1. A method with same but different parameter - polymorphism(多态): overloaded (重载)

Example 2.1: 

Class Sub{
}

public class TextConstant {
Int number =0; 
public void run(Object obj){
System.out.println("is Object class: "+ obj);
}
public void run(Sub sub){
System.out.println("is sub class: "+ sub);
}
public static void main(String [] args){
TextConstant constant = new TextConstant();
Constant.run(null);
}
}

Output: Is sub class: null
Reason: When invoke run() method, it will first consider a parameter with class object. 
If the parameter is not class object, only it will consider object class. 
* null will not use for primitive variable like int. 

2. Primitive variable :  int, short, long, byte, float (4byte), double (8 byte), char (2 byte), boolean

Type conversion among primitive variable: can be auto change or force change. 

Rule for auto change: two type must be compatible, and target type must have range bigger than the initial type. 

Example 2.2: 
float f = 1.7f;
double dou = f;
system.out.println("dou: "+dou);

Output:  dou=1.7000000476837158

Rule for force change: put a cast in front with the new type. Will lost accuracy.  

Example 2.3: 
double d = 123.456d; 
byte b = (byte)d;
system.out.println("d: "+d+" b: "+b);
double d = 567.89d; 
byte b = (byte)d;
system.out.println("d: "+d+" b: "+b);

Output: 
d: 123.456 b=123
d: 567.89 b=55
Reason: 
The first d is within one byte (less than 256 (0x100)). So it take all the value in front the decimal. 
The second d is more than one byte (more than 256 (0x100)). So it will first convert 567.89 to 567. Then, 567 is mod with 256 and get the value and assign it to b. 

3. Operate on a variety of operators: 

Calculate operate: involve the problem on lost of accuracy when involve operation with different type of data. To ensure more accurate result, system will auto convert all the data to the type have more accuracy level. 

Example 2.4: 
int a=6, b=5; 
int divideA = a/b; 
float divideB=a/b;
float divideC=a/(b*2f);
System.out.println(divideA +","+divideB+","+divideC); 

Output: 1,1.0,0.6
Reason: 
For divideB: a = int type, b = int type. Largest accuracy : int. All the data will maintain at int type. And final result will convert from int to float. (6/5=1 in int. 1 int to float = 1.0)
For divideC: a=int, b = int, 2F=float. Largest accuracy: float. All the data will upgrade to float type. And final result will maintain in float type. (5*2f=10.0 in float, 6/10.0=0.6 in float too)

Note: 
~ : NOT 
^: XOR 
A+=1; : A=A+1; 
&& (Logical AND): When both side are true, then only true is return. Else, return false. 
If left side=false, right side is not executed. Return false directly. 
|| (Logical OR): When left side = true, return true, right side is not executed. 
If left side = false, return result as result of right side. 
& (Bitwise AND): When left side = false, right side is executed. 
| (Bitwise OR): When leftside = true, right side is executed but the result not concern, return true. 
Logical AND, OR: return type can be boolean type 
Bitwise AND, OR: return type can be boolean type or int type
For bit operator: <<(move left, LSB=0), >>(signed move right, if MSB=1 then MSB=1, if MSB=0, MSB=0), >>> (unsigned move right. MSB=0) 
Bit operator only suitable for the following data type: byte, char, short, int, long. 
<< : left move, multiple by 2. 
>>: right move, divide by 2. 
For int type right move:  a >> b,  if b > 31 . B will be mod with 324*8
a>>32 = a>>0
a>>33 = a>>1
For long type right move: a>>b, if b> 63. B will be mod with 648*8
a>>1 : a value will not change since it not assigned to a. 
a=a>>1: a value will change.