Java API 工具类

基本类型包装类

八大类型包装类

  • char Character
  • int Integer
  • byte Byte
  • short Short
  • long Long
  • float Float
  • boolean Boolean

parseInt 字符串转整数

这是一个Integer类下的静态方法,直接类名调用

Integer.parseInt("111")

另外一个重载方法,两个参数,第二个参数为基数,按进制数转换

Integer.parseInt("111", 2)

toString 整数转字符串

int i = 3;
String s = i+"";
// 或者这种形式:
String s1 = Integer.toString(5, 2);

构造方法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
    }

进制转换

  • Integer.toBinaryString
  • Integer.toOctalString
  • Integer.toHexString

最大最小值

  • Integer.MAX_VALUE
  • Integer.MIN_VALUE

自动装箱和拆箱

JDK1.5新特性

自动装箱,拆箱的 好处: 基本类型和引用类直接运算

自动装箱:使用Integer.valueOf(整数值)返回一个封装了该整数值的Integer对象

自动拆箱:使用Integer对象.intValue()返回Integer对象中封装的整数值


Integer in = 1; // Integer in = new Integer(1)

    //in+1  ==> in.inValue()+1 = 2    
	//in = 2    自动装箱
	in = in + 1;

这个特性的一个弊端是,可能会造成空指针异常

需要注意几个问题:

 	Integer a = 500;//Integer integer=Integer.valueOf(500)
 	Integer b = 500; //integer=new Integer(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

System 类方法

currentTimeMillis 获取毫秒值

  • long start = System.currentTimeMillis();//当前时间x-1970年1月1日零时零分零秒

exit

public static void function_1(){
    while(true){
	    System.out.println("hello");
	 	System.exit(0);//该方法会在以后的finally代码块中使用(讲到再说)
	 }
}

gc

public class Person {
	public void finalize(){
		System.out.println("垃圾收取了");
	}
}

public static void function_2(){
 	new Person();
 	System.gc();
 }

getProperties

获取操作系统的属性,如操作系统名称,java环境等

arraycopy

/*
* 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]);
  }
}

Math

sqrt 求平方根

pow 幂运算

floor 向下取整

static double floor(double d);

返回小于或者等于参数d的最大整数

ceil 向上取整

static double ceil(double d)

返回大于或者等于参数d的最小整数

abs 绝对值

static int abs(int i)

round 四舍五入取整

static double round(doubl d)

问: 如何保留n位小数?

random 随机数

static double random() 返回随机数 0.0-1.0之间

来源,也是Random类

Arrays 类

java.util.Arrays

快速排序法 sort

直接对参数排序,无需返回

二分查找法 binarySearch

元素不存在,返回的是 -(插入点-1) 也就是都是负数

toString 转字符串

Array.toString(arr)

这个不是重写,是自定义的方法

大整数

java.math.BigInteger

构造器, BigInteger(String str)

四则运算

加法运算 BigInteger add(BigInteger)

减法运算 BigInteger substract(BigInteger)

乘法运算 BigInteger multiply(BigInteger)

除法运算 BigInteger divide(BigInteger)

java.math.BigDecimal

浮点精度问题

System.out.println(0.09 + 0.01); //0.09999999999999999
System.out.println(1 - 0.68);

BigDecimal除了解决超大浮点数据外,还提供了高精度浮点运算

构造器: 用String运算

三则运算

BigDecimal b1 = new BigDecimal("1");
BigDecimal b2 = new BigDecimal("0.32");
System.out.println(b1.subtract(b2));

除法运算

会出现无限循环小数,抛出数学计算异常

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

解决办法: 保留小数

divide(BigDecimal, scale, mode)

scale: 保留几位
mode: 保留模式

保留模式:静态常量

  • ROUND_UP : 向上+1
  • ROUND_DOWN: 直接舍去
  • ROUND_HALF_UP: 四舍五入
  • ROUND_HALF_DOWN: > 0.5 向上+1
BigDecimal b1 = new BigDecimal("1");
BigDecimal b2 = new BigDecimal("7");
System.out.println(b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP));
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值