经常使用API - 包装类、System类

包装类

概述

Java提供了两个类型系统,基本类型与引用类型,使用基本类型在于效率。java

然而不少状况,会建立对象使用,由于对象能够作更多的功能。数组

若是想要咱们的基本类型像对象同样操做,就能够使用基本类型对应的包装类。以下:安全

基本类型 对应的包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

装箱与拆箱

基本类型与对应的包装类对象之间,来回转换的过程称为”装箱“与”拆箱“:ide

  • 装箱:基本类型 --> 对应的包装类对象。函数

  • 拆箱:包装类对象 --> 对应的基本类型。性能

用Integer与 int为例:(看懂代码便可)code

基本数值---->包装对象orm

Integer i = new Integer(4);//使用构造函数函数
Integer iii = Integer.valueOf(4);//使用包装类中的valueOf方法

包装对象---->基本数值对象

int num = i.intValue();

自动装箱与自动拆箱

因为咱们常常要作基本类型与包装类之间的转换,从Java 5(JDK 1.5)开始,继承

基本类型与包装类的装箱、拆箱动做能够自动完成。例如:

package com;

/**
 * 自动装箱和拆箱:基本类型的数据和包装类之间能够自动的互相转换
 *
 */
public class IntegerTest {
    public static void main(String[] args) {
        // 自动装箱,直接把int类型的整数赋值给包装类
        Integer i = 1; //至关于 Integer i = new Integer(1);


        // 自动拆箱:i是包装类,没法直接参与运算,能够自动转为基本类型再进行计算
        // i = i + 2 至关于 i = i.intValue() + 2
        // 计算出结果后又自动装箱
        i = i +2;
        System.out.println(i);
    }
}

基本类型与字符串之间的转换

基本类型转换为String

基本类型转换String总共有三种方式,查看课后资料能够得知,这里只讲最简单的一种方式:

  1. 直接拼接空串,遇到串以后的数据都当成字符串拼接,以前的数据正常运算。好比

    "" + 1 + 2 + 3  // 获得123
    1 + 2 + "" + 3  // 得 33
    1 + 2 + 3 + ""  // 得 6
  2. 调用String的valueOf方法

    int num = 123;
    String str = String.valueOf(num);
    System.out.println(str);
  3. 转换成封装类调用toString方法

    Integer num = 6;
    String str = num.toString();
    System.out.println(str);
基本类型直接与””相链接便可;如:34 + ""

String转换成对应的基本类型

除了Character类以外,其余全部包装类都具备parseXxx静态方法能够将字符串参数转换为对应的基本类型:

  • public static byte parseByte(String s):将字符串参数转换为对应的byte基本类型。
  • public static short parseShort(String s):将字符串参数转换为对应的short基本类型。
  • public static int parseInt(String s):将字符串参数转换为对应的int基本类型。
  • public static long parseLong(String s):将字符串参数转换为对应的long基本类型。
  • public static float parseFloat(String s):将字符串参数转换为对应的float基本类型。
  • public static double parseDouble(String s):将字符串参数转换为对应的double基本类型。
  • public static boolean parseBoolean(String s):将字符串参数转换为对应的boolean基本类型。

代码使用(仅以Integer类的静态方法parseXxx为例)如:

public class IntegerParse {
    public static void main(String[] args) {
        int num = Integer.parseInt("100");
    }
}

注意:若是字符串参数的内容没法正确转换为对应的基本类型,则会抛出java.lang.NumberFormatException异常。

System类

java.lang.System类中提供了大量的静态方法,能够获取与系统相关的信息或系统级操做。它不能被实例化。

System 类中,有标准输入、标准输出和错误输出流;

对外部定义的属性和环境变量的访问;加载文件和库的方法;

还有快速复制数组的一部分的实用方法。

字段摘要

字段摘要 描述
static PrintStream err “标准”错误输出流。
static InputStream in “标准”输入流。
static PrintStream out “标准”输出流。

方法摘要

方法 描述
static long currentTimeMillis() 返回以毫秒为单位的当前时间。
static void arraycopy(Object src, int srcPos,
Object dest, int destPos, int length)
从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
static String clearProperty(String key) 移除指定键指示的系统属性。
static Console console() 返回与当前 Java 虚拟机关联的惟一 Console 对象(若是有)。
static void exit(int status) 终止当前正在运行的 Java 虚拟机。
static void gc() 运行垃圾回收器。
static Map<String,String> getenv() 返回一个不能修改的当前系统环境的字符串映射视图。
static String getenv(String name) 获取指定的环境变量值。
static Properties getProperties() 肯定当前的系统属性。
static String getProperty(String key) 获取指定键指示的系统属性。
static String getProperty(String key, String def) 获取用指定键描述的系统属性。
static SecurityManager getSecurityManager() 获取系统安全接口。
static int identityHashCode(Object x) 返回给定对象的哈希码,该代码与默认的方法 hashCode() 返回的代码同样,不管给定对象的类是否重写 hashCode()。
static Channel inheritedChannel() 返回从建立此 Java 虚拟机的实体中继承的信道。
static void load(String filename) 从做为动态库的本地文件系统中以指定的文件名加载代码文件。
static void loadLibrary(String libname) 加载由 libname 参数指定的系统库。
static String mapLibraryName(String libname) 将一个库名称映射到特定于平台的、表示本机库的字符串中。
static long nanoTime() 返回最准确的可用系统计时器的当前值,以毫微秒为单位。
static void runFinalization() 运行处于挂起终止状态的全部对象的终止方法。
static void setErr(PrintStream err) 从新分配“标准”错误输出流。
static void setIn(InputStream in) 从新分配“标准”输入流。
static void setOut(PrintStream out) 从新分配“标准”输出流。
static void setProperties(Properties props) 将系统属性设置为 Properties 参数。
static String setProperty(String key, String value) 设置指定键指示的系统属性。
static void setSecurityManager(SecurityManager s) 设置系统安全性。

在System类的API文档中,经常使用的方法有:

  • public static long currentTimeMillis():返回以毫秒为单位的当前时间。

  • public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

    将数组中指定的数据拷贝到另外一个数组中。

currentTimeMillis方法

实际上,currentTimeMillis方法就是 获取当前系统时间与1970年01月01日00:00点之间的毫秒差值

package com;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SystemTest {
    public static void main(String[] args) {
        
        //获取当前时间毫秒值
        long ms = System.currentTimeMillis();
        System.out.println("当前时间戳:" + ms);
        Date date = new Date(ms);
        System.out.println("默认日期格式:" + date);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("指定日期格式:" + simpleDateFormat.format(date));
    }
}

练习

验证for循环打印数字1-9999所须要使用的时间(毫秒)

public class SystemTest1 {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            System.out.println(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("共耗时毫秒:" + (end - start));
    }
}

arraycopy方法

  • public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

    将数组中指定的数据拷贝到另外一个数组中。

数组的拷贝动做是系统级的,性能很高。System.arraycopy方法具备5个参数,含义分别为:

参数名称 参数类型 参数含义
src Object 源数组
srcPos int 源数组索引发始位置
dest Object 目标数组
destPos int 目标数组索引发始位置
length int 复制元素个数

练习

将src数组中前3个元素,复制到dest数组的前3个位置上

复制元素前:src数组元素[1,2,3,4,5],dest数组元素[6,7,8,9,10]

复制元素后:src数组元素[1,2,3,4,5],dest数组元素[1,2,3,9,10]

import java.util.Arrays;

/**
 * 将src数组中前3个元素,复制到dest数组的前3个位置上
 * 复制元素前:src数组元素[1,2,3,4,5],dest数组元素[6,7,8,9,10]
 * 复制元素后:src数组元素[1,2,3,4,5],dest数组元素[1,2,3,9,10]
 */
public class SystemPractise {
    public static void main(String[] args) {
        int[] src = {1, 2, 3, 4, 5};
        int[] dest = {5, 6, 7, 8, 9};

        System.out.println("复制以前:");
        System.out.println(Arrays.toString(src));
        System.out.println(Arrays.toString(dest));

        System.arraycopy(src,0,dest,0,3);
        System.out.println("复制以后:");
        System.out.println(Arrays.toString(src));
        System.out.println(Arrays.toString(dest));
    }
}
相关文章
相关标签/搜索