Object类
是所有类的根类,本类中的所有方法,所有累都继承
Object类中的方法
判断两个对象是否相等
public boolean equals(Object Obj)
返回该对象的哈希码值
public int hashcode()
返回Object的运行时类
public final classgetClass()
返回该对象的字符串表示形式
public String toString()
==与equals
基本数据类型比较
3==3 true(比较的是值)
对象的比较
New Person()==New Person() false(比较的是内存地址,必须是同一类型的对象才可以比较否则报编译错误)
hashCode
返回该对象的哈希码值:
New Person().hashCode() -->int 值 对象的地址为十进制的
Integer.toHexString(int型数据)–>10进制转换成16进制
哈希码值就是对象的地址
getClass
对象名.getClass();得到当前对象的包名及类名
package fuxi.com;
public class Outer {
public static void main(String[] args) {
Outer p1=new Outer();
Outer p2=new Outer();
System.out.println(p1.getClass());
System.out.println(p2.getClass());
System.out.println(p2.getClass().getName());
System.out.println(p2.getClass().getSuperclass());
//比如说可以返回类名
//知道该类中字段
//知道该类中的方法名
//知道该类中的参数名
//知道该类中方法返回类型
//返回一个Class,封装了该类的信息
}
}
toString
toString中打印的是操作系统为我们计算的对象的内存地址,想要我们的值,如下:
String对象的创建
方法一
String a=“abc”;
方法二
String a=new String(“abc”);
区别:方法一为赋值方式,值被创建会存入字符串缓冲区,无论a怎样更改,缓冲区内的abc都不会更改,新创建的值会添加到缓冲区中,例如 String a=“abc”; String a=“bc”; ,那么字符串缓冲区就会添加bc,而不是在abc中去掉a,abc仍然存在字符串缓冲区中,若String b=“bc”; ,则会到字符串常量池中查找,若找到bc,则a与b共同指向字符串缓冲区中的bc,而不是在创建bc到常量池中
方法二为新建对象,创建的字符串不会被放入字符串缓冲区,并且每一个对象的内存地址都不相同,无论值是否相等。同时每一个对象指向一个值,不会出现多个对象指向一个值的情况。并且String a=new String();创建String对象,并初始化字符串,等价于String a=""; 不等价String a=null;
用赋值的方式更加节省空间,效率更高
常用的String类方法
查找
获取字符串的长度
String str="mvklakval";
System.out.println(str.length());//9
根据位置获取字符
String str="mvklakval";
System.out.println(str.charAt(4));//a
根据字符获取字符在字符串中第一次出现的位置
String str="mvklakval";
System.out.println(str.indexOf("k"));//2
从指定位置开始查找
String str="mvklakval";
System.out.println(str.indexOf("l",4));//8
根据字符获取字符在字符串中最后一次出现的位置
String str="mvklakval";
System.out.println(str.lastIndexOf("l"));//8
从指定位置开始查找反向搜索
String str="mvklakval";
System.out.println(str.lastIndexOf("k", 3));//2
截取字符串
String str="mvklakval";
System.out.println(str.substring(2, 5));//kla(包前不包后,截取下标第2个,但不截取下标第5个)
截取字符串的一部分
String str="mvklakval";
System.out.println(str.substring(5));//kval(从第五个开始截取一直到结尾)
转换
字符转换成字符串数组(切割)
String str="m*v*k*la*kv*al";
String [] x=str.split("\\*");
for (int i = 0; i < x.length; i++) {
System.out.print(x[i]);
}//切割*,并用数组接收,for循环遍历出来
字符串转成字符数组
String str="mvklakval";
char [] x=str.toCharArray();
for (int i = 0; i < x.length; i++) {
System.out.print(x[i]);
}
字符串转成字节数组
String str="mvklakval";
byte [] x=str.getBytes();
for (int i = 0; i < x.length; i++) {
System.out.print(x[i]);
}
将字符串中的字符全部大写
String str="mvklakval";
System.out.println(str.toUpperCase());
将字符串中的字符全部小写
String str = "mvklakval";
System.out.println(str.toLowerCase());
将字符串中的内容进行替换
String str = "mvklakval";
System.out.println(str.replace("l", "0"));
去掉字符串两端的空格
String str = " mvklakval ";
System.out.println(str.trim());
字符串连接
String str = "mvklakval";
System.out.println("aa".concat(str));
将基本数据类型转成字符串
System.out.println(String.valueOf(100));
判断
比较字符串内容
String str1=" hello world";
System.out.println(" hello world".equals(str1));
不考虑大小写比较字符串内容
String str1=" helLo world";
System.out.println(" hello world".equalsIgnoreCase(str1));
字符串是否包含指定字符串
String st="abc";
String st1="abc1";
System.out.println(st1.contains(st));//true
字符串是否以指定字符串开头(区分大小写)
String st="abc";
System.out.println(st.startsWith("b"));/false
字符串是否以指定字符串结尾(区分大小写)
String st="abc";
System.out.println(st.endsWith("c"));//true
StringBuffer类
是字符串缓冲区,作用是方便了操作字符串
特点:
长度可变,可以储存不同类型的数据、接收不同类型的数据要转成字符串使用
StringBuffer方法
添加
StringBuffer st=new StringBuffer("aa");
System.out.println(st.append("ss"));//添加在后面
System.out.println(st.insert(0, "ss"));//根据下标位置添加
删除
System.out.println(st.delete(0, 2));//包前不包后 0~2下标删除的是0~1
System.out.println(st.deleteCharAt(2));//根据下标删除单个字符
查找
StringBuffer st=new StringBuffer("abcd");
System.out.println(st.charAt(1));//查找某一位置上的字符
System.out.println(st.indexOf("c"));//查找某一字符的位置
修改
StringBuffer st=new StringBuffer("abcd");
System.out.println(st.replace(1, 3, "h"));//从哪个下标开始,修改到哪个下标结束,不包括后面下标
反转
System.out.println(st.reverse());