Object类的概述
- Object类的概述
1.Object是类层次结构的根类 2.所有类都直接或者间接的继承Object类 3.Object类的构造方法:public Object() 4.子类的构造方法默认访问的是父类的无参构造方法
class Student{
public Student(){
super();//这调用就是Object的无参构造方法
}
- Object的hashCode方法
1.Object有一个方法 public int hashCode() 2.方法返回对象的哈希码值,默认情况下该方法会根据对象的地址来计算 3.不同对象的hashCode()一般来说不会相同,但同一个对象的hashcode值肯定是相同的 4.hashCode不是对象的实际地址值,可以理解为逻辑地址值
Student stu1 = new Student();
Student stu2 = new Student();
Student stu3 = stu1;
System.out.println("stu1:" + stu1.hashCode());
System.out.println("stu2:" + stu2.hashCode());
System.out.println("stu3:" + stu3.hashCode());
输出结果为
stu1:284720968
stu2:122883338
stu3:284720968
可见stu1和stu3哈希值相同
- Object的getClass方法
1.public final class getClass() 2.返回此object运行时类 3.可以通过class类中的一个方法,获取对象的真实类的全名称 getName()--包名+类名 getSimpleName() -- 类名 - Object类的toString 方法
1.toString方法返回此对象的字符串表示 2.包名.类名+@+Integer.toHexString(d.hashCode()) 3.toString方法默认返回的字符没啥用 4.打印对象,默认就是调用toString方法 5.此方法一般用于自定义字符串输出 当你直接打印一个对象的时候,会调用Object的toString
重写object的toString方法(其实可以一键生成?)
class Student {
String name;
int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj){
//1.把obj还原成Student
Student other = (Student) obj;
boolean b = this.age == other.age && this.name.equals(other.name);
return b;
}
}
- Object类的toString 方法
1.==是一个比较运算符号,即可以比较基本数据类型,也可以比较引用数据类据类型 基本数据类型比较的是值,引用数据类型比较的是地址值 2.equals方法是一个方法,只能比较引用数据类型,所有对象都会继承object类中的方法 如果没有重写Object类中的equals方法,equals方法和==号比较引用数据类型无区别 重写后的equals方法比较的是对象中的属性
Object的String类
面试题:String这个类能不能被继承,或者说有没有子类?
* 答:String这个类是被final修饰的,所以不能被继承,也就是没有子类
*1.String 类代表字符串 。
* 2.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
* 3.字符串是常量;它们的值在创建之后不能更改。
// 1.public boolean equals(Object anObject)
// 判断字符串是否一样
String s1 = "abc";
String s2 = "ab"+"c";
System.out.println(s1.equals(s2));
// 2.public boolean equalsIgnoreCase(String anotherString) 用在验证码
// 判断字符串是否一样,忽略大小写
String s3 = "Abc";
System.out.println("s1与s3:"+s1.equals(s3));
System.out.println("s1与s3:"+s1.equalsIgnoreCase(s3));
// 3.public boolean contains(CharSequence s)
//判断字符串是否包含哪字符串
String s4 = "I hate you";
System.out.println(s4.contains("Love"));
// 4.public boolean startWith(String prefix)
//判断字符串以什么开头
String s5 = "luck";
System.out.println(s5.startsWith("A"));
// 5.public boolean endsWith(String suffix)
//判断字符串是否以什么结尾
String s6 = "lily.png";
System.out.println(s6.endsWith(".png"));
// 6.public boolean isEmpty()
// 判断字符串是否为空字符串
String s7 = "java";
System.out.println(s7.isEmpty());
输出结果
true
s1与s3:false
s1与s3:true
false
false
true
false
//String 类的获取功能【掌握】
// 1.int length():获取字符串的长度
String s1 = "520java";
System.out.println("s1的长度:"+ s1.length());
// 2.char charAt(int index):获取指定索引位置的字符
String s2 = "studio";
char c = s2.charAt(0);
System.out.println(c);
// 3.int indexOf(int ch):获取指定字符在此字符串第一次出现处的索引
String s3 = "educationa";
System.out.println(s3.indexOf(97));
// 4.int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引
//如果结果是-1,表示你指定的字符串没有出现过
String s4 = "WeLoveJava";
System.out.println(s4.indexOf("Love"));
// 5.int indexOf(int ch,int fromIndex):返回指定字符在此字符串中指定位置后第一次出现处的索引
String s5 = "abcdefal";
System.out.println(s5);
System.out.println(s5.indexOf('a',2));
// 6.int indexOf(String str,int fromIndex):返回指定字符串在此字符串中指定位置后第一次出现处的索引
String s6 = "I Love Java,I Love Gosling";
System.out.println(s6.indexOf("Love", 3));
// 7.lastIndexOf() 最后出现的位置
String s7 = "ababaccad";
System.out.println(s7.lastIndexOf('a'));
// 8.String substring(int start):从指定位置开始截取字符串,默认到未尾
String s8 = "Gosling,How Are You!";
String tmp = s8.substring(8);
System.out.println(tmp);
// 9.String substring(int start,int end):从指定位置开始到指定位置结束截取字符图
System.out.println(s8.substring(2, 7));
输出结果
s1的长度:7
s
4
2
abcdefal
6
14
7
How Are You!
sling
//案例:统计不同类型字符个数
String password = "abZZc23&%Asd^4";
//密码:大写,小写,数字,特殊字符
//1.声明变量
int big = 0; //大写
int small = 0; // 小写
int num = 0;//数字
int other = 0;//特殊字符
//2.遍历字符串的字符
for(int i=0; i < password.length();i++){
//取字符
char ch = password.charAt(i);
System.out.println(ch);
//判断字符是否为大写
if(ch >= 'A' && ch <= 'Z'){//判断是否为大写
big ++;
}else if(ch >= 'a' && ch <= 'z'){//判断是否为小写
small++;
}else if(ch >='0' && ch <='9'){//判断是否为数字
num++;
}else {
other ++;
}
}
System.out.println("大写字母出现的次数:" + big);
System.out.println("小写字母出现的次数:" + small);
System.out.println("数字出现的次数:" + num);
System.out.println("特殊字符出现的次数:" + other);
//String类的转换功能
//1.byte[] getBytes():把字符串转换为字节数组
System.out.println("========byte[] getBytes():把字符串转换为字节数组========");
String s1 = "521java";
//NSDate = byte[]
byte[] bytes = s1.getBytes();
for (int i = 0 ;i < bytes.length ; i++){
System.out.println(bytes[i]);
}
// char[] toCharArray():把字符串转换为字符数组
System.out.println("=========char[] toCharArray():把字符串转换为字符数组========");
String s2 = "java";
char[] chs = s2.toCharArray();
for(int i = 0 ; i < chs.length;i++){
System.out.println(chs[i]);
}
// static String valueOf(char[] chs):把字符数组转成字符串
System.out.println("=======static String valueOf(char[] chs):把字符数组转成字符串========");
char[] chs2 = {'L','O','V','E'};
String s3 = String.valueOf(chs2);
System.out.println("s3="+s3);
// static String valueOf(int i):把int类型的数据转成字符串
System.out.println("=======static String valueOf(int i):把int类型的数据转成字符串========");
int a = 97;
String s4 = String.valueOf(a);
System.out.println(s4);
// String toLowerCase():把字符串转成小写
System.out.println("========String toLowerCase():把字符串转成小写=====");
String s5 = "HELLO,HOW ARE YOU!";
s5 = s5.toLowerCase();
System.out.println("s5"+s5);
// String toUpperCase():把字符串转成大写
System.out.println("=======String toUpperCase():把字符串转成大写=====");
String s6 = "hi,麻辣吉斯";
s6 = s6.toUpperCase();
System.out.println("s6:"+s6);
// String concat(String str):把字符串拼接,且只能拼接字符串。+方式拼接字符串时,可以是任意类型
System.out.println("=======String concat(String str):把字符串拼接,且只能拼接字符串======");
String s7 = "When";
String s8 = " you go home?";
String s9 = s7.concat(s8);
//String s9 = s7 + s8 + 98
System.out.println("s9:"+s9);
运行结果
========byte[] getBytes():把字符串转换为字节数组========
53
50
49
106
97
118
97
=========char[] toCharArray():把字符串转换为字符数组========
j
a
v
a
=======static String valueOf(char[] chs):把字符数组转成字符串========
s3=LOVE
=======static String valueOf(int i):把int类型的数据转成字符串========
97
========String toLowerCase():把字符串转成小写=====
s5hello,how are you!
=======String toUpperCase():把字符串转成大写=====
s6:HI,麻辣吉斯
=======String concat(String str):把字符串拼接,且只能拼接字符串======
s9:When you go home?
//案例:把数组转成字符串
/* 把数组中的数据按照指定的格式拼接成字符串
eg:int[] arr = {1,2,3} 转成 “[1,2,3]”*/
//1.声明个数组
int[] arr = {1,2,3,4,5,6};
//2.声明一个字符串
String str = "[";
//3.遍历元素
for(int i = 0;i<arr.length;i++){
//str = str + arr[i] + ",";
if(i == arr.length -1 ){
str += arr[i] + "]";
}else {
str += arr[i] + ",";
}
}
System.out.println(str);
输出结果
[1,2,3,4,5,6]
//String类的其它功能
// public String replace(char oldChar, char newChar)//替换字符
String s1 = "hello";
s1 = s1.replace('l','y');
System.out.println("s1:"+s1);
// public String replace(CharSequence target, CharSequence replacement) //替换字符串
//CharSequence,接口,字符系列
//String是CharSequence的实现类
String s2 = "I Love You!";
s2 = s2.replace("Love", "Hate");
System.out.println("s2:"+ s2);
// String trim();//去除前后空格
String s3 = " Java Gosling ";
System.out.println("s3:" + s3);
s3 = s3.trim();
System.out.println("s3:" + s3);
// public int compareTo(String anotherString) //比较字符串的大小
String s4 = "abc";
String s5 = "abc";
int result = s4.compareTo(s5);
System.out.println("s4 pk s5 = " + result);
// public int compareToIgnoreCase(String str) //比较
String s6 = "AbB";
String s7 = "aBc";
System.out.println("s6 pk s7 = " + s6.compareToIgnoreCase(s7));
输出结果
s1:heyyo
s2:I Hate You!
s3: Java Gosling
s3:Java Gosling
s4 pk s5 = 0
s6 pk s7 = -1
``````java
//案例:统计大串中小串出现的次数
String str = "helgyfnnnngyfooogyfgyf88gyf";
String name = "gyf";
int count = 0;
while (true){
System.out.println(str);
int index = str.indexOf(name);
if(index == -1){//没找到退出循环
break;
}
//找到,截取字符,再+1
str = str.substring(index + name.length());
count ++;
}
System.out.println(name + "出现次数"+count);
输出结果
helgyfnnnngyfooogyfgyf88gyf
nnnngyfooogyfgyf88gyf
ooogyfgyf88gyf
gyf88gyf
88gyf
gyf出现次数5
Comments NOTHING