




免费预览已结束,剩余30页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第六章,java.lang包,回顾,只要在运行时遇到错误,就会发生异常 java 异常是一个对象,用来描述一段代码中发生的异常情况 发生异常情况时,将在导致错误的方法中创建和引发表示该异常的对象 可以使用 try、catch、throw、throws 和 finally 来管理 java 异常处理 用于监视的程序语句包含在 try 块内。catch 块内的代码用于捕获和处理异常 必须在方法返回之前执行的任何代码应放置在 finally 块内 要手动引发异常,可以使用关键字 throw。任何异常可以通过 throws 子句从方法抛出,目标,了解 java.lang 包 掌握包装类 掌握string 和 stringbuffer 类 运用以下类的方法: math class object,. int num1 = 5; integer num = new integer(num1); int num2 = value(); .,包装类 6-1,int digit = 10;,原始数据类型,使用原始数据类型 声明的变量,视为对象,原始数据类型,包装类,java.lang 提供,包装类 6-2,包装类 6-3,演示:示例 1,包装类的用法 使用包装类的方法,如 ceil()、floor() 和 round(),public class numberwrap /* 构造方法 */ protected numberwrap() /* 这是 main 方法 * 它将原始值转换为其相应的包装类型 * param args 传递至 main 方法的参数 */ public static void main(string args) string number = args0; byte bynum = byte.valueof(number); short shnum = short.valueof(number); integer num = integer.valueof(number); long lgnum = long.valueof(number); system.out.println(“output“); system.out.println(bynum); system.out.println(shnum); system.out.println(num); system.out.println(lgnum); ,包装类 6-4,character包装类的方法,包装类 6-5,演示:示例 2,使用包装类的方法,如 character 类,public class testcharacter public static void main(string args) int count; char values = *, 7, p, , p; for (count = 0; count values.length; count+) if (character.isdigit(valuescount) system.out.println(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 “ + “标识符的第一个有效字符“); ,string 类,string 类,字符串字面量,对象,未修改的原始字符串,使用 string 类的方法,可以更改字符串版本,原始字符串保持不变,string 类的构造方法,字符串长度 2-1,字符串,长度,由 length() 方法确定,语法,public int length();,返回字符串中的字符数,字符串长度 2-2,字符串 1,长度,determined by length() method,syntax,public int length(),returns number of characters in the string,string name = “john smith“; system.out.println (name.length();,字符串比较 4-1,字符串 1,字符串 2,字符串 1,字符串 2,由 equals() 方法确定,检查组成字符串内容的字符,同一个对象,用 = 运算符检查,检查字符串是否指向同一个或不同的对象,字符串比较 4-2,演示:示例 3,public class equality /* 构造方法 */ protected equality() /*它演示两个字符串的比较 * param args 传递至 main 方法的参数 */ public static void main(string args) string string1 = new string(“苹果是一种水果“); string string2 = new string(“玫瑰花是一种花“); string string3 = new string(“苹果是一种水果“); 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 相等“); else system.out.println(“字符串 1 和字符串 2 不等“); if (string1.equals(string3) system.out.println(“字符串 1 和字符串 3 相等“); else system.out.println(“字符串 1 和字符串 2 不等“); system.out.println(“设置字符串 1 等于字符串 2“); string2 = string1; if (string1.equals(string2) system.out.println(“两个字符串相等“); else system.out.println(“两个字符串不等“); ,字符串比较 4-3,字符串比较 4-4,演示:示例 4,比较不同的字符串 使用 string 类的方法,如 equalsignorecase()、compareto()、startswith() 和 endswith(),public class stringdemo /* 构造方法 */ protected stringdemo() /* 这是 main 方法 * 它演示 string 类的比较方法 * param args 传递至 main 方法的参数 */ public static void main(string args) string string1, string2, string3; string1 = new string(“answer“); string2 = new string(“answer“); string3 = new string(“question“); system.out.println(“字符串 a 是 “ + string1); system.out.println(“字符串 b 是 “ + string2); system.out.println(“字符串 c 是 “ + string3);,if (string1 = string2) system.out.println(“字符串 a 和字符串 b 指同一个对象“); else system.out.println(“字符串 a 和字符串 b 指不同的对象“); if (string1.equals(string2) system.out.println(“字符串 a 和字符串 b 的内容相同“); else system.out.println(“字符串 a 和字符串 b 的内容不同“); if (string1.equalsignorecase(string2) system.out.println(“忽略大小写,字符串 a 和 b 的内容相同“); else if (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 开始“);,搜索字符串 2-1,字符串 1,情形 1:,indexof(character) 方法,找到第一个匹配,索引,0 1 2 3,情形 2:,如果没有找到匹配,则返回 -1,返回找到的第一个匹配的位置索引,搜索字符串 2-2,示例:示例 5,搜索字符串内有无指定的字符或字符串 使用 string 类的方法,如 indexof(),public class searchstring /* 构造方法 */ protected searchstring() /* 这是 main 方法 * 它演示在字符串内搜索 * param args 传递至 main 方法的参数 */ public static void main(string args) string name = “johns“; system.out.println(“email id 是: “ + name); system.out.println(“ 的索引是:“ + name.indexof(); system.out.println(“. 的索引是:“ + name.indexof(.); if (name.indexof(.) name.indexof() system.out.println(“该电子邮件地址有效“); else system.out.println(“该电子邮件地址无效“); ,提取字符串 3-1,提取字符串 3-2,. char ch; ch = “orange“.charat(3); .,它将从 index(3) 中提取单个字符串 “n”并将其存储在变量 ch 中,提取字符串 3-3,演示:示例 6,如何使用字符串提取或字符提取 使用 string 类的方法,如 substring()、concat()、replace() 和 trim(),public class stringmethods /* 构造方法 */ protected stringmethods() /* 这是 main 方法 * param args 传递至 main 方法的参数 */ public static void main(string args) string s = “java is a “ + “platform independent language“; string s1 = “hello world“; string s2 = “hello“; string s3 = “hello“; system.out.println(s); system.out.println(“index of t = “ + s.indexof(t); system.out.println(“last index of t = “ +s.lastindexof(t); system.out.println(“index of(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(); ,更改字符串中字符的大小写 2-1,hello,使用 touppercase( ) 方法,hello,hello,使用 tolowercase( ) 方法,hello,语法,public string touppercase();,语法,public string tolowercase();,更改字符串中字符的大小写 2-2,演示:示例 7,更改字符串中字符的大小写形式 使用 string 类的方法,如 touppercase() 和 tolowercase(),public class stringtest /* 构造方法 */ protected stringtest() /* 这是 main 方法 * 它演示字符串的 length() 和 uppercase() 方法 * param args 传递至 main 方法 */ public static void main(string args) string name = new string(“george“); system.out.println(“姓名是“ + name); int length = name.length(); system.out.println(“姓名的长度为 ” + length + “ 个字符“); system.out.println(“姓名用大写形式表示为: “); string nameuppercase = name.touppercase(); system.out.println(nameuppercase); ,stringbuffer 类 2-1,stringbuffer 用于表示可以修改的字符串 使用连接运算符 (+) 的字符串会自动创建字符串缓冲对象,stringbuffer 类 2-2,不变性 2-1,string类,创建后,直接修改,不变性的概念,解决方法,stringbuffer 类,string 的对等类,表示可增加和可编 写字符的可变序列,将字符插入到字符串中间 或附加到字符串末尾,不变性 2-2,演示:示例 8,stringbuffer 类的用法 使用 stringbuffer 类的方法,如 append()、insert()、replace()、setcharat() 和 tostring(),public class stringbuf /* 构造方法 */ protected stringbuf() public static void main(string args) stringbuffer buf = new stringbuffer(“java“); buf.append(“ guide ver1/”); buf.append(3); int index = 5; buf.insert(index, “student “); index = 23; buf.setcharat(index, .); int start = 24; int end = 25; buf.replace(start, end, “4“); string s = buf.tostring(); /转换为字符串 system.out.println(s); ,math 类 3-1,math 类,数字运算的方法,几何函数的方法,静态方法,子 类,math 类 3-2,math 类 3-3,演示:示例 9,math 类的用法 使用 math 类的方法,如 ceil()、floor() 和 round(),public class mathdemo /* 构造方法 */ protected mathdemo() /* main 方法演示 math 类的不同方法 * param args 传递至 main 方法的参数 */ public static void main(string args) /* 此变量存储 num 的值*/ int num = 38; /* 该变量存储 num1 的值*/ float num1 = 65.7f; system.out.println(math.ceil(num); system.out.println(math.ceil(num1); system.out.println(math.floor(num); system.out.println(math.floor(num1); system.out.println(math.round(num); system.out.println(math.round(num1); ,class 类 2-1,使用对象中的 getclass( ) 方法,使用静态 forname( ) 方法,使用自定义 classloader 对象加载新类,创建的对象,或,或,无需声明,自动创建对象,通过,class 类 2-2,演示:示例 10,class 类的用法 使用 class 类的方法,如 getclass() 和 getsuperclass(),class storestring /*构造方法. */ protected storestring() private string name = “diana“; /* 这个类扩展 storestring 类.*/ class storeinteger extends storestring /* 构造方法.*/ protected storeinteger() /* 该变量存储整数值. */ private int deptno; ,public class classdemo /*构造方法 */ protected classdemo() /* 这个类演示 class 类的访问方法 * param args 传递至 main 方法的参数 */ public static void main(string args) storestring objstring = new storestring(); storeinteger objinteger = new storeinteg
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024-2025学年河南省郑州市八十八中八年级(下)期中数学试卷(含答案)
- 养殖小区出租合同范本
- 房东日常收租合同范本
- 公共平台转让合同范本
- 夫妻买房的合同范本
- 空房公寓出租合同范本
- 自家车队维修合同范本
- 车位分期还款合同范本
- 定制制服服装合同范本
- 农业种植西红柿合同范本
- 德育副校长在班主任会议上讲话:7步走轻松打造和谐班级
- 外研版高一到高三单词表
- 2025年度智慧社区租赁意向协议书
- 《园林绿化工程施工方案》知识培训
- 《鼻内镜上颌窦开放》课件
- 2025版商业综合体物业服务合同招标文件3篇
- 建设工程降低成本、提高经济效益措施
- 课程思政融合深度学习的“实变函数与泛函分析”课程教学体系构建
- 助听器与辅听设备基本性能及使用建议的专家共识
- 2025年日历表( 每2个月一张打印版)
- 四年级下册数学200道竖式计算
评论
0/150
提交评论