Presentation is loading. Please wait.

Presentation is loading. Please wait.

第六章 java.lang包.

Similar presentations


Presentation on theme: "第六章 java.lang包."— Presentation transcript:

1 第六章 java.lang包

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

3 目标 了解 java.lang 包 掌握包装类 掌握String 和 StringBuffer 类 运用以下类的方法: Math Class
Object

4 包装类 6-1 ... int num1 = 5; Integer num = new Integer(num1);
int digit = 10; 使用原始数据类型 声明的变量 ... int num1 = 5; Integer num = new Integer(num1); int num2 = num.intValue(); 原始数据类型 视为对象 Java.lang 提供 原始数据类型 包装类

5 包装类 6-2 原始数据类型 包装类 byte(字节) Byte char(字符) Character int(整型) Integer
long(长整型) Long float(浮点型) Float double(双精度) Double boolean(布尔) Boolean short(短整型) Short

6 包装类 6-3 包装类的用法 使用包装类的方法,如 ceil()、floor() 和 round() 演示:示例 1
public class NumberWrap { /** 构造方法 */ protected NumberWrap() { } /** 这是 main 方法 * 它将原始值转换为其相应的包装类型 args 传递至 main 方法的参数 */ public static void main(String[] args) { String number = args[0]; 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); }} 包装类的用法 使用包装类的方法,如 ceil()、floor() 和 round() 演示:示例 1

7 包装类 6-4 Character包装类的方法 方法 说明 isDigit() 确定字符是否为 0 至 9 之间的数字 isLetter()
确定字符是否为字母 isLowerCase() 确定字符是否为小写形式 isUpperCase() 确定字符是否为大写形式 isSpace() 确定字符是否为空格或换行符 isUnicodeIdentifierStart() 确定是否允许将指定字符作为 Unicode 标识符中的首字符

8 包装类 6-5 使用包装类的方法,如 Character 类 演示:示例 2 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(values[count])) { System.out.println(values[count] + “是一个数字"); } if (Character.isLetter(values[count])) { System.out.println(values[count] + “是一个字母"); if (Character.isUpperCase(values[count])) { System.out.println(values[count] + “是大写形式"); if(Character.isUnicodeIdentifierStart(values[count])) { System.out.println(values[count] + “是 Unicode " + “标识符的第一个有效字符"); 使用包装类的方法,如 Character 类 演示:示例 2

9 String 类 String 类 字符串字面量 未修改的原始字符串 对象 可以更改字符串版本 使用 String 类的方法
原始字符串保持不变

10 String 类的构造方法 构造方法 说明 String() 它将创建一个空字符串 String(String value)
它将新建一个字符串作为指定字符串的副本 String (char [ ] value) 它将根据字符数组构造一个新字符串 String(byte [ ] value) 它将通过转换指定的字节数组新建一个字符串

11 字符串长度 2-1 字符串 长度 由 length() 方法确定 语法 返回字符串中的字符数 public int length();

12 Returns number of characters in the string
字符串长度 2-2 字符串 1 长度 String name = "John Smith"; System.out.println (name.length()); Determined by length() method Syntax Returns number of characters in the string Public int length()

13 字符串比较 4-1 字符串 1 字符串 2 用 == 运算符检查 由 equals() 方法确定 同一个对象
检查字符串是否指向同一个或不同的对象 字符串 1 由 equals() 方法确定 字符串 2 检查组成字符串内容的字符

14 字符串比较 4-2 演示:示例 3 字符串比较运算符的用法 使用 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 相等"); System.out.println("字符串 1 和字符串 2 不等"); System.out.println(“设置字符串 1 等于字符串 2"); string2 = string1; if (string1.equals(string2)) { System.out.println(“两个字符串相等"); System.out.println(“两个字符串不等"); 字符串比较运算符的用法 使用 String 类的方法,如 equals() 和 == 运算符 public class Equality { /** 构造方法 */ protected Equality() { } /**它演示两个字符串的比较 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); 演示:示例 3

15 字符串比较 4-3 方法 说明 boolean equalsIgnoreCase (String value)
此方法比较两个字符串,忽略大小写形式 int compareTo(String value) 按字母顺序比较两个字符串。 如果两个字符串相等,则返回 0; 如果字符串在该值之前,则返回值小于 0; 如果字符串在该值之后,则返回值大于 0 boolean startsWith(String value) 检查一个字符串是否以另一个字符串开始。 boolean endsWith(String value) 检查一个字符串是否以另一个字符串结束。

16 字符串比较 4-4 if (string1 == string2) { System.out.println(“字符串 A 和字符串 B 指同一个对象"); } else { System.out.println(“字符串 A 和字符串 B 指不同的对象"); if (string1.equals(string2)) { System.out.println(“字符串 A 和字符串 B 的内容相同"); 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 (string1.compareTo("Answer") == 0) { System.out.println(“按字母,字符串 A 与 Answer 的内容相同"); if (string1.startsWith("A")) { System.out.println(“以 A 开始");}}} 比较不同的字符串 使用 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); 演示:示例 4

17 indexOf(character) 方法
搜索字符串 2-1 索引 字符串 1 indexOf(character) 方法 找到第一个匹配 情形 1: 返回找到的第一个匹配的位置索引 情形 2: 如果没有找到匹配,则返回 -1

18 搜索字符串 2-2 搜索字符串内有无指定的字符或字符串 使用 String 类的方法,如 indexOf() 示例:示例 5
public class SearchString { /** 构造方法 */ protected SearchString() { } /** 这是 main 方法 * 它演示在字符串内搜索 args 传递至 main 方法的参数 */ public static void main(String[] args) { String name = System.out.println(“ ID 是: " + name); 的索引是:" + System.out.println(“. 的索引是:" + name.indexOf('.')); if (name.indexOf('.') > { System.out.println(“该电子邮件地址有效"); } else { System.out.println(“该电子邮件地址无效"); 搜索字符串内有无指定的字符或字符串 使用 String 类的方法,如 indexOf() 示例:示例 5

19 提取字符串 3-1 方法 说明 public char charAt(int index)
此方法用于从指定位置提取单个字符,该位置由索引指定,索引中的值必须为非负 public String substring(int index) 此方法用于提取从位置索引开始的字符串部分 public String substring(int beginindex, int endindex) 此方法用于提取 beginindex 和 endindex 位置之间的字符串部分 public String concat(String str) 此方法用于连接两个字符串,并新建一个包含调用字符串的字符串对象 public String replace(char old, char new) 此方法用于将调用字符串中出现某个字符的所有位置都替换为另一个字符 public String trim() 此方法用于返回一个前后不含任何空格的调用字符串的副本

20 提取字符串 3-2 方法 说明 ... char ch; ch = "orange".charAt(3);
public char charAt(int index) 此方法用于从指定位置提取单个字符,该位置由索引指定,索引中的值必须为非负。 public String substring(int index) 此方法用于提取从位置索引开始的字符串部分。 public String substring(int beginindex, int endindex) 此方法用于提取 beginindex 和 endindex 位置之间的字符串部分。 public String concat(String str) 此方法用于连接两个字符串,并新建一个包含调用字符串的 String 对象。 public String replace(char old, char new) 此方法用于将调用字符串中出现某个字符的所有位置都替换为另一个字符。 public String trim() 此方法用于返回一个前后不含任何空格的调用字符串的副本。 ... char ch; ch = "orange".charAt(3); 它将从 index(3) 中提取单个字符串 “n”并将其存储在变量 ch 中

21 提取字符串 3-3 如何使用字符串提取或字符提取
public class StringMethods { /** 构造方法 */ protected StringMethods() { } /** 这是 main 方法 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()); 如何使用字符串提取或字符提取 使用 String 类的方法,如 substring()、concat()、replace() 和 trim() 演示:示例 6

22 public String toUpperCase(); Public String toLowerCase();
更改字符串中字符的大小写 2-1 使用 toUpperCase( ) 方法 Hello HELLO 语法 public String toUpperCase(); 使用 toLowerCase( ) 方法 HELLO hello 语法 Public String toLowerCase();

23 更改字符串中字符的大小写 2-2 更改字符串中字符的大小写形式
使用 String 类的方法,如 toUpperCase() 和 toLowerCase() public class StringTest { /** 构造方法 */ protected StringTest() { } /** 这是 main 方法 * 它演示字符串的 length() 和 UpperCase() 方法 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); 演示:示例 7

24 StringBuffer 类 2-1 StringBuffer 用于表示可以修改的字符串
使用连接运算符 (+) 的字符串会自动创建字符串缓冲对象 构造方法 说明 public StringBuffer() 保留 16 个字符的空间 public StringBuffer (int length) 设置缓存器大小 public StringBuffer(Stringvalue) 接收字符串参数,用来设置初始内容, 并在不重新分配的情况下保留 16 个字符 的空间

25 StringBuffer 类 2-2 方法 说明 StringBuffer insert(String s)
在指定位置插入布尔值的字符串表示 int length( ) 确定 StringBuffer 对象的长度 void setCharAt(int pos, char ch) 使用 ch 指定的新值设置 pos 指定的位置上的字符 String toString( ) 转换为字符串形式 StringBuffer reverse() 保留 StringBuffer 对象中的字符 StringBuffer delete(int start, int end) 此方法将删除调用对象中从 start 位置开始直到 end 指定的索引 – 1 位置的字符序列 StringBuffer deleteCharAt(int pos) 此方法将删除 pos 指定的索引处的字符 StringBuffer replace(int start, int end, String s) 此方法使用一组字符替换另一组字符。将用替换字符串从 start 指定的位置开始替换,直到 end 指定的位置结束

26 不变性 2-1 String类 直接修改 创建后 不变性的概念 StringBuffer 类 String 的对等类 表示可增加和可编
解决方法 String 的对等类 表示可增加和可编 写字符的可变序列 将字符插入到字符串中间 或附加到字符串末尾

27 不变性 2-2 StringBuffer 类的用法
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); StringBuffer 类的用法 使用 StringBuffer 类的方法,如 append()、insert()、replace()、setCharAt() 和 toString() 演示:示例 8

28 Math 类 3-1 Math 类 静态方法 数字运算的方法 几何函数的方法 最终类 子 类

29 Math 类 3-2 方法 说明 double sin (double numvalue) 计算角 numvalue 的正弦值
double cos (double numvalue) 计算角 numvalue 的余弦值 double pow (double a, double b) 计算 a 的 b 次方 double sqrt (double numvalue) 计算给定值的平方根 int abs (int numvalue) 计算 int 类型值 numvalue 的绝对值,也接收 long、float 和 double 类型的参数 double ceil (double numvalue) 返回大于等于 numvalue 的最小整数值 double floor (double numvalue) 返回小于等于 numvalue 的最大整数值 int max(int a, int b) 返回 int 型值 a 和 b 中的较大值,也接收 long、float 和 double 类型的参数 int min(int a, int b) 返回 a 和 b 中的较小值,也可接收 long、float 和 double 类型的参数

30 Math 类 3-3 Math 类的用法 使用 Math 类的方法,如 ceil()、floor() 和 round() 演示:示例 9
public class MathDemo { /** 构造方法 */ protected MathDemo() { } /** main 方法演示 Math 类的不同方法 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)); Math 类的用法 使用 Math 类的方法,如 ceil()、floor() 和 round() 演示:示例 9

31 Class 类 2-1 自动创建对象 无需声明 使用对象中的 getClass( ) 方法 通过 创建的对象
通过 创建的对象 使用静态 forName( ) 方法 使用自定义 ClassLoader 对象加载新类

32 Class 类 2-2 Class 类的用法 使用 Class 类的方法,如 getClass() 和 getSuperClass()
public class ClassDemo { /**构造方法 */ protected ClassDemo() { } /** 这个类演示 Class 类的访问方法 args 传递至 main 方法的参数 */ public static void main(String[] args) { StoreString objString = new StoreString(); StoreInteger objInteger = new StoreInteger(); Class objClass; objClass = objString.getClass(); System.out.println(“objString 对象的类型是: “ + objClass.getName()); objClass = objInteger.getClass(); System.out.println(“objInteger 对象的类型是: " + objClass.getName()); objClass = objClass.getSuperclass(); System.out.println(“objInteger的父类是" + objClass.getName()); class StoreString { /**构造方法. */ protected StoreString() { } private String name = "diana"; /** 这个类扩展 StoreString 类.*/ class StoreInteger extends StoreString { /** 构造方法.*/ protected StoreInteger() { /** 该变量存储整数值. */ private int deptno; 演示:示例 10

33 Object 类 2-1 所有类的父类 默认情况下,用户定义的类扩展自 Object 类 方法 说明 boolean
equals(Objectobj) 将当前对象实例与给定的对象进行比较,检查 它们是否相等 void finalize() throws Throwable 当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。通常被子类重写 String toString() 返回此对象的字符串表示 void wait() throws InterruptedException 使当前线程进入等待状态

34 Object 类 2-2 Object 类的用法 使用 Object 类的方法,如 equals() 演示:示例 11
1.0, 2005 年 6 月 13 日 Ben  */ public class ObjectDemo {     /** 构造方法 */     protected ObjectDemo() {     }     /** 这是 main 方法      * 它演示 Object 类 args 传递至 main 方法的参数      */   public static void main(String[] args) {    if (args[0].equals(“Java”)) {    System.out.println(“是, Java 是一项非常好的技术!");       }    } } Object 类的用法 使用 Object 类的方法,如 equals() 演示:示例 11

35 总结 包中的类必须保存在与包同名的文件夹下 默认情况下,会将 java.lang 包导入到每个 Java 程序中
包装类可以以类的形式封装简单的原始类型 StringBuffer 类用作构建字符串的构建块 字符串是不可变的,也就是说字符串是常量并且不能改变它们的值 Math 是一个 final 类,用于定义基本数字运算和三角函数的方法


Download ppt "第六章 java.lang包."

Similar presentations


Ads by Google