|  | Object类 | java.lang.Object 类是Java语言中的根类,即所有类的父类。它中描述的所有方法子类都可以使用。 |  |  | 
 |  |  | 在对象实例 化的时候,最终找的父类就是Object。 |  |  |  |  |  |  | 
 |  |  | 如果一个类没有特别指定父类, 那么默认则继承自Object类。例如: |  |  |  |  |  | 
 |  |  | public class MyClass /*extends Object*/ {      // ... |  |  |  |  |  |  | 
 |  |  | } |  |  |  |  |  |  |  |  |  |  | 
 |  |  |  |  |  |  |  |  |  |  |  |  |  | 
 |  | Object类中方法①toString方法②equals方法 |  |  |  |  |  |  |  |  | 
 |  |  | ①toString |  | public String toString() |  |  |  |  |  |  | 
 |  |  | toString方法返回该对象的字符串表示,其实该字符串内容就是对象的类型+@+内存地址值。 |  |  | 
 |  |  | 由于toString方法返回的结果是内存地址,而在开发中,经常需要按照对象的属性得到相应的字符串表现形式,因此也需要重写它。 | 
 |  |  | 覆盖重写 |  |  |  |  |  |  |  |  |  |  | 
 |  |  | 如果不希望使用toString方法的默认行为,(一般开发中都是自己重写toString方法) |  |  |  |  | 
 |  |  | 在对象中重写toString()方法 |  |  |  |  |  |  |  |  | 
 |  |  | public String  toString() { |  |  |  |  |  |  |  |  | 
 |  |  |     return "学生信息:" + "姓名:"  + name + ",年龄:" + age; |  |  |  |  |  | 
 |  |  | } |  |  |  |  |  |  |  |  |  |  | 
 |  |  | ②equals |  | public boolean equals(Object obj) |  |  |  |  |  | 
 |  |  | 默认地址比较 |  |  |  |  |  |  |  |  |  |  | 
 |  |  | 如果没有覆盖重写equals方法,那么Object类中默认进行 ==  运算符的对象地址(地址值)比较,只要不是同一个对象,结果必然为false。 | 
 |  |  | 自定义重写比较 | 可以使用快捷键 alt+insert ,并选 择  equals() and hashCode() 进行自动代码生成 |  | 
 |  |  | @Override |  |  |  |  |  |  |  |  |  |  | 
 |  |  | public boolean equals(Object o) { |  |  |  |  |  |  |  | 
 |  |  |     if (this ==  o) return true; ////提高代码运行速率 |  |  |  |  |  | 
 |  |  |     if (o == null || getClass() != o.getClass()) return  false;//代码健壮性 |  |  |  | 
 |  |  |     Student student = (Student) o; |  |  |  |  |  |  |  | 
 |  |  |     return age == student.age && |  |  |  |  |  |  |  | 
 |  |  |             Objects.equals(name,  student.name); |  |  |  |  |  |  | 
 |  |  | } |  |  |  |  |  |  |  |  |  |  | 
 |  |  |  |  |  |  |  |  |  |  |  |  |  | 
 |  | 日期时间类 | java.util.Date 类  表示特定的瞬间,精确到毫秒。  |  |  |  |  |  |  |  |  |  |  | 
 |  |  | public Date() :分配Date对象并初始化此对象,以表示分配它的时间(精确到毫秒)。 |  |  |  | 
 |  |  | public Date(long date)  :分配Date对象并初始化此对象,以表示自从标准基准时间(称为“历元 (epoch)(时间原点)”, | 
 |  |  | public long getTime()  把日期对象转换成对应的时间毫秒值。 |  | long S  = new Date().getTime(); | 
 |  |  | 即1970年1月1日00:00:00 GMT)以来的指定毫秒数。 |  |  |  |  |  |  | 
 |  |  | tips: 由于我们处于东八区,所以我们的基准时间为1970年1月1日8时0分0秒 |  |  |  |  | 
 |  |  | import java.util.Date; |  |  |  |  |  |  |  |  | 
 |  |  | public class Demo01Date { |  |  |  |  |  |  |  |  | 
 |  |  |     public static void main(String[] args) { |  |  |  |  |  |  | 
 |  |  |         // 创建日期对象,把当前的时间 |  |  |  |  |  |  |  | 
 |  |  |         System.out.println(new Date()); // Tue Jan 16 14:37:35 CST 2018 |  |  |  | 
 |  |  |         // 创建日期对象,把当前的毫秒值转成日期对象 |  |  |  |  |  |  | 
 |  |  |         System.out.println(new Date(0L)); // Thu Jan 01 08:00:00 CST 1970 |  |  |  | 
 |  |  |         // 把当前的系统日期转成毫秒值s |  |  |  |  |  |  |  | 
 |  |  |         long s = new Date().getTime(); |  |  |  |  |  |  | 
 |  |  |         System.out.println(s);//1541852026818 |  |  |  |  |  |  | 
 |  |  |     } |  |  |  |  |  |  |  |  |  |  | 
 |  |  | } |  |  |  |  |  |  |  |  |  |  | 
 |  |  | tips:在使用println方法时,会自动调用Date类中的toString方法。Date类对Object类中的toString方法进行  |  | 
 |  |  | 了覆盖重写,所以结果为指定格式的字符串 |  |  |  |  |  |  |  | 
 |  |  |  |  |  |  |  |  |  |  |  |  |  | 
 |  |  | DateFormat类 |  |  |  |  |  |  |  |  |  |  | 
 |  |  | java.text.DateFormat   是日期/时间格式化子类的抽象类,我们通过这个类可以帮我们完成日期和文本之间的转  换,也就是可以在Date对象与String对象之间进行来回转换。 | 
 |  |  | 格式化:按照指定的格式,从Date对象转换为String对象。  | SimpleDateFormat里的format方法 |  |  | 
 |  |  | 解析:按照指定的格式,从String对象转换为Date对象。 | SimpleDateFormat里的parse方法 |  |  | 
 |  |  | 构造方法 |  |  |  |  |  |  |  |  |  |  | 
 |  |  | 由于DateFormat为抽象类,不能直接使用,所以需要常用的子类  java.text.SimpleDateFormat 。这个类需要一个 模式(格式)来指定格式化或解析的标准。 | 
 |  |  | 构造方法为:public SimpleDateFormat(String pattern)  :用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。 | 
 |  |  | 参数pattern是一个字符串,代表日期时间的自定义格式。 |  |  |  |  |  |  | 
 |  |  | 格式规则 | 年 | 月 | 日 | 时 | 分  | 秒 |  |  |  |  | 
 |  |  |  | y | M | d | H | m | s |  |  |  |  | 
 |  |  |     //将Date日期格式化   日期===>文本(SimpleDateFormate中format方法) |  |  |  | 
 |  |  |     //步骤1,创建SimpleDateFormate对象,并传入格式(例如yy-MM-dd  HH:mm:ss) |  |  |  | 
 |  |  |     //2,调用SimpleDateFormat中的format方法,将Date数据格式化为想要的格式的字符串 |  |  | 
 |  |  |     private static void one() { |  |  |  |  |  |  |  | 
 |  |  |         Date d1 = new Date(); |  |  |  |  |  |  |  | 
 |  |  |         System.out.println(d1); |  |  |  |  |  |  |  | 
 |  |  |         SimpleDateFormat sd1 = new SimpleDateFormat("yyyy年MM月dd  HH时mm分ss秒"); |  |  | 
 |  |  |         String str = sd1.format(d1); |  |  |  |  |  |  |  | 
 |  |  |         System.out.println(str); |  |  |  |  |  |  |  | 
 |  |  |     } |  |  |  |  |  |  |  |  |  |  | 
 |  |  |     //将String字符串转为Date日期类型(解析)  文本===>日期(SimpleDateFormate中parse方法) |  | 
 |  |  |     //步骤1,创建SimpleDateFormat对象,并传入文本格式 |  |  |  |  |  | 
 |  |  |     //2,调用SimpleDateFormat中的parse方法,将符合格式的字符串转化为Date数据格式 |  |  | 
 |  |  |     /*注意--- public Date parse(String  source) throws ParseException |  |  |  | 
 |  |  |              parse方法声明了一个异常叫ParseException |  |  |  |  |  | 
 |  |  |              如果字符串和构造方法的模式不一样,那么程序就会抛出异常 |  |  |  |  | 
 |  |  |              调用一个抛出了异常的方法,就必须的处理这个异常,要么throws继续抛出这个异常,要么try  catch自己处理 | 
 |  |  |     */ |  |  |  |  |  |  |  |  |  |  | 
 |  |  |     private static void two() throws ParseException { |  |  |  |  |  | 
 |  |  |         String str = "2018-1-1"; |  |  |  |  |  |  |  | 
 |  |  |         SimpleDateFormat sd2= new SimpleDateFormat("yyyy-MM-dd"); |  |  |  |  | 
 |  |  |         Date d1 = sd2.parse(str); |     //将记得要抛ParseException异常 |  |  |  | 
 |  |  |         System.out.println(d1); |  |  |  |  |  |  |  | 
 |  |  |     } |  |  |  |  |  |  |  |  |  |  | 
 |  |  | } |  |  |  |  |  |  |  |  |  |  | 
 |  |  |  |  |  |  |  |  |  |  |  |  |  | 
 |  |  | Calendar类 | java.util.Calendar  是日历类,在Date后出现,替换掉了许多Date的方法。该类将所有可能用到的时间信息封装 为静态成员变量,方便获取。 | 
 |  |  |  | 日历类就是方便获取各个时间属性的。 |  |  |  |  |  |  | 
 |  |  | 获取方式 |  |  |  |  |  |  |  |  |  |  | 
 |  |  | Calendar为抽象类,由于语言敏感性,Calendar类在创建对象时并非直接创建,而是通过静态方法创建,返回子 类对象,如下: | 
 |  |  | Calendar静态方法 |     Calendar cal =  Calendar.getInstance(); |  |  |  |  | 
 |  |  | public static Calendar getInstance()  :使用默认时区和语言环境获得一个日历 |  |  |  |  | 
 |  |  | 例如: |  |  |  |  |  |  |  |  |  |  | 
 |  |  | 常用方法 |  |  |  |  |  |  |  |  |  |  | 
 |  |  | 根据Calendar类的API文档,常用方法有: |  |  |  |  |  |  |  | 
 |  |  | public int get(int field)  :返回给定日历字段的值。 |  |  |  |  |  |  | 
 |  |  | public void set(int field, int value)  :将给定的日历字段设置为给定值。 |  |  |  |  | 
 |  |  | public abstract void add(int field, int amount)  :根据日历的规则,为给定的日历字段添加或减去指 |  |  | 
 |  |  | 定的时间量。 |  |  |  |  |  |  |  |  |  |  | 
 |  |  | public Date getTime()  :返回一个表示此Calendar时间值(从历元到现在的毫秒偏移量)的Date对象。 |  |  | 
 |  |  | Calendar类中提供很多成员常量,代表给定的日历字段: |  |  |  |  |  |  | 
 |  |  | 年 | 月(从0开始,得+1使用) | 月中的天(几号) | 时(12小时) | 时(24小时制) | 分 | 秒 | 周中的天(周几,周日为1可以减1使用) | 
 |  |  | YEAR | MONTH |  | DAY_OF_MONTH | HOUR | HOUR_OF_DAY | MINUTE | SECOND | DAY_OF_WEEK | 
 |  |  | get和set方法 |  |  |  |  |  |  |  |  |  |  | 
 |  |  | public static void main(String[] args) { |  |  |  |  |  |  | 
 |  |  |     // 创建Calendar对象 |  |  |  |  |  |  |  |  | 
 |  |  |     Calendar cal =  Calendar.getInstance(); |  |  |  |  |  |  | 
 |  |  |     // 获得月 |  |  |  |  |  |  |  |  |  | 
 |  |  |     int month = cal.get(Calendar.MONTH) + 1; |  |  |  |  |  |  | 
 |  |  |     // 获得日 |  |  |  |  |  |  |  |  |  | 
 |  |  |     int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); |  |  |  |  |  | 
 |  |  |     // 设置年 |  |  |  |  |  |  |  |  |  | 
 |  |  |     cal.set(Calendar.YEAR, 2020); |  |  |  |  |  |  |  | 
 |  |  |     // 获得年 |  |  |  |  |  |  |  |  |  | 
 |  |  |     int year = cal.get(Calendar.YEAR); |  |  |  |  |  |  | 
 |  |  |     System.out.print(year + "年" +  month + "月" + dayOfMonth + "日"); |  |  |  | 
 |  |  | } |  |  |  |  |  |  |  |  |  |  | 
 |  |  | add方法 | add方法可以对指定日历字段的值进行加减操作,如果第二个参数为正数则加上偏移量,如果为负数则减去偏移量 | 
 |  |  | public static void main(String[] args) { |  |  |  |  |  |  | 
 |  |  |     // 创建Calendar对象 |  |  |  |  |  |  |  |  | 
 |  |  |     Calendar cal =  Calendar.getInstance(); |  |  |  |  |  |  | 
 |  |  |     // 获得月 |  |  |  |  |  |  |  |  |  | 
 |  |  |     int month = cal.get(Calendar.MONTH) + 1; |  |  |  |  |  |  | 
 |  |  |     // 获得日 |  |  |  |  |  |  |  |  |  | 
 |  |  |     int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); |  |  |  |  |  | 
 |  |  |     // 获得年 |  |  |  |  |  |  |  |  |  | 
 |  |  |     int year = cal.get(Calendar.YEAR); |  |  |  |  |  |  | 
 |  |  |     System.out.println(year + "年" +  month + "月" + dayOfMonth + "日"); |  |  |  | 
 |  |  |     //2018年11月11日 |  |  |  |  |  |  |  |  | 
 |  |  |     // 使用add方法 |  |  |  |  |  |  |  |  |  | 
 |  |  |     cal.add(Calendar.DAY_OF_MONTH, 2); // 加2天 |  |  |  |  |  |  | 
 |  |  |     cal.add(Calendar.YEAR, -3); // 减3年 |  |  |  |  |  |  | 
 |  |  |     // 获得月 |  |  |  |  |  |  |  |  |  | 
 |  |  |     int month1 = cal.get(Calendar.MONTH) + 1; |  |  |  |  |  |  | 
 |  |  |     // 获得日 |  |  |  |  |  |  |  |  |  | 
 |  |  |     int dayOfMonth1 = cal.get(Calendar.DAY_OF_MONTH); |  |  |  |  |  | 
 |  |  |     // 获得年 |  |  |  |  |  |  |  |  |  | 
 |  |  |     int year1 = cal.get(Calendar.YEAR); |  |  |  |  |  |  | 
 |  |  |     System.out.println(year1 + "年" +  month1 + "月" + dayOfMonth1 + "日"); |  |  |  | 
 |  |  |     // 2015年11月13日 |  |  |  |  |  |  |  |  | 
 |  |  | } |  |  |  |  |  |  |  |  |  |  | 
 |  |  | getTime方法 | Calendar中的getTime方法并不是获取毫秒时刻,而是拿到对应的Date对象。 | 西方星期的开始为周日,中国为周一。  | 
 |  |  | public static void main(String[] args) { |  |  |  |  |  在Calendar类中,月份的表示是以0-11代表1-12月。  | 
 |  |  |     Calendar cal = Calendar.getInstance(); |  |  |  |  |  日期是有大小关系的,时间靠后,时间越大。 | 
 |  |  |     Date date = cal.getTime(); |  |  |  |  |  |  |  | 
 |  |  |     System.out.println(date); |  |  |  |  |  |  |  | 
 |  |  |     //Sun Nov 11  09:02:50 CST 2018 |  |  |  |  |  |  |  | 
 |  |  | } |  |  |  |  |  |  |  |  |  |  | 
 |  |  |  |  |  |  |  |  |  |  |  |  |  | 
 |  |  |  |  |  |  |  |  |  |  |  |  |  | 
 |  | System类 | java.lang.System 类中提供了大量的静态(直接方法名点调用)方法,可以获取与系统相关的信息或系统级操作,在System类的API文  档中,常用的方法有: |  |  |  |  |  |  |  |  |  |  | 
 |  |  | public static long currentTimeMillis() :返回以毫秒为单位的当前时间。 | 可用来检测代码运行的时间 |  |  | 
 |  |  | public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)  :将 |  |  |  | 
 |  |  | 数组中指定的数据拷贝到另一个数组中。 |  |  |  |  |  |  |  | 
 |  |  | currentTimeMillis(返回值是long类型) |  |  |  |  |  |  |  | 
 |  |  | /** |  |  |  | public static void main(String[] args) { |  |  | 
 |  |  |  * 输出100天后星期几 |  |  |     long fistTime = System.currentTimeMillis(); |  | 
 |  |  |  * 月份默认从0开始,如果想拿正常的月份需要+1 |     System.out.println("第一次"+fistTime); |  |  | 
 |  |  |  * 星期值默认从星期天(1)开始算第一天,如果是正常国内显示需要-1 |     //第一次1542013360291 |  |  |  |  | 
 |  |  |  */ |  |  |  |     Calendar ca =  Calendar.getInstance(); |  |  | 
 |  |  |  |  |  |  |     ca.add(Calendar.DATE, 1); |  |  |  | 
 |  |  |  |  |  |  |     int dayOfWeek=ca.get(Calendar.DAY_OF_WEEK); |  | 
 |  |  |  |  |  |  |     System.out.println("100天后星期" + dayOfWeek); |  | 
 |  |  |  |  |  |  |     //100天后星期3 |  |  |  |  |  | 
 |  |  |  |  |  |  |     System.out.println("第二次"+System.currentTimeMillis()); | 
 |  |  |  |  |  |  |     //第二次1542013360518 |  |  |  |  | 
 |  |  |  |  |  |  | } |  |  |  |  |  |  | 
 |  |  | arraycopy方法(复制操作的是数组) |  |  |  |  |  |  |  | 
 |  |  | public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)  :将 数组中指定的数据拷贝到另一个数组中。 | 
 |  |  | 参数序号 | 参数名称 | 参数类型 | 参数含义 |  | public static void main(String[] args) { |  | 
 |  |  | 1  | src | Object | 源数组 |  |     int arr1[] = {1, 2, 3, 4, 5}; |  |  | 
 |  |  | 2  | srcPos | int | 源数组索引起始位置 |     int arr2[] = new int[5]; |  |  | 
 |  |  | 3  | dest | Object | 目标数组 |  |     System.arraycopy(arr1, 1,  arr2, 1, 3); |  | 
 |  |  | 4  | destPos | int | 目标数组索引起始位置  |     System.out.println(Arrays.toString(arr2)); | 
 |  |  | 5  | length | int | 复制元素个数 | }//[0,  2, 3, 4, 0] |  |  |  | 
 |  |  |  |  |  |  |  |  |  |  |  |  |  | 
 |  |  |  |  |  |  |  |  |  |  |  |  |  | 
 |  | StringBuilder类 |   | 由于String类的对象内容不可改变,所以每当进行字符串拼接时,总是会在内存中创建一个新的对象。例如: | 
 |  |  |  | 在API中对String类有这样的描述:字符串是常量,它们的值在创建后不能被更改。 |    |   | file:///C:/Users/ADMINI~1/AppData/Local/Temp/msohtmlclip1/01/clip_image002.png |  |   |      |  | 
 |  |  |  | 根据这句话分析我们的代码,其实总共产生了三个字符串,即  "Hello" 、 "World" 和 "HelloWorld" 。引用变量s 首先指向  Hello 对象, |  | 
 |  |  |  | 最终指向拼接出来的新字符串对象,即 HelloWord  。 |  | 
 |  |  |  | public static void main(String[] args) { |  | 
 |  |  |  |     String s = "Hello"; |  |  |  | 
 |  |  |  |     s += "World"; |  |  |  | 
 |  |  |  |     System.out.println(s); |  
 |  |  |  | 
 |  |  |  | } |  |  |  |  |  | 
 |  |  |  |  |  |  |  |  |  | 
 |  |  |  |  |  |  |  |  |  |  |  |  |  | 
 |  |  |  | 由此可知,如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象, |  |  | 
 |  |  |  | 既耗时,又浪费空间。为了解 决这一问题,可以使用  java.lang.StringBuilder 类。 |  |  | 
 |  |  |  |  |  |  |  |  |  |  |  |  |  | 
 |  |  | StringBuilder |  | 又称为可变字符序列,它是一个类似于 String 的字符串缓冲区,(字符串的容器) |  | 
 |  |  |  |  | 通过某些方法调用可以改变该序列的长度和内容。 (默认16字符空间,超过自动扩充) |  | 
 |  |  | 构造方法 |  |  |  |  |  |  |  |  |  |  | 
 |  |  | public StringBuilder()  :构造一个空的StringBuilder容器。 |  |  |  |  |  |  | 
 |  |  | public StringBuilder(String str) :构造一个StringBuilder容器,并将字符串添加进去。 |  |  |  | 
 |  |  |  | public static void main(String[] args) { |  |  |  |  |  | 
 |  |  |  |     StringBuilder sb1 = new  StringBuilder(); |  |  |  |  |  | 
 |  |  |  |     System.out.println(sb1); // (空白) |  |  |  |  |  | 
 |  |  |  |     // 使用带参构造 |  |  |  |  |  |  |  | 
 |  |  |  |     StringBuilder sb2 = new StringBuilder("test1"); |  |  |  |  | 
 |  |  |  |     System.out.println(sb2); // test1 |  |  |  |  |  | 
 |  |  |  | } |  |  |  |  |  |  |  |  |  | 
 |  |  | 常用方法 |  |  |  |  |  |  |  |  |  |  | 
 |  |  | public StringBuilder append(...)  :添加任意类型数据的字符串形式,并返回当前对象自身。 |  |  |  | 
 |  |  | public String toString() :将当前StringBuilder对象转换为String对象。 |  |  |  |  | 
 |  |  | append(...) 方法 | 备注:StringBuilder已经覆盖重写了Object当中的toString方法。 |  |  |  | 
 |  |  |  | public static void main(String[] args) { |  |  |  |  |  | 
 |  |  |  |     //创建对象 |  |  |  |  |  |  |  |  | 
 |  |  |  |     StringBuilder builder  = new StringBuilder(); |  |  |  |  | 
 |  |  |  |     //public  StringBuilder append(任意类型) |  |  |  |  |  | 
 |  |  |  |     StringBuilder  builder2 = builder.append("hello"); |  |  |  |  | 
 |  |  |  |     //对比一下 |  |  |  |  |  |  |  |  | 
 |  |  |  |     System.out.println("builder:" + builder);//builder:hello |  |  |  | 
 |  |  |  |     System.out.println("builder2:" + builder2);//builder2:hello |  |  | 
 |  |  |  |     System.out.println(builder == builder2); //true |  |  |  |  | 
 |  |  |  |     // 可以添加 任何类型 |  |  |  |  |  |  |  | 
 |  |  |  |     builder.append("hello"); |  |  |  |  |  |  | 
 |  |  |  |     builder.append("world"); |  |  |  |  |  |  | 
 |  |  |  |     builder.append(true); |  |  |  |  |  |  |  | 
 |  |  |  |     builder.append(100); |  |  |  |  |  |  |  | 
 |  |  |  |     // 在我们开发中,会遇到调用一个方法后,返回一个对象的情况。然后使用返回的对象继续调用方法。          | 
 |  |  |  |     // 这种时候,我们就可以把代码现在一起,如append方法一样,代码如下 |  |  |  | 
 |  |  |  |     //链式编程 |  |  |  |  |  |  |  |  | 
 |  |  |  |     builder.append("hello").append("world").append(true).append(100); |  |  | 
 |  |  |  |     System.out.println("builder:" + builder); |  |  |  |  | 
 |  |  |  |     //builder:hellohelloworldtrue100helloworldtrue100 |  |  |  |  | 
 |  |  |  | } |  |  |  |  |  |  |  |  |  | 
 |  |  |  |  |  |  |  |  |  |  |  |  |  | 
 |  |  | toString方法 | 通过toString方法,StringBuilder对象将会转换为不可变的String对象。 |  |  |  | 
 |  |  |  |  public static void main(String[] args) { |  |  |  |  |  | 
 |  |  |  |     // 链式创建 |  |  |  |  |  |  |  |  | 
 |  |  |  |     StringBuilder sb = new StringBuilder("Hello").append("World").append("Java"); |  | 
 |  |  |  |     // 调用方法 |  |  |  |  |  |  |  |  | 
 |  |  |  |     String str =  sb.toString(); |  |  |  |  |  |  | 
 |  |  |  |     System.out.println(str); // HelloWorldJava |  |  |  |  | 
 |  |  |  | } |  |  |  |  |  |  |  |  |  | 
 |  | 包装类 |   |  |  |  |  |  |  |  |  |  |  | 
 |  |  | Java提供了两个类型系统,基本类型与引用类型,使用基本类型在于效率, |  |  |  |  | 
 |  |  | 然而很多情况,会创建对象使用,因为 对象可以做更多的功能 |  |  |  |  |  | 
 |  |  | 基本数据类型 | byte | short | int | long  | double  | float | char | boolean |  |  | 
 |  |  |  | Byte | Short | Integer | Long  | Double | Float | Character | Boolean |  |  | 
 |  |  | 包装类作用:①提供一些有用字段 | Long.MAX_VALUE | ②还有一些可进行类型转换的方法 |  |  | 
 |  |  | 装箱与拆箱 | 基本类型与对应的包装类对象之间,来回转换的过程称为”装箱“与”拆箱“ |  |  |  | 
 |  |  |  | 装箱:从基本类型转换为对应的包装类对象。 |  |  |  |  |  | 
 |  |  |  | 拆箱:从包装类对象转换为对应的基本类型。 |  |  |  |  |  | 
 |  |  | 自动装箱与自动拆箱 | 由于我们经常要做基本类型与包装类之间的转换,从Java 5(JDK  1.5)开始, |  | 
 |  |  |  |  | 基本类型与包装类的装箱、拆箱动作 可以自动完成。 |  |  |  |  | 
 |  |  | public static void main(String[] args) { |  |  |  |  |  |  | 
 |  |  |      Integer i = 4;//自动装箱。相当于Integer i = Integer.valueOf(4); |  |  |  | 
 |  |  |      i = i + 5;//等号右边:将i对象转成基本数值(自动拆箱)  i.intValue() + 5;  |  |  |  | 
 |  |  |      // 加法运算完成后,再次装箱,把基本数值转成对象。 |  |  |  |  |  | 
 |  |  | } |  |  |  |  |  |  |  |  |  |  | 
 |  |  |  |  |  |  |  |  |  |  |  |  |  | 
 |  |  | 基本类型与字符串之间的转换 |  |  |  |  |  |  |  |  | 
 |  |  | 基本类型转换为String | 基本类型直接与””相连接即可;如: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基本类型。 |  | 
 |  |  |  public static void main(String[] args) { | 注意:如果字符串参数的内容无法正确转换为对应的基本类型 | 
 |  |  |  int num = Integer.parseInt("100"); |  | ,则会抛出  java.lang.NumberFormatException 异常。 |  | 
 |  |  | }//字符串100转为int数据类型Integer |  |  |  |  |  |  |  |