java lang q java lang q q String

  • Slides: 42
Download presentation
java. lang包

java. lang包

目标 q 了解 java. lang 包 q 掌握包装类 q 掌握String 和 String. Buffer 类

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

包装类 6 -2 4 原始数据类型 包装类 byte(字节) Byte char(字符) Character int(整型) Integer long(长整型) Long

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

包装类 6 -3 public class Number. Wrap { /** 构造方法 */ q protected Number.

包装类 6 -3 public class Number. Wrap { /** 构造方法 */ q protected Number. Wrap() { 包装类的用法 q }使用包装类的方法,如 ceil()、floor() 和 round() /** 这是 main 方法 * 它将原始值转换为其相应的包装类型 * @param args 传递至 main 方法的参数 */ public static void main(String[] args) { String number = args[0]; Byte by. Num = Byte. value. Of(number); Short sh. Num = Short. value. Of(number); Integer num = Integer. value. Of(number); Long lg. Num = Long. value. Of(number); System. out. println("Output"); System. out. println(by. Num); System. out. println(sh. Num); System. out. println(num); System. out. println(lg. Num); }} 7 演示:示例 1

包装类 6 -5 public class Test. Character { q public static void main(String[] args)

包装类 6 -5 public class Test. Character { q public static void main(String[] args) { 使用包装类的方法,如 Character 类 int count; char[] values = {'*', '7', 'p', 'P'}; for (count = 0; count < values. length; count++) { if (Character. is. Digit(values[count])) { System. out. println(values[count] + “是一个数字"); } if (Character. is. Letter(values[count])) { System. out. println(values[count] + “是一个字母"); } if (Character. is. Upper. Case(values[count])) { System. out. println(values[count] + “是大写形式"); } if(Character. is. Unicode. Identifier. Start(values[count])) { System. out. println(values[count] + “是 Unicode " + “标识符的第一个有效字符"); } } 演示:示例 2 9}

字符串长度 2 -2 字符串 1 长度 Determined by length() method String name = "John

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

字符串比较 4 -2 if (string 1 == string 2) { System. out. println(“字符串 1

字符串比较 4 -2 if (string 1 == string 2) { System. out. println(“字符串 1 和字符串 2 相等"); } else { q 字符串比较运算符的用法 System. out. println(“字符串 1 和字符串 2 不等"); q } 使用 String 类的方法,如 equals() 和 == 运算符 if (string 1. equals(string 3)) { public class Equality { System. out. println(“字符串 1 和字符串 3 相等"); /** 构造方法 */ } protected Equality() { else { } System. out. println("字符串 1 和字符串 2 不等"); /**它演示两个字符串的比较 } * @param args 传递至 main 方法的参数 System. out. println(“设置字符串 1 等于字符串 2"); */ string 2 = string 1; public static void main(String [] args) { if (string 1. equals(string 2)) { String string 1 = new String(“苹果是一种水果"); System. out. println(“两个字符串相等"); String string 2 = new String(“玫瑰花是一种花"); } String string 3 = new String(“苹果是一种水果"); else { System. out. println(“字符串 1: " + string 1); System. out. println(“两个字符串不等"); System. out. println(“字符串 2: " + string 2); 演示:示例 3 } System. out. println(“字符串 3: " + string 3); } }16

字符串比较 4 -4 if (string 1 == string 2) { System. out. println(“字符串 A

字符串比较 4 -4 if (string 1 == string 2) { System. out. println(“字符串 A 和字符串 B 指同一个对象"); } q 比较不同的字符串 else { System. out. println(“字符串 A 和字符串 B 指不同的对象"); q 使用 String 类的方法,如 equals. Ignore. Case()、 } public class Stringdemo { compare. To()、starts. With() 和 ends. With() if (string 1. equals(string 2)) { /** 构造方法 */ System. out. println(“字符串 A 和字符串 B 的内容相同"); } protected Stringdemo() { } else { /** 这是 main 方法 System. out. println(“字符串 A 和字符串 B 的内容不同"); } * 它演示 String 类的比较方法 * @param args 传递至 main 方法的参数 if (string 1. equals. Ignore. Case(string 2)) { */ System. out. println(“忽略大小写,字符串 A 和 B 的内容相同"); } public static void main(String [] args) { String string 1, string 2, string 3; else if (string 1. equals. Ignore. Case(string 3)) { string 1 = new String("Answer"); System. out. println(“字符串 A 和 B 的内容相同"); } string 2 = new String("ANSWER"); string 3 = new String("Question"); if (string 1. compare. To("Answer") == 0) { 演示:示例 4 System. out. println(“字符串 A 是 " + string 1); System. out. println(“按字母,字符串 A 与 Answer 的内容相同"); } System. out. println(“字符串 B 是 " + string 2); if (string 1. starts. With("A")) { System. out. println(“以 A 开始"); }}} 18 System. out. println(“字符串 C 是 " + string 3);

搜索字符串 2 -2 public class Search. String { q /** 构造方法 */ 搜索字符串内有无指定的字符或字符串 protected

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

提取字符串 3 -3 public class String. Methods { /** 构造方法 */ protected String. Methods()

提取字符串 3 -3 public class String. Methods { /** 构造方法 */ protected String. Methods() { q }如何使用字符串提取或字符提取 q /** 这是 main 方法 使用 String 类的方法,如 substring()、concat()、 * @param args 传递至 main 方法的参数 replace() 和 trim() */ public static void main(String [] args) { String s = "Java is a " + "platform independent language"; String s 1 = "Hello world"; String s 2 = "Hello"; String s 3 = "HELLO"; System. out. println(s); System. out. println("index of t = " + s. index. Of('t')); System. out. println("last index of t = " +s. last. Index. Of('t')); System. out. println("index of(t, 10) = " +s. index. Of('t‘, 10)); System. out. println(s 1. substring(3, 8)); System. out. println(s 2. concat("World")); System. out. println(s 2. replace('l', 'w')); System. out. println(s 1. trim()); } 演示:示例 6 23 }

Matchs方法 "Java". matches("Java"); "Java". equals("Java"); "Java is fun". matches("Java. *") "Java is cool". matches("Java.

Matchs方法 "Java". matches("Java"); "Java". equals("Java"); "Java is fun". matches("Java. *") "Java is cool". matches("Java. *") "Java is powerful". matches("Java. *") 24

更改字符串中字符的大小写 2 -1 Hello 使用 to. Upper. Case( ) 方法 HELLO 语法 public String

更改字符串中字符的大小写 2 -1 Hello 使用 to. Upper. Case( ) 方法 HELLO 语法 public String to. Upper. Case(); HELLO 使用 to. Lower. Case( ) 方法 语法 Public String to. Lower. Case(); 26 hello

更改字符串中字符的大小写 2 -2 q public class String. Test { 更改字符串中字符的大小写形式 /** 构造方法 */ q

更改字符串中字符的大小写 2 -2 q public class String. Test { 更改字符串中字符的大小写形式 /** 构造方法 */ q protected String. Test() { 使用 String 类的方法,如 to. Upper. Case() 和 } to. Lower. Case() /** 这是 main 方法 * 它演示字符串的 length() 和 Upper. Case() 方法 * @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 name. Uppercase = name. to. Upper. Case(); System. out. println(name. Uppercase); 演示:示例 7 } } 27

不变性 2 -2 public class String. Buf { q /** 构造方法 */ String. Buffer

不变性 2 -2 public class String. Buf { q /** 构造方法 */ String. Buffer 类的用法 protected String. Buf() { q }使用 String. Buffer 类的方法,如 append()、insert()、 public static void main(String []args) { replace()、set. Char. At() 和 to. String() String. Buffer buf = new String. Buffer("Java"); buf. append(“ Guide Ver 1/”); buf. append(3); int index = 5; buf. insert(index, "Student "); index = 23; buf. set. Char. At(index, '. '); int start = 24; int end = 25; buf. replace(start, end, "4"); String s = buf. to. String(); //转换为字符串 System. out. println(s); 演示:示例 8 } } 31

Math 类 3 -2 方法 说明 double sin (double numvalue) 计算角 numvalue 的正弦值 double

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 类型的参数 35

Math 类 3 -3 public class Math. Demo { q /** 构造方法 */ Math

Math 类 3 -3 public class Math. Demo { q /** 构造方法 */ Math 类的用法 protected Math. Demo() { q }使用 Math 类的方法,如 ceil()、floor() 和 round() /** main 方法演示 Math 类的不同方法 * @param args 传递至 main 方法的参数 */ public static void main(String[] args) { /** 此变量存储 num 的值*/ int num = 38; /** 该变量存储 num 1 的值*/ float num 1 = 65. 7 f; System. out. println(Math. ceil(num)); System. out. println(Math. ceil(num 1)); System. out. println(Math. floor(num 1)); System. out. println(Math. round(num 1)); } 演示:示例 9 } 36

Class 类 2 -2 qpublic class Class. Demo { Class 类的用法 q/**构造方法 */ 使用

Class 类 2 -2 qpublic class Class. Demo { Class 类的用法 q/**构造方法 */ 使用 Class 类的方法,如 get. Class() 和 get. Super. Class() protected Class. Demo() { class Store. String { } /**构造方法. */ /** 这个类演示 Class 类的访问方法 protected Store. String() { * @param args 传递至 main 方法的参数 } */ private String name = "diana"; public static void main(String[] args) { } Store. String obj. String = new Store. String(); Store. Integer obj. Integer = new Store. Integer(); /** 这个类扩展 Store. String 类. */ Class obj. Class; class Store. Integer extends Store. String { obj. Class = obj. String. get. Class(); /** 构造方法. */ System. out. println(“obj. String protected Store. Integer() { 对象的类型是: “ + obj. Class. get. Name()); obj. Class = obj. Integer. get. Class(); } System. out. println(“obj. Integer 对象的类型是: " + obj. Class. get. Name()); /** 该变量存储整数值. */ obj. Class = obj. Class. get. Superclass(); private int deptno; System. out. println(“obj. Integer 的父类是" + obj. Class. get. Name()); } } } 38 演示:示例 10

Object 类 2 -2 q/** 这个类演示 Object 类的用法 * @version 1. 0, 2005 年

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