只要在运行时遇到错误讲解

上传人:我*** 文档编号:137055353 上传时间:2020-07-04 格式:PPT 页数:35 大小:380.50KB
返回 下载 相关 举报
只要在运行时遇到错误讲解_第1页
第1页 / 共35页
只要在运行时遇到错误讲解_第2页
第2页 / 共35页
只要在运行时遇到错误讲解_第3页
第3页 / 共35页
只要在运行时遇到错误讲解_第4页
第4页 / 共35页
只要在运行时遇到错误讲解_第5页
第5页 / 共35页
点击查看更多>>
资源描述

《只要在运行时遇到错误讲解》由会员分享,可在线阅读,更多相关《只要在运行时遇到错误讲解(35页珍藏版)》请在金锄头文库上搜索。

1、第六章,java.lang包,2,万策实训,回顾,只要在运行时遇到错误,就会发生异常Java异常是一个对象,用来描述一段代码中发生的异常情况发生异常情况时,将在导致错误的方法中创建和引发表示该异常的对象可以使用try、catch、throw、throws和finally来管理Java异常处理用于监视的程序语句包含在try块内。catch块内的代码用于捕获和处理异常必须在方法返回之前执行的任何代码应放置在finally块内要手动引发异常,可以使用关键字throw。任何异常可以通过throws子句从方法抛出,3,万策实训,目标,了解java.lang包掌握包装类掌握String和StringBuf

2、fer类运用以下类的方法:MathClassObject,4,万策实训,.intnum1=5;Integernum=newInteger(num1);intnum2=num.intValue();.,包装类6-1,intdigit=10;,原始数据类型,使用原始数据类型声明的变量,视为对象,原始数据类型,包装类,Java.lang提供,5,万策实训,包装类6-2,6,万策实训,包装类6-3,演示:示例1,包装类的用法使用包装类的方法,如ceil()、floor()和round(),publicclassNumberWrap/*构造方法*/protectedNumberWrap()/*这是mai

3、n方法*它将原始值转换为其相应的包装类型*paramargs传递至main方法的参数*/publicstaticvoidmain(Stringargs)Stringnumber=args0;BytebyNum=Byte.valueOf(number);ShortshNum=Short.valueOf(number);Integernum=Integer.valueOf(number);LonglgNum=Long.valueOf(number);System.out.println(Output);System.out.println(byNum);System.out.println(shN

4、um);System.out.println(num);System.out.println(lgNum);,7,万策实训,包装类6-4,Character包装类的方法,8,万策实训,包装类6-5,演示:示例2,使用包装类的方法,如Character类,publicclassTestCharacterpublicstaticvoidmain(Stringargs)intcount;charvalues=*,7,p,P;for(count=0;countvalues.length;count+)if(Character.isDigit(valuescount)System.out.println

5、(valuescount+“是一个数字);if(Character.isLetter(valuescount)System.out.println(valuescount+“是一个字母);if(Character.isUpperCase(valuescount)System.out.println(valuescount+“是大写形式);if(Character.isUnicodeIdentifierStart(valuescount)System.out.println(valuescount+“是Unicode+“标识符的第一个有效字符);,9,万策实训,String类,String类,字

6、符串字面量,对象,未修改的原始字符串,使用String类的方法,可以更改字符串版本,原始字符串保持不变,10,万策实训,String类的构造方法,11,万策实训,字符串长度2-1,字符串,长度,由length()方法确定,语法,publicintlength();,返回字符串中的字符数,12,万策实训,字符串长度2-2,字符串1,长度,Determinedbylength()method,Syntax,Publicintlength(),Returnsnumberofcharactersinthestring,Stringname=JohnSmith;System.out.println(na

7、me.length();,13,万策实训,字符串比较4-1,字符串1,字符串2,字符串1,字符串2,由equals()方法确定,检查组成字符串内容的字符,同一个对象,用=运算符检查,检查字符串是否指向同一个或不同的对象,14,万策实训,字符串比较4-2,演示:示例3,publicclassEquality/*构造方法*/protectedEquality()/*它演示两个字符串的比较*paramargs传递至main方法的参数*/publicstaticvoidmain(Stringargs)Stringstring1=newString(“苹果是一种水果);Stringstring2=new

8、String(“玫瑰花是一种花);Stringstring3=newString(“苹果是一种水果);System.out.println(“字符串1:+string1);System.out.println(“字符串2:+string2);System.out.println(“字符串3:+string3);,字符串比较运算符的用法使用String类的方法,如equals()和=运算符,if(string1=string2)System.out.println(“字符串1和字符串2相等);elseSystem.out.println(“字符串1和字符串2不等);if(string1.equa

9、ls(string3)System.out.println(“字符串1和字符串3相等);elseSystem.out.println(字符串1和字符串2不等);System.out.println(“设置字符串1等于字符串2);string2=string1;if(string1.equals(string2)System.out.println(“两个字符串相等);elseSystem.out.println(“两个字符串不等);,15,万策实训,字符串比较4-3,16,万策实训,字符串比较4-4,演示:示例4,比较不同的字符串使用String类的方法,如equalsIgnoreCase()

10、、compareTo()、startsWith()和endsWith(),publicclassStringdemo/*构造方法*/protectedStringdemo()/*这是main方法*它演示String类的比较方法*paramargs传递至main方法的参数*/publicstaticvoidmain(Stringargs)Stringstring1,string2,string3;string1=newString(Answer);string2=newString(ANSWER);string3=newString(Question);System.out.println(“字

11、符串A是+string1);System.out.println(“字符串B是+string2);System.out.println(“字符串C是+string3);,if(string1=string2)System.out.println(“字符串A和字符串B指同一个对象);elseSystem.out.println(“字符串A和字符串B指不同的对象);if(string1.equals(string2)System.out.println(“字符串A和字符串B的内容相同);elseSystem.out.println(“字符串A和字符串B的内容不同);if(string1.equal

12、sIgnoreCase(string2)System.out.println(“忽略大小写,字符串A和B的内容相同);elseif(string1.equalsIgnoreCase(string3)System.out.println(“字符串A和B的内容相同);if(pareTo(Answer)=0)System.out.println(“按字母,字符串A与Answer的内容相同);if(string1.startsWith(A)System.out.println(“以A开始);,17,万策实训,搜索字符串2-1,字符串1,情形1:,indexOf(character)方法,找到第一个匹配

13、,索引,0123,情形2:,如果没有找到匹配,则返回-1,返回找到的第一个匹配的位置索引,18,万策实训,搜索字符串2-2,示例:示例5,搜索字符串内有无指定的字符或字符串使用String类的方法,如indexOf(),publicclassSearchString/*构造方法*/protectedSearchString()/*这是main方法*它演示在字符串内搜索*paramargs传递至main方法的参数*/publicstaticvoidmain(Stringargs)Stringname=JohnS;System.out.println(“EmailID是:+name);System

14、.out.println(“的索引是:+name.indexOf();System.out.println(“.的索引是:+name.indexOf(.);if(name.indexOf(.)name.indexOf()System.out.println(“该电子邮件地址有效);elseSystem.out.println(“该电子邮件地址无效);,19,万策实训,提取字符串3-1,20,万策实训,提取字符串3-2,.charch;ch=orange.charAt(3);.,它将从index(3)中提取单个字符串“n”并将其存储在变量ch中,21,万策实训,提取字符串3-3,演示:示例6,如

15、何使用字符串提取或字符提取使用String类的方法,如substring()、concat()、replace()和trim(),publicclassStringMethods/*构造方法*/protectedStringMethods()/*这是main方法*paramargs传递至main方法的参数*/publicstaticvoidmain(Stringargs)Strings=Javaisa+platformindependentlanguage;Strings1=Helloworld;Strings2=Hello;Strings3=HELLO;System.out.println(

16、s);System.out.println(indexoft=+s.indexOf(t);System.out.println(lastindexoft=+s.lastIndexOf(t);System.out.println(indexof(t,10)=+s.indexOf(t,10);System.out.println(s1.substring(3,8);System.out.println(s2.concat(World);System.out.println(s2.replace(l,w);System.out.println(s1.trim();,22,万策实训,更改字符串中字符的大小写2-1,Hello,使用toUpperCase()方法,HELLO,HELLO,使用toLowerCase()方法,hello,语法

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号