今日内容介绍
一、Object
二、String
三、StringBuilderjava
b: 案例代码git
public class Person extends Object{ private String name; private int age; public Person(){} public Person(String name, int age) { this.name = name; this.age = age; } /* * 将父类的equals方法写过来,重写父类的方法 * 可是,不改变父类方法的源代码, 方法equals 比较两个对象的内存地址 * */ public boolean equals(Object obj){ return this == obj; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } //测试代码 public class TestEquals { public static void main(String[] args) { //Person类继承Object类,继承下来了父类的方法equals Person p1 = new Person("李四",20); Person p2 = new Person("张三",20); //Person对象p1,调用父类的方法equals,进行对象的比较 boolean b = p1.equals(p1); System.out.println(b); } }
public class Person extends Object{ private String name; private int age; public Person(){} public Person(String name, int age) { this.name = name; this.age = age; } /* * 重写父类的方法toString() * 没有必要让调用者看到内存地址 * 要求: 方法中,返回类中全部成员变量的值 */ public String toString(){ return name + age; } /* * 将父类的equals方法写过来,重写父类的方法 * 可是,不改变父类方法的源代码, 方法equals 比较两个对象的内存地址 * * 两个对象,比较地址,没有意义 * 比较两个对象的成员变量,age * 两个对象变量age相同,返回true,不一样返回false * * 重写父类的equals,本身定义本身对象的比较方式 */ public boolean equals(Object obj){ if( this == obj){ return true; } //对参数obj,非null判断 if( obj == null){ return false; } if( obj instanceof Person){ // 参数obj接受到是Person对象,才能转型 // 对obj参数进行类型的向下转型,obj转成Person类型 Person p = (Person)obj; return this.age == p.age; } return false; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } //测试代码 public class TestEquals { public static void main(String[] args) { //Person类继承Object类,继承下来了父类的方法equals Person p1 = new Person("李四",20); Person p2 = new Person("张三",20); //Person对象p1,调用父类的方法equals,进行对象的比较 boolean b = p1.equals(p1); System.out.println(b); } }
/* * 重写父类的方法toString() * 没有必要让调用者看到内存地址 * 要求: 方法中,返回类中全部成员变量的值 */ public String toString(){ return name + age; } //Eclipse中自动生成的toString @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } //测试代码 public class TestToString { public static void main(String[] args) { //调用Person类的方法toString() //输出语句中,写的是一个对象,默认调用对象的toString方法 Person p = new Person("张三",20); String s = p.toString(); System.out.println(p); System.out.println(s); /* * System.out.println(p); * System.out.println(p.toString()); */ /*Random r = new Random(); System.out.println(r.toString()); Scanner sc = new Scanner(System.in); System.out.println(sc.toString());*/ } }
/* * String类特色: * 一切都是对象,字符串事物 "" 也是对象 * 类是描述事物,String类,描述字符串对象的类 * 全部的 "" 都是String类的对象 * * 字符串是一个常量,一旦建立,不能改变 */ public class StringDemo { public static void main(String[] args) { //引用变量str执行内存变化 //定义好的字符串对象,不变 String str = "itcast"; System.out.println(str); str = "itheima"; System.out.println(str); } }
public class StringDemo2 { public static void main(String[] args) { //字符串定义方式2个, 直接= 使用String类的构造方法 String str1 = new String("abc"); String str2 = "abc"; System.out.println(str1); System.out.println(str2); System.out.println(str1==str2);//引用数据类型,比较对象的地址 false System.out.println(str1.equals(str2));//true } }
* public String():空构造 * public String(byte[] bytes):把字节数组转成字符串 * public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串 * public String(String original):把字符串常量值转成字符串
public class StringDemo3 { public static void main(String[] args) { function_1(); } /* * 定义方法,String类的构造方法 * String(byte[] bytes) 传递字节数组 * 字节数组转成字符串 * 经过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。 * 平台 : 机器操做系统 * 默认字符集: 操做系统中的默认编码表, 默认编码表GBK * 将字节数组中的每一个字节,查询了编码表,获得的结果 * 字节是负数,汉字的字节编码就是负数, 默认编码表 ,一个汉字采用2个字节表示 * * String(byte[] bytes, int offset, int length) 传递字节数组 * 字节数组的一部分转成字符串 * offset 数组的起始的索引 * length 个数,转几个 , 不是结束的索引 */ public static void function(){ byte[] bytes = {97,98,99,100}; //调用String类的构造方法,传递字节数组 String s = new String(bytes); System.out.println(s); byte[] bytes1 ={65,66,67,68,69}; //调用String构造方法,传递数组,传递2个int值 String s1 = new String(bytes1,1,3); System.out.println(s1); } }
a: 常见构造方法面试
/* * String类构造方法 * String类的构造方法,重载形式 * */ public class StringDemo3 { public static void main(String[] args) { function_1(); } /* * String(char[] value) 传递字符数组 * 将字符数组,转成字符串, 字符数组的参数,不查询编码表 * * String(char[] value, int offset, int count) 传递字符数组 * 将字符数组的一部分转成字符串 * offset 数组开始索引 * count 个数 */ public static void function_1(){ char[] ch = {'a','b','c','d','e','f'}; //调用String构造方法,传递字符数组 String s = new String(ch); System.out.println(s); String s1 = new String(ch,1,4); System.out.println(s1); } }
* A:String类的其余方法 * a: 方法介绍 * int length(): 返回字符串的长度 * String substring(int beginIndex,int endIndex): 获取字符串的一部分 * String substring(int beginIndex): 获取字符串的一部分 * boolean startsWith(String prefix): 判断一个字符串是否是另外一个字符串的前缀,开头 * boolean endsWith(String prefix): 判断一个字符串是否是另外一个字符串的后缀,结尾 * boolean contains (String s): 判断一个字符串中,是否包含另外一个字符串 * int indexOf(char ch): 查找一个字符,在字符串中第一次出现的索引,被查找的字符不存在,返回-1 * byte[] getBytes(): 将字符串转成字节数组,此功能和String构造方法相反,byte数组相关的功能,查询编码表 * char[] toCharArray(): 将字符串转成字符数组,功能和构造方法相反 * boolean equals(Object obj): 方法传递字符串,判断字符串中的字符是否彻底相同,若是彻底相同返回true * boolean equalsIgnoreCase(String s): 传递字符串,判断字符串中的字符是否相同,忽略大小写 * b: 案例代码 public class StringDemo4 { public static void main(String[] args) { function_9(); } /* * boolean equals(Object obj) * 方法传递字符串,判断字符串中的字符是否彻底相同,若是彻底相同返回true * * boolean equalsIgnoreCase(String s) * 传递字符串,判断字符串中的字符是否相同,忽略大小写 */ public static void function_9(){ String str1 = "Abc"; String str2 = "abc"; //分别调用equals和equalsIgnoreCase boolean b1 = str1.equals(str2); boolean b2 = str1.equalsIgnoreCase(str2); System.out.println(b1); System.out.println(b2); } /* * char[] toCharArray() 将字符串转成字符数组 * 功能和构造方法相反 */ public static void function_8(){ String str = "itcast"; //调用String类的方法toCharArray() char[] ch = str.toCharArray(); for(int i = 0 ; i < ch.length ; i++){ System.out.println(ch[i]); } } /* * byte[] getBytes() 将字符串转成字节数组 * 此功能和String构造方法相反 * byte数组相关的功能,查询编码表 */ public static void function_7(){ String str = "abc"; //调用String类方法getBytes字符串转成字节数组 byte[] bytes = str.getBytes(); for(int i = 0 ; i < bytes.length ; i++){ System.out.println(bytes[i]); } } /* * int indexOf(char ch) * 查找一个字符,在字符串中第一次出现的索引 * 被查找的字符不存在,返回-1 */ public static void function_6(){ String str = "itcast.cn"; //调用String类的方法indexOf int index = str.indexOf('x'); System.out.println(index); } /* * boolean contains (String s) * 判断一个字符串中,是否包含另外一个字符串 */ public static void function_5(){ String str = "itcast.cn"; //调用String类的方法contains boolean b =str.contains("ac"); System.out.println(b); } /* * boolean endsWith(String prefix) * 判断一个字符串是否是另外一个字符串的后缀,结尾 * Demo.java * .java */ public static void function_4(){ String str = "Demo.java"; //调用String类方法endsWith boolean b = str.endsWith(".java"); System.out.println(b); } /* * boolean startsWith(String prefix) * 判断一个字符串是否是另外一个字符串的前缀,开头 * howareyou * hOw */ public static void function_3(){ String str = "howareyou"; //调用String类的方法startsWith boolean b = str.startsWith("hOw"); System.out.println(b); } /* * String substring(int beginIndex,int endIndex) 获取字符串的一部分 * 返回新的字符串 * 包含头,不包含尾巴 * * String substring(int beginIndex)获取字符串的一部分 * 包含头,后面的字符全要 */ public static void function_2(){ String str = "howareyou"; //调用String类方法substring获取字符串一部分 str= str.substring(1, 5); System.out.println(str); String str2 = "HelloWorld"; str2 = str2.substring(1); System.out.println(str2); } /* * int length() 返回字符串的长度 * 包含多少个字符 */ public static void function(){ String str = "cfxdf#$REFewfrt54GT"; //调用String类方法length,获取字符串长度 int length = str.length(); System.out.println(length); } }
案例代码编程
public class StringTest { public static void main(String[] args) { getCount("A%A3eBr1FFy"); } /* * 获取指定字符串中,大写字母、小写字母、数字的个数。 * 思想: * 1. 计数器,就是int变量,知足一个条件 ++ * 2. 遍历字符串, 长度方法length() + charAt() 遍历 * 3. 字符判断是大写,是小写,仍是数字 */ public static void getCount(String str){ //定义三个变量,计数 int upper = 0; int lower = 0; int digit = 0; //对字符串遍历 for(int i = 0 ; i < str.length() ; i++){ //String方法charAt,索引,获取字符 char c = str.charAt(i); //利用编码表 65-90 97-122 48-57 if(c >='A' && c <=90){ upper++; }else if( c >= 97 && c <= 122){ lower++; }else if( c >= 48 && c <='9'){ digit++; } } System.out.println(upper); System.out.println(lower); System.out.println(digit); } }
C: 案例代码api
public class StringTest { public static void main(String[] args) { System.out.println(toConvert("aBc5%4dEF")); } /* * 将字符串的首字母转成大写,其余内容转成小写 * 思想: * 获取首字母, charAt(0) substring(0,1) * 转成大写 toUpperCase() * * 获取剩余字符串, substring(1) toLowerCase() */ public static String toConvert(String str){ //定义变量,保存首字母,和剩余字符 String first = str.substring(0,1); String after = str.substring(1); //调用String类方法,大写,小写转换 first = first.toUpperCase(); after = after.toLowerCase(); return first+after; } }
package cn.itcast.demo02; public class StringTest { public static void main(String[] args) { System.out.println(getStringCount("hellojava,nijavahaojava,javazhenbang", "java")); } /* * 获取一个字符串中,另外一个字符串出现的次数 * 思想: * 1. indexOf到字符串中到第一次出现的索引 * 2. 找到的索引+被找字符串长度,截取字符串 * 3. 计数器++ */ public static int getStringCount(String str, String key){ //定义计数器 int count = 0; //定义变量,保存indexOf查找后的索引的结果 int index = 0; //开始循环找,条件,indexOf==-1 字符串没有了 while(( index = str.indexOf(key) )!= -1){ count++; //获取到的索引,和字符串长度求和,截取字符串 str = str.substring(index+key.length()); } return count; } }
public class StringBufferDemo { public static void main(String[] args) { function_5(); } /* * StringBuffer类的方法 * String toString() 继承Object,重写toString() * 将缓冲区中的全部字符,变成字符串 */ public static void function_5(){ StringBuffer buffer = new StringBuffer(); buffer.append("abcdef"); buffer.append(12345); //将可变的字符串缓冲区对象,变成了不可变String对象 String s = buffer.toString(); System.out.println(s); } /* * StringBuffer类的方法 * reverse() 将缓冲区中的字符反转 */ public static void function_4(){ StringBuffer buffer = new StringBuffer(); buffer.append("abcdef"); buffer.reverse(); System.out.println(buffer); } /* * StringBuffer类方法 * replace(int start,int end, String str) * 将指定的索引范围内的全部字符,替换成新的字符串 */ public static void function_3(){ StringBuffer buffer = new StringBuffer(); buffer.append("abcdef"); buffer.replace(1, 4, "Q"); System.out.println(buffer); } /* * StringBuffer类方法 insert * insert(int index, 任意类型) * 将任意类型数据,插入到缓冲区的指定索引上 */ public static void function_2(){ StringBuffer buffer = new StringBuffer(); buffer.append("abcdef"); buffer.insert(3, 9.5); System.out.println(buffer); } /* * StringBuffer类方法 * delete(int start,int end) 删除缓冲区中字符 * 开始索引包含,结尾索引不包含 */ public static void function_1(){ StringBuffer buffer = new StringBuffer(); buffer.append("abcdef"); buffer.delete(1,5); System.out.println(buffer); } /* * StringBuffer类方法 * StringBuffer append, 将任意类型的数据,添加缓冲区 * append 返回值,写return this * 调用者是谁,返回值就是谁 */ public static void function(){ StringBuffer buffer = new StringBuffer(); //调用StringBuffer方法append向缓冲区追加内容 buffer.append(6).append(false).append('a').append(1.5); System.out.println(buffer); } }
public class StringBufferTest { public static void main(String[] args) { int[] arr = {4,1,4,56,7,8,76}; System.out.println(toString(arr)); } /* * int[] arr = {34,12,89,68};将一个int[]中元素转成字符串 * 格式 [34,12,89,68] * String s = "[" * 数组遍历 * s+= arr[i]; * s+"]" * StringBuffer实现,节约内存空间, String + 在缓冲区中,append方法 */ public static String toString(int[] arr){ //建立字符串缓冲区 StringBuffer buffer = new StringBuffer(); buffer.append("["); //数组遍历 for(int i = 0 ; i < arr.length;i++){ //判断是否是数组的最后一个元素 if(i == arr.length-1){ buffer.append(arr[i]).append("]"); }else{ buffer.append(arr[i]).append(","); } } return buffer.toString(); } }
做业测试数组
1.用代码演示String类中的如下方法的用法安全
(1)boolean isEmpty(): 判断字符串是否是空串,若是是空的就返回true (2)char charAt(int index): 返回索引上的字符 (3)String toLowerCase(): 字符串转成小写 (4)String toUpperCase(): 字符串转成大写 (5)String repalce(char oldChar, char newChar): 将字符串中的老字符,替换为新字符 (6)String repalce(String old, String newstr): 将字符串中的老字符串,替换为新字符串 (7)String trim(): 去掉字符串两端空格
2.分析如下需求,并用代码实现:app
(1)定义以下方法public static String getPropertyGetMethodName(String property); (2)该方法的参数为String类型,表示用户给定的成员变量的名字,返回值类型为String类型,返回值为成员变量对应的get方法的名字 (3)如:用户调用此方法时给定的参数为"name",该方法的返回值为"getName"
3.分析如下需求,并用代码实现:dom
(1)定义数字字符串数组{"010","3223","666","7890987","123123"} (2)判断该数字字符串数组中的数字字符串是不是对称(第一个数字和最后一个数字相等,第二个数字和倒数第二个数字是相等的,依次类推)的,并逐个输出 (3)如:010 是对称的,3223 是对称的,123123 不是对称的 (4)最终打印该数组中对称字符串的个数 提示:循环获取字符串的每个字符,依次比较第一个和最后一个,第二个和倒数第二个。。。
4.分析如下需求,并用代码实现:ide
(1)从键盘循环录入录入一个字符串,输入"end"表示结束 (2)将字符串中大写字母变成小写字母,小写字母变成大写字母,其它字符用"*"代替,并统计字母的个数 举例: 键盘录入:Hello12345World 输出结果:hELLO*****wORLD 总共10个字母
5.分析如下需求,并用代码实现:
(1)从键盘循环录入录入一个字符串,输入"end"表示结束 (2)定义一个方法 public Object[] deleteSubString(String str1,String str2) { } (3)方法功能描述:从str1中删除全部的str2,并返回删除后的结果,返回结果为Object[]数组 * 该数组的第一个元素为删除全部的str2后的最终的字符串 * 该数组的第二个元素为删除的str2的个数
6.关于String类的练习题,分析运行结果?
public class Test01 { public static void main(String[] args) { //demo1(); //demo2(); //demo3(); //demo4(); demo5(); } private static void demo5() { String s1 = "ab"; String s2 = "abc"; String s3 = s1 + "c"; System.out.println(s3 == s2); System.out.println(s3.equals(s2)); //true } private static void demo4() { //byte b = 3 + 4; //在编译时就变成7,把7赋值给b,常量优化机制 String s1 = "a" + "b" + "c"; //java中有常量优化机制,在编译时期就能肯定s2的值为"abc",因此编译时期,在常量池中建立"abc" String s2 = "abc";//执行到这里时常量池中已经有了"abc",因此就再也不建立, //因此s1和s2指向的是常量池中同一个字符串常量"abc" System.out.println(s1 == s2); //true,java中有常量优化机制 System.out.println(s1.equals(s2)); //true } private static void demo3() {//==比较的是地址值 String s1 = new String("abc"); //录的是堆内存对象的地址值 String s2 = "abc"; //记录的是常量池中的地址值 System.out.println(s1 == s2); //false System.out.println(s1.equals(s2)); //true } private static void demo2() { //建立几个对象 //建立两个对象,一个在常量池中,一个在堆内存中 String s1 = new String("abc"); System.out.println(s1); } private static void demo1() { //常量池中没有这个字符串对象,就建立一个,若是有直接用便可 String s1 = "abc"; String s2 = "abc"; System.out.println(s1 == s2); //==号比较的是地址值,true System.out.println(s1.equals(s2)); //比较的是字符串的内容:true } }