本文主要內(nèi)容:
1、基本類型包裝類
2、System類
3、Math類
4、Arrays類
5、BigInteger、BigDecimal類數(shù)據(jù)運(yùn)算
01基本數(shù)據(jù)類型對(duì)象包裝類
A.八種基本類型對(duì)應(yīng)的包裝類
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類靜態(tài)方法parseInt(String s, int radix)
* radix基數(shù),進(jìn)制
* "110",2 含義 前面的數(shù)字是二進(jìn)制的,但是方法parseInt運(yùn)行結(jié)果都是十進(jìn)制
* 指定進(jìn)制的字符串轉(zhuǎn)換為十進(jìn)制的整數(shù)
*/
public static void function_1(){
int i = Integer.parseInt("110", 2);
System.out.println(i);
}
C.Integer類int轉(zhuǎn)成字符串
a:使用+與字符串拼接
int i = 3;
String s = i+"";
System.out.println(s+1);//"31"
b:toString(int ,int 進(jìn)制),任意進(jìn)制整數(shù)轉(zhuǎn)成任意進(jìn)制的字符串 (了解)
String s1 = Integer.toString(5,2);
System.out.println(s1);
D.Integer類構(gòu)造方法
/*
* Integer類構(gòu)造方法
* Integer (String s)
* 將數(shù)字格式的字符串,傳遞到Integer類的構(gòu)造方法中
* 創(chuàng)建Integer對(duì)象,包裝的是一個(gè)字符串
* 將構(gòu)造方法中的字符串,轉(zhuǎn)成基本數(shù)據(jù)類型,調(diào)用方法,非靜態(tài)的, intValue()
*/
public static void function_3(){
Integer in = new Integer("100");
int i = in.intValue();
System.out.println(--i);//99
}
E.Integer類其他方法
Integer類的3個(gè)靜態(tài)方法
- 十進(jìn)制轉(zhuǎn)成二進(jìn)制 toBinarString(int)
- 十進(jìn)制轉(zhuǎn)成八進(jìn)制 toOctalString(int)
- 十進(jìn)制轉(zhuǎn)成十六進(jìn)制 toHexString(int)
三個(gè)方法,返回值都是以String形式出現(xiàn)
a:十進(jìn)制轉(zhuǎn)二,八,十六進(jìn)制
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類的靜態(tài)成員變量
* MAX_VALUE
* MIN_VALUE
*/
public static void function(){
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
02自動(dòng)裝箱和自動(dòng)拆箱
A.自動(dòng)裝箱與自動(dòng)拆箱:
JDK1.5新特性
自動(dòng)裝箱,拆箱的 好處: 基本類型和引用類直接運(yùn)算
自動(dòng)裝箱:使用Integer.valueOf(整數(shù)值)返回一個(gè)封裝了該整數(shù)值的Integer對(duì)象
自動(dòng)拆箱:使用Integer對(duì)象.intValue()返回Integer對(duì)象中封裝的整數(shù)值
public static void function(){
//引用類型 , 引用變量一定指向?qū)ο? //自動(dòng)裝箱, 基本數(shù)據(jù)類型1, 直接變成了對(duì)象
Integer in = 1; // Integer in = new Integer(1)
//in 是引用類型,不能和基本類型運(yùn)算, 自動(dòng)拆箱,引用類型in,轉(zhuǎn)換基本類型
//in+1 ==> in.inValue()+1 = 2
//in = 2 自動(dòng)裝箱
in = in + 1;
System.out.println(in);
}
B.自動(dòng)裝箱與自動(dòng)拆箱
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i==j);// false 對(duì)象地址
System.out.println(i.equals(j));// true 繼承Object重寫equals,比較的對(duì)象數(shù)據(jù)
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("===================");
//數(shù)據(jù)在byte(-128~127)范圍內(nèi),JVM不會(huì)從新new對(duì)象
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():用于計(jì)算程序的執(zhí)行時(shí)間
public static void function(){
long start = System.currentTimeMillis();//當(dāng)前時(shí)間x-1970年1月1日零時(shí)零分零秒
for(int i = 0 ; i < 10000; i++){
System.out.println(i);
}
long end = System.currentTimeMillis();//當(dāng)前時(shí)間y-1970年1月1日零時(shí)零分零秒
System.out.println(end - start);//當(dāng)前時(shí)間y-當(dāng)前時(shí)間x
}
B:exit()方法,退出虛擬機(jī),所有程序全停止
public static void function_1(){
while(true){
System.out.println("hello");
System.exit(0);//該方法會(huì)在以后的finally代碼塊中使用(講到再說)
}
}
C:static void gc() JVM在內(nèi)存中,收取對(duì)象的垃圾
* 當(dāng)沒有更多引用指向該對(duì)象時(shí),會(huì)自動(dòng)調(diào)用垃圾回收機(jī)制回收堆中的對(duì)象
* 同時(shí)調(diào)用回收對(duì)象所屬類的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() 獲取當(dāng)前操作系統(tǒng)的屬性:例如操作系統(tǒng)名稱
public static void function_3(){
System.out.println( System.getProperties() );
}
E:arraycopy(Object src, int srcPos, Object dest, int destPos, int length) ,復(fù)制數(shù)組
/*
* System類方法,復(fù)制數(shù)組
* arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
* Object src, 要復(fù)制的源數(shù)組
* int srcPos, 數(shù)組源的起始索引
* Object dest,復(fù)制后的目標(biāo)數(shù)組
* int destPos,目標(biāo)數(shù)組起始索引
* int length, 復(fù)制幾個(gè)
*/
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數(shù)組的1位置開始(包含1位置)的兩個(gè)元素,拷貝到desc的1,2位置上
for(int i = 0 ; i < desc.length ; i++){
System.out.println(desc[i]);
}
}
04 Math類
A:static double sqrt(double d)返回參數(shù)的平方根
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)返回小于或者等于參數(shù)d的最大整數(shù)
public static void function_2(){
double d = Math.floor(1.5);
System.out.println(d);
}
D:static double ceil(double d)返回大于或者等于參數(shù)d的最小整數(shù)
public static void function_1(){
double d = Math.ceil(5.1);
System.out.println(d);
}
E:static int abs(int i)獲取參數(shù)的絕對(duì)值
public static void function(){
int i = Math.abs(0);
System.out.println(i);
}
F:static double round(doubl d)獲取參數(shù)的四舍五入,取整數(shù)
public static void function_6(){
double d = Math.round(5.4195);
System.out.println(d);
}
G:static double random() 返回隨機(jī)數(shù) 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(數(shù)組) 數(shù)組轉(zhuǎn)換為字符串
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(數(shù)組, 被查找的元素) 數(shù)組的二分搜索法,返回元素在數(shù)組中出現(xiàn)的索引,元素不存在, 返回的是 (-插入點(diǎn)-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(數(shù)組) 對(duì)數(shù)組升序排列
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類的構(gòu)造方法
傳遞字符串,要求數(shù)字格式,沒有長(zhǎng)度限制
public static void function(){
BigInteger b = new BigInteger("8465846668464684562385634168451684568645684564564");
System.out.println(b);
BigInteger b1 = new BigInteger("5861694569514568465846668464684562385634168451684568645684564564");
System.out.println(b1);
}
B:BigInteger類四則運(yùn)算
BigInteger b1 = new BigInteger("5665464516451051581613661405146");
BigInteger b2 = new BigInteger("965855861461465516451051581613661405146");
//計(jì)算 b1+b2對(duì)象的和,調(diào)用方法 add
BigInteger bigAdd = b1.add(b2);//965855867126930032902103163227322810292
System.out.println(bigAdd);
//計(jì)算b1-b2對(duì)象的差,調(diào)用方法subtract
BigInteger bigSub = b1.subtract(b2);
System.out.println(bigSub);
//計(jì)算b1*b2對(duì)象的乘積,調(diào)用方法multiply
BigInteger bigMul = b1.multiply(b2);
System.out.println(bigMul);
//計(jì)算b2/b1對(duì)象商,調(diào)用方法divied
BigInteger bigDiv = b2.divide(b1);
System.out.println(bigDiv);
07BigDecimal類
A:BigDecimal類三則運(yùn)算
BigDecimal b1 = new BigDecimal("0.09");
BigDecimal b2 = new BigDecimal("0.01");
//計(jì)算b1+b2的和,調(diào)用方法add
BigDecimal bigAdd = b1.add(b2);
System.out.println(bigAdd);
BigDecimal b3 = new BigDecimal("1");
BigDecimal b4 = new BigDecimal("0.32");
//計(jì)算b3-b2的差,調(diào)用方法subtract
BigDecimal bigSub = b3.subtract(b4);
System.out.println(bigSub);
BigDecimal b5 = new BigDecimal("1.015");
BigDecimal b6 = new BigDecimal("100");
//計(jì)算b5*b6的成績(jī),調(diào)用方法 multiply
BigDecimal bigMul = b5.multiply(b6);
System.out.println(bigMul);
}
B:BigDecimal類實(shí)現(xiàn)除法
/*
* BigDecimal實(shí)現(xiàn)除法運(yùn)算
* divide(BigDecimal divisor, int scale, int roundingMode)
* int scale : 保留幾位小數(shù)
* 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");
//計(jì)算b1/b2的商,調(diào)用方法divied
BigDecimal bigDiv = b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//0.01301
System.out.println(bigDiv);
}