【object类】
(1)equals方法:比较两个对象的内存地址是否相等。
equalis源码:
public boolean equals(Object obj) {
return (this == obj);
}
重写equalis源码:
// 比较Person类型不同对象之间的age是否相等
public boolean equals(Object obj) {
// 若比较的为同一对象
if(this == obj){
return true;
}
// 排除null
if(obj == null){
return false;
}
// 排除非Person对象
if(obj instanceof Person){
Person p = (Person)obj; // Person为子类 向下转型
return (this.age == p.age);
}
else{
return false;
}
}
(2)toString方法
public class Test {
public static void main(String[] args) {
// 调用Person类的方法toString
// 输出语句中是一个Person对象,默认调用对象的toString方法
Person p = new Person("张三","0001");
System.out.println(p);
System.out.println(p.toString());
}
}
输出:
usst.javacode.obj.Person@193c0cf
usst.javacode.obj.Person@193c0cf
重写toString方法
// 重写toString方法
// 要求:返回类中所有成员变量的值
public String toString(){
return name+Id;
}
public class Test {
public static void main(String[] args) {
// 调用Person类的方法toString
// 输出语句中是一个Person对象,默认调用对象的toString方法
Person p = new Person("张三","0001");
System.out.println(p);
System.out.println(p.toString());
}
}
输出:
张三0001
张三0001
【String类】
String类代表 字符串。java程序中所有字符串面值(如"abc")都作此类的实例对象。字符串是常量,它们的值在创建后不能更改。
// str引用变量指向内存变化
// 定义好的字符串对象不变
String str = "秋香";
System.out.println(str); //秋香
str = "春香";
System.out.println(str); //春香
String s1 = new String("abc"); // 创建了两个对象,"abc"是一个对象,New String()是一个
String s2 = "abc";
System.out.println(s1 == s2); // false 引用数据类型 比较对象的地址
System.out.println(s1.equals(s2)); // true String 重写了object的equals方法:比较字符串中的每个字符是否相等
【String类的构造函数举例】
public static void main(String[] args) {
function();
function1();
}
/*
* 定义方法 String类 构造方法
*
* 1.字节数组转成字符串
* String(byte[] bytes ) 传递字节数组
* 使用平台默认的字符集解码 指定的byte 数组 构造一个新的String
* 平台:机器操作系统 默认字符集:操作系统中默认的编码表 , 默认编码表GBK
*
* 将字节数组中的每个字节,查询了编码表,得到的结果 (字节数为正,查询ASCII;为负查询GBK)
* 汉字的字节编码是负数,默认GBK,一个汉字采用2个字节表示
*
* 2.字节数组的一部分转换成字符串
* String(byte[] bytes, int offset, int length) 字节数组的一部分转换成字符串
* offset:数组的初始索引 length:数组的个数
*/
public static void function(){
// 1.字节数组转成字符串
byte[] bytes = {97,98,-99,-100}; // 字节数组
// 调用String类的构造方法,传递字节数组
String s = new String(bytes);
System.out.println(s); // ab潨
// 2.字节数组的一部分转换成字符串
byte[] bytes1 = {65,66,67,68,69};
String s1 = new String(bytes1,1,2);
System.out.println(s1); // BC
}
/*
* 1.传递字符数组
* String(char[] value) 将字符数组转换成字符串,字符数组的参数不查询编码表
*
* 2.将字符数组的一部分转换成字符串
* String(char[] vale, int offset, int count)
* offset:数组开始索引
* count:数组的个数
*
*/
public static void function1(){
// 1.
char[] ch = {'a','b','c','d'};
String s = new String(ch);
System.out.println(s); //"abcd"
// 2.
String s1 = new String(ch,2,1);
System.out.println(s1); // c
}
【String类的方法】
public static void main(String[] args) {
function_8();
}
/*1.返回字符串的长度
* int length()
*/
public static void function_1(){
String str1 = "ababan";
// 调用String类的方法length(),来获取字符串的长度
int length = str1.length();
System.out.println(length);
}
/*2.获取字符串的一部分
* String substring(int beginIndex, int endIndex)
* 注意:返回新的字符串
*
* String substring(int beginIndex)
* 获取指定范围以后的字符串
*/
public static void function_2(){
String str2 = "pandans";
// 调用String类的方法substring来获取字符串的一部分
String str3 = str2.substring(1, 3); // [1,3) 包头不包尾
System.out.println(str3); // an
String str4 = str2.substring(1);
System.out.println(str4); // andans
}
/*3.测试此字符串是否以指定的前缀开始
* boolean startsWith(String prefix)
*
* 判断一个字符串是否为另一个字符串的后缀
* boolean endsWith(String prefix)
*/
public static void function_3(){
String str3 = "flower.java";
// 调用String类的方法startsWith,来判断此字符串是否以指定的前缀开始
boolean s3 = str3.startsWith("flo");
System.out.println(s3); // true
boolean s4 = str3.endsWith(".java");
System.out.println(s4); // true
}
/*4.判断一个字符串中是否包含另一个字符串
* boolean contains(String s)
*
*/
public static void function_4(){
String str4 = "fofofoffo";
// 调用String类的方法contains,来判断一个字符串是否包含另一个字符串
boolean s4 = str4.contains("ofo");
System.out.println(s4); // true
}
/*5.返回一个字符在一个字符串中,第一次出现的索引
* int indexof(char ch)
* 若查找的字符不存在,返回-1
*
*/
public static void function_5(){
String str5 = "agshsahshh";
// 调用String 类的indexof 方法,返回一个字符在一个字符串中首次出现的索引
int i5 = str5.indexOf('h');
System.out.println(i5); // 3
}
/*6.将字符串转换成 字节数组
* byte[] getBytes()
* 此功能与String 构造方法相反,byte数组的相关功能,查询编码表
*
*/
public static void function_6(){
String str6 = "当归";
// 调用String类的方法getBytes,将字符串转换成字符数组
byte[] bytes6 = str6.getBytes();
for(int i=0;i<bytes6.length;i++){
System.out.println(bytes6[i]); // -75,-79,-71,-23
}
}
/*7.将字符串转换成字符数组
* char[] toCharArray
* 功能和构造方法相反
*/
public static void function_7(){
String str7 = "abstract";
// 调用String类的方法,将字符串转换成字符数组
char[] ch7 = str7.toCharArray();
for(int i=0;i<ch7.length;i++){
System.out.print(ch7[i]); // a b s t r a c t
}
}
/*8.判断字符串中的字符是否完全相同,若完全相等返回true
* boolean equals(object obj)
*
* 判断字符串中的字符是否相同,忽略大小写
* boolean equalsIgnoreCase(String s)
*/
public static void function_8(){
String str1 = "Abc";
String str2 = "abc";
// 分别调用equals 和equalsIgnoreCase()
boolean b1 = str1.equals(str2);
boolean b2 = str1.equalsIgnoreCase(str2);
System.out.println(b1); // false
System.out.println(b2); // ture
}
【获取指定字符串中的大写,小写字符,数字的个数】
public class Test1 {
public static void main(String[] args) {
String s = "AAbbngf188";
fun1(s);
System.out.println(toconvert(s));
String str = "AAjavajiijavakkkhjavaj";
String key = "java";
System.out.println(getStringCount(str,key));
}
/* 获取指定字符串中,大写字母,小写字母,数字的个数
* 思想:
* 1.计数器,就是int变量,满足一个条件就++
* 2.遍历字符串,长度方法length() + charAt()遍历
* 3.字符判断是大写,小写数字
*/
public static void fun1(String str){
int upper = 0;
int lower = 0;
int digit = 0;
for(int i=0;i<str.length();i++){
// String类方法charAt,根据索引来获取字符
char ch = str.charAt(i);
// 编码表,65~90(A~Z) 97~122(a~z) 48~57(0~9)
if(ch >= 65 && ch <= 90){ // if(ch >= 'A' && ch <= 'Z')
upper++;
}else if(ch >= 97 && ch <= 122){
lower++;
}else if(ch >= 48 && ch <=57){
digit++;
}
}
System.out.println(upper+" "+lower+" "+digit);
}
/*将字符串的首字母改为大写,其他元素改为小写
* 思想:
* 1.获取首字母,charAt(0) substring(0,1)
* 2.转成大写 toUpperCase()
* 3.其他部分 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;
}
/*获取一个字符串中,另一个字符串的个数
* 思路:
* 1.indexof 从一个字符串中找另一个字符串第一次出现的索引
* 2.找到索引+所找字符串的长度,截取字符串 substring()
* 3.count计数
*/
public static int getStringCount(String str, String key){
// 定义计数器
int count = 0;
// 定义变量,保存indexof查找后的索引
int index = 0;
while( (index = str.indexOf(key)) != -1 ){
count++;
str = str.substring(index+key.length());
}
return count;
}
}
【StringBuffer】
/*
* StringBuffer 字符串缓冲区 提高了字符串的操作效率
* 内部采用了可变数组的方法实现,类内部定义类数组,这个数组没有final
* char[]数组 默认容量为16个字符
*
*/
public class Test2 {
public static void main(String[] args) {
function_1();
}
/* 1.StringBuffer类方法
* StringBuffer append,将任意类型的数据,添加到缓冲区
*
* 2.delete(int star, int end) 删除缓冲区内容 注意:包头不包尾
*
* 3.insert(int index, 任意类型) 将任意类型的数据,插入到缓冲区指定的索引上去
*
* 4.replace(int star, int end, String str) 将指定索引范围内的字符,替换为新的字符串
*
* 5.reverse() 将缓冲区中的字符翻转
*
* 6.String toString() 继承object,重写toString() 将缓冲区内的所有字符转换成 字符串
*/
public static void function_1(){
StringBuffer buffer = new StringBuffer();
// 1.调用StringBuffer方法append向缓冲区追加内容
buffer.append(61234).append(true);
System.out.println(buffer); // 6true
// 2.调用StringBuffer方法delete,删除缓冲区内容
buffer.delete(1,3);
System.out.println(buffer); // 634true
// 3.调用StringBuffer方法insert,将任意类型的数据,插入到缓冲区指定的索引上
buffer.insert(0,19.01);
System.out.println(buffer); //19.01634true
// 4.调用StringBuffer方法replace,将指定范围内的字符,替换为新的字符串
buffer.replace(0, 2, "zp");
System.out.println(buffer); // zp.01634true
// 5.调用StringBuffer方法reverse,将字符串中的字符翻转
buffer.reverse();
System.out.println(buffer); // eurt43610.pz
// 6.调用StringBuffer方法toString,将缓冲区中的字符转换成字符串
buffer.append(1212);
// 将可变的缓冲区对象,变成不可变的String对象
String s = buffer.toString();
System.out.println(s); //eurt43610.pz1212
}
}
public class Test3 {
public static void main(String[] args) {
int[] arr = {12,34,55,26};
toString(arr);
}
/* 将数组 {12,34,55,26},转换成[12,34,55,26]的形式
* 使用StringBuffer,节约内存空间
*/
public static void 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(",");
}
}
System.out.println(buffer); // [12,34,55,26]
}
}