本文主要內容:
1、基本類型包裝類
2、System類
3、Math類
4、Arrays類
5、BigInteger、BigDecimal類數據運算
01基本數據類型對象包裝類
A.八種基本類型對應的包裝類
char Character
int Integer
byte Byte
short Short
long Long
float Float
double Double
boolean Boolean
B.Integer類parseInt方法
a:parseInt()
int i = Integer.parseInt("12");
System.out.println(i/2);//6
b:parseInt(String s, int radix)
/*
* Integer類靜態方法parseInt(String s, int radix)
* radix基數,進制
* "110",2 含義 前面的數字是二進制的,但是方法parseInt運行結果都是十進制
* 指定進制的字符串轉換為十進制的整數
*/
public static void function_1(){
int i = Integer.parseInt("110", 2);
System.out.println(i);
}
C.Integer類int轉成字符串
a:使用+與字符串拼接
int i = 3;
String s = i+"";
System.out.println(s+1);//"31"
b:toString(int ,int 進制),任意進制整數轉成任意進制的字符串 (了解)
String s1 = Integer.toString(5,2);
System.out.println(s1);
D.Integer類構造方法
/*
* Integer類構造方法
* Integer (String s)
* 將數字格式的字符串,傳遞到Integer類的構造方法中
* 創建Integer對象,包裝的是一個字符串
* 將構造方法中的字符串,轉成基本數據類型,調用方法,非靜態的, intValue()
*/
public static void function_3(){
Integer in = new Integer("100");
int i = in.intValue();
System.out.println(--i);//99
}
E.Integer類其他方法
Integer類的3個靜態方法
- 十進制轉成二進制 toBinarString(int)
- 十進制轉成八進制 toOctalString(int)
- 十進制轉成十六進制 toHexString(int)
三個方法,返回值都是以String形式出現
a:十進制轉二,八,十六進制
public static void function_1(){
System.out.println(Integer.toBinaryString(99));
System.out.println(Integer.toOctalString(99));
System.out.println(Integer.toHexString(999));
}
b:獲取int的最大值和最小值
/*
* Integer類的靜態成員變量
* MAX_VALUE
* MIN_VALUE
*/
public static void function(){
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
02自動裝箱和自動拆箱
A.自動裝箱與自動拆箱:
JDK1.5新特性
自動裝箱,拆箱的 好處: 基本類型和引用類直接運算
自動裝箱:使用Integer.valueOf(整數值)返回一個封裝了該整數值的Integer對象
自動拆箱:使用Integer對象.intValue()返回Integer對象中封裝的整數值
public static void function(){
//引用類型 , 引用變量一定指向對象
//自動裝箱, 基本數據類型1, 直接變成了對象
Integer in = 1; // Integer in = new Integer(1)
//in 是引用類型,不能和基本類型運算, 自動拆箱,引用類型in,轉換基本類型
//in+1 ==> in.inValue()+1 = 2
//in = 2 自動裝箱
in = in + 1;
System.out.println(in);
}
B.自動裝箱與自動拆箱
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i==j);// false 對象地址
System.out.println(i.equals(j));// true 繼承Object重寫equals,比較的對象數據
System.out.println("===================");
Integer a = 500;//Integer integer=Integer.valueOf(500)
//integer=new Integer(500);
Integer b = 500;
System.out.println(a==b);//false
System.out.println(a.equals(b));//true
System.out.println("===================");
//數據在byte(-128~127)范圍內,JVM不會從新new對象
Integer aa = 127; // Integer aa = new Integer(127)
Integer bb = 127; // Integer bb = aa;
System.out.println(aa==bb); //true
System.out.println(aa.equals(bb));//true
03System類方法
A:currentTimeMillis():用于計算程序的執行時間
public static void function(){
long start = System.currentTimeMillis();//當前時間x-1970年1月1日零時零分零秒
for(int i = 0 ; i < 10000; i++){
System.out.println(i);
}
long end = System.currentTimeMillis();//當前時間y-1970年1月1日零時零分零秒
System.out.println(end - start);//當前時間y-當前時間x
}
B:exit()方法,退出虛擬機,所有程序全停止
public static void function_1(){
while(true){
System.out.println("hello");
System.exit(0);//該方法會在以后的finally代碼塊中使用(講到再說)
}
}
C:static void gc() JVM在內存中,收取對象的垃圾
* 當沒有更多引用指向該對象時,會自動調用垃圾回收機制回收堆中的對象
* 同時調用回收對象所屬類的finalize方法()
public class Person {
public void finalize(){
System.out.println("垃圾收取了");
}
}
public static void function_2(){
new Person();
new Person();
new Person();
new Person();
System.gc();
}
D: static Properties getProperties() 獲取當前操作系統的屬性:例如操作系統名稱
public static void function_3(){
System.out.println( System.getProperties() );
}
E:arraycopy(Object src, int srcPos, Object dest, int destPos, int length) ,復制數組
/*
* System類方法,復制數組
* arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
* Object src, 要復制的源數組
* int srcPos, 數組源的起始索引
* Object dest,復制后的目標數組
* int destPos,目標數組起始索引
* int length, 復制幾個
*/
public static void function_4(){
int[] src = {11,22,33,44,55,66};
int[] desc = {77,88,99,0};
System.arraycopy(src, 1, desc, 1, 2);//將src數組的1位置開始(包含1位置)的兩個元素,拷貝到desc的1,2位置上
for(int i = 0 ; i < desc.length ; i++){
System.out.println(desc[i]);
}
}
04 Math類
A:static double sqrt(double d)返回參數的平方根
public static void function_4(){
double d = Math.sqrt(-2);
System.out.println(d);
}
B:static double pow(double a, double b)a的b次方
public static void function_3(){
double d = Math.pow(2, 3);
System.out.println(d);
}
C:static double floor(double d)返回小于或者等于參數d的最大整數
public static void function_2(){
double d = Math.floor(1.5);
System.out.println(d);
}
D:static double ceil(double d)返回大于或者等于參數d的最小整數
public static void function_1(){
double d = Math.ceil(5.1);
System.out.println(d);
}
E:static int abs(int i)獲取參數的絕對值
public static void function(){
int i = Math.abs(0);
System.out.println(i);
}
F:static double round(doubl d)獲取參數的四舍五入,取整數
public static void function_6(){
double d = Math.round(5.4195);
System.out.println(d);
}
G:static double random() 返回隨機數 0.0-1.0之間
public static void function_5(){
for(int i = 0 ; i < 10 ;i++){
double d = Math.random();
System.out.println(d);
}
}
05Arrays工具類
A:static String toString(數組) 數組轉換為字符串
public static void function_2(){
int[] arr = {5,1,4,6,8,9,0};
String s = Arrays.toString(arr);
System.out.println(s);
}
B:static int binarySearch(數組, 被查找的元素) 數組的二分搜索法,返回元素在數組中出現的索引,元素不存在, 返回的是 (-插入點-1)
public static void function_1(){
int[] arr = {1,4,7,9,11,15,18};
int index = Arrays.binarySearch(arr, 10);
System.out.println(index);
}
C:static void sort(數組) 對數組升序排列
public static void function(){
int[] arr = {5,1,4,6,8,9,0};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
06 BigInteger類
A:BigInteger類的構造方法
傳遞字符串,要求數字格式,沒有長度限制
public static void function(){
BigInteger b = new BigInteger("8465846668464684562385634168451684568645684564564");
System.out.println(b);
BigInteger b1 = new BigInteger("5861694569514568465846668464684562385634168451684568645684564564");
System.out.println(b1);
}
B:BigInteger類四則運算
BigInteger b1 = new BigInteger("5665464516451051581613661405146");
BigInteger b2 = new BigInteger("965855861461465516451051581613661405146");
//計算 b1+b2對象的和,調用方法 add
BigInteger bigAdd = b1.add(b2);//965855867126930032902103163227322810292
System.out.println(bigAdd);
//計算b1-b2對象的差,調用方法subtract
BigInteger bigSub = b1.subtract(b2);
System.out.println(bigSub);
//計算b1*b2對象的乘積,調用方法multiply
BigInteger bigMul = b1.multiply(b2);
System.out.println(bigMul);
//計算b2/b1對象商,調用方法divied
BigInteger bigDiv = b2.divide(b1);
System.out.println(bigDiv);
07BigDecimal類
A:BigDecimal類三則運算
BigDecimal b1 = new BigDecimal("0.09");
BigDecimal b2 = new BigDecimal("0.01");
//計算b1+b2的和,調用方法add
BigDecimal bigAdd = b1.add(b2);
System.out.println(bigAdd);
BigDecimal b3 = new BigDecimal("1");
BigDecimal b4 = new BigDecimal("0.32");
//計算b3-b2的差,調用方法subtract
BigDecimal bigSub = b3.subtract(b4);
System.out.println(bigSub);
BigDecimal b5 = new BigDecimal("1.015");
BigDecimal b6 = new BigDecimal("100");
//計算b5*b6的成績,調用方法 multiply
BigDecimal bigMul = b5.multiply(b6);
System.out.println(bigMul);
}
B:BigDecimal類實現除法
/*
* BigDecimal實現除法運算
* divide(BigDecimal divisor, int scale, int roundingMode)
* int scale : 保留幾位小數
* int roundingMode : 保留模式
* 保留模式 閱讀API文檔
* static int ROUND_UP 向上+1
* static int ROUND_DOWN 直接舍去
* static int ROUND_HALF_UP >= 0.5 向上+1
* static int ROUND_HALF_DOWN > 0.5 向上+1 ,否則直接舍去
*/
public static void function_1(){
BigDecimal b1 = new BigDecimal("1.0301");
BigDecimal b2 = new BigDecimal("100");
//計算b1/b2的商,調用方法divied
BigDecimal bigDiv = b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//0.01301
System.out.println(bigDiv);
}