《JavaJDK6学习笔记-ppt简体版第04章.ppt》由会员分享,可在线阅读,更多相关《JavaJDK6学习笔记-ppt简体版第04章.ppt(14页珍藏版)》请在三一办公上搜索。
1、第4章,从autoboxing、unboxing认识对象 关于对象自动装箱、拆箱,使用对象,想写一个程序取得现在的系统时间,您只要产生一个工具就可以了Date实际上如何向系统取得时间,则无需您来操心,Date date=new Date();System.out.println(date.toString();,Tue May 03 16:06:46 GMT+08:00 2005,使用对象,字符串就是对象,是类别的一个实例,String text=Have a nice day!:);System.out.println(原文:+text);/传回全为大写的字符串内容 System.out.p
2、rintln(大写:+text.toUpperCase();/转回全为小写的字符串内容 System.out.println(小写:+text.toLowerCase();/计算字符串长度 System.out.println(长度:+text.length();/传回取代文字后的字符串 System.out.println(取代:+text.replaceAll(nice,good);/传回指定位置后的子字符串 System.out.println(子字符串:+text.substring(5);,使用对象,简单的用户登入程序,System.out.print(使用者名称:);String
3、username=scanner.next();System.out.print(用户密码:);String password=scanner.next();if(caterpillar.equals(username),包裹(Wrap)基本型态,Long、Integer、Double、Float、Boolean等类别是所谓的Wrapper类别主要目的,就是让您提供一个对象实例作为壳,将基本型态包到这个对象之中如此您就可以操作这个对象,就好像您将基本型态当作对象一样操作,包裹(Wrap)基本型态,int data1=10;int data2=20;/使用Integer来包里int资料 Inte
4、ger data1Wrapper=new Integer(data1);Integer data2Wrapper=new Integer(data2);/直接除以3 System.out.println(data1/3);/转为double值再除以3 System.out.println(data1Wrapper.doubleValue()/3);/进行两个值的比较 System.out.println(data1WpareTo(data2Wrapper);,包裹(Wrap)基本型态,自动装箱、拆箱,在J2SE5.0之前,要如下才能将int包装为一个Integer物件在J2SE5.0之后提供了
5、自动装箱的功能,Integer integer=new Integer(10);,Integer integer=10;,自动装箱、拆箱,Integer data1=10;Integer data2=20;/转为double值再除以3 System.out.println(data1.doubleValue()/3);/进行两个值的比较 System.out.println(pareTo(data2);,自动装箱、拆箱,自动装箱运用的方法还可以如下:更一般化的类别自动装箱,int i=10;Integer integer=i;,Number number=3.14f;,自动装箱、拆箱,自动拆箱
6、(unboxing)在运算时,也可以进行自动装箱与拆箱,Integer fooInteger=10;int fooPrimitive=fooInteger;,Integer i=10;System.out.println(i+10);System.out.println(i+);,Boolean boo=true;System.out.println(boo,小心使用boxing,自动装箱与拆箱的功能是编译程序来帮忙自动装箱与拆箱的功能是所谓的编译程序蜜糖(Compilersugar),Integer i=100;,Integer i=new Integer(100);,Integer i=n
7、ull;int j=i;,Integer i=null;int j=i.intValue();,NullPointerException,小心使用boxing,Integer i1=100;Integer i2=100;if(i1=i2)System.out.println(i1=i2);else System.out.println(i1!=i2);,Integer i1=200;Integer i2=200;if(i1=i2)System.out.println(i1=i2);else System.out.println(i1!=i2);,显示i1=i2,显示i1!=i2,小心使用boxing,=也用于判断两个对象参考名称是否参考至同一个对象在自动装箱时对于值从-128到127之间的值,它们被装箱为Integer对象后,会存在内存之中被重用,Integer i1=200;Integer i2=200;if(i1.equals(i2)System.out.println(i1=i2);else System.out.println(i1!=i2);,
链接地址:https://www.31ppt.com/p-6509342.html