java基础专栏—CommonApi

Java常用Api

将基本类型也转换成了对象以对其功能扩展

javaWeb接受到的数据都是以String类型的,怎么把String转换为int

|字节 | 整型 | 字符 | 布尔|

| ---- | ------- | ------ | ------- |

| Byte | Integer | String | Boolean |

Integer应用举例

数字格式的字符串

  • *Integer.parseInt("12") NumberFormatException

  • 任何类型转换为String ,只需要连接空串" "

  • 使用Integer中的静态方法toString()

    • 只有引用类型才有toString(),基础类型是没有toString()

利用Integer的构造方法将String转换为int类型

Integer in = new Integer("100");
int in = in.valueOf();
  • Integer的静态成员变量
    • MAX_VALUE
    • MIN_VALUE
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE)
  • Integer的静态方法
    • 十进制转二进制 toBinarString(int value)
    • 十进制转八进制 toOctalString(int value)
    • 十进制转十六进制 toHexString(int value)
    • 转换完以后是String转换的

自动装箱,自动拆箱

  • 自动装箱:将基本数据类型直接转换为对象
  • 自动拆箱:将对象数据转换为基本数据类型
Integer i = 1;
//不用new Integer()
System.out.println(i)
//打印应用类型会调用toString(),说明我们重写过toString()
  
ArrayList<int> ar = new ArrayList<int>
//会报错,因为int是基本数据类型不是对象
ArrayList<Integer> ar = new ArrayList<Integer>
ar.add(1);
//1是基本数据,但是自动转换为对象了

Integer in = null;
in = in + 1;
//弊端,在为空的时候是不会自动装箱的
//在运算的时候就会NullPointException
  • 基本类型可以直接和引用类型做运算
  • 简化代码
Integer i = 127;
Integer j = 127;
System.out.println(i==j);//Ture
//数据在自动装箱的时候数据没有大于1字节,所以 Integer j = i;
System.out.println(i.equals(j));//Ture

System应用

私有修饰构造函数 调用方法只能使用类名调用

  • public static void currentTimeMillis()

    • 用来计算程序运行的时间
  • public static void exit(int status)

    • 终止正在运行的虚拟机,0正常终止,非0异常终止
  • public static void gc()

    • 垃圾回收,虚拟机回收垃圾的时候回调用finalize()
  • public static Properties getPropertise()

    • 返回当前操作系统的信息
  • public static void arraycopy(Object src ,int srcPos ,Object dest , int destPos, int length)

    • 复制一个数组

    • src:源数组

      srcPos:源数组的开始的索引

      dest:复制后目标数组

      destPos:目标数组起始索引

      length:复制几个

    • int[] src = {11,22,33,44,55,66};
      int[] dest = {77,88,99,0};
      
      System.arraycopy(src, 1, dest, 1, 2)
      

Math类应用举例

静态方法工具类

  • public static int abs(int i)
    • 返回绝对值
  • public static int max(int i, int j)
  • public static int min(int i,int j)
  • public static double ceil(double d)
    • 返回大于或者等于参数d的最小整数
  • public static double floor(double d)
    • 返回小于或者等于参数d的最大整数
  • public static double pow(double a, double b)
  • public static double sqrt(double a)
  • public static double random()
    • 返回一个随机数(伪随机数)0.0~1,0之间
      • 也是使用random类来实现的,推荐直接使用random
  • public static double round(double d)
    • 四舍五入取整

Array工具类

java.util 继承至java.lang.Object

  • public static void sort(array)

    Arrays.sort(数组)
    
  • public static int binarySearch(array, element)

    • 返回数组的二分查找,只能搜索有序的数组,元素存在返回- 插入点索引 - 1
  • public static String toString(array)

    • 将数组转换为String

BigInteger

超过lang的范围的就是BigInteger对象

  • public BigInteger(String str)
  • BigInteger的四则运算,都是调用方法来实现的不是通过四则运算符 来完成
    • public static BigInteger add()
    • public static BigInteger subtract()
    • public static BigInteger mutiply()
    • public static BigInteger divied()

BigDecimal

  • public BiDecimal(String string)
    • add, subtact, multiply
    • divide(BigDecimal dicisor, int scale ,int rindingMode)
      • 无限不循环小数不能显示,arithmeticException Non-terminating decimal exception
    • int scale保留几位小数

转载于:https://my.oschina.net/u/3483440/blog/1554141

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值