常用类总结(上)
Object:
java.lang.Object
Object:Java中所有的类(自定义的类,jdk提供的类)都继承自Object(所有的根类)
public int hashCode()返回该对象的哈希码值(在内存中每个对象的哈希码值不一样),跟地址值有关系(哈希算法算出来的)虽然跟地址值有关系,理解为"地址值",但是不是实际意义的地址值!
public final Class getClass()返回当前某个类的运行时的类(就是字节码文件在运行)
返回值是一个引用类型(具体类)
Class 类的实例表示正在运行的 Java 应用程序中的类和接口(跟反射有关系)
Class类成员方法:public String getName():以 String 的形式返回此 Class 对象所表示的实体(类,接口…)
//举例
public class ObjectDemo{
public static void main(String[] args){
Student s1=new Student();
Student s2=new Student();
System.out.println(s1.hashCode());//305808283
System.out.println(s2.hashCode());//292938459
Class c=s1.getClass();
System.out.println(c);//class com.qianfeng.object_01.Student //Class类对象
//public String getName()
String name = c.getName() ;
System.out.println(name);//com.qianfeng.object_01.Student (一个类的全限定名称:包名.类名)
}
}
Object类中toString()方法
public String toString():返回该对象的字符串表示结果应是一个简明但易于读懂的信息表达式。(建议所有子类都重写此方法)
创建一个类对象:直接输出对象名和使用对象名.toString()是等价的
toString():包名.类名 +"@"+十六进制数据===>等价于 某个对象.getClass().getName() +"@" +Integer.toHexString(对象.hasCode())
public class ObjectDemo {
public static void main(String[] args) {
//创建一个学生类
Student s1 = new Student("高圆圆",30) ;
System.out.println(s1);//com.qianfeng.object_02.Student@7de26db8
//输出s1的toString
System.out.println(s1.toString());//com.qianfeng.object_02.Student@7de26db8
//toString()实质的意义:就是为了描述当前对象的信息表达式(属性信息)
Object类中equals方法
public boolean equals(Object obj):表达某个对象和当前obj对象是否"相等"
==:连接的是两个对象:比较的是两个对象地址值是否相同
==:如果连接的是两个数值(整数默认int),比较的两个值是否相同!
public class ObjectDemo {
public static void main(String[] args) {
//创建学生对象
Student s1 = new Student("高圆圆",27) ;
Student s2 = new Student("高圆圆",27) ;
System.out.println(s1==s2);//false
Student s3 = s1 ;
System.out.println(s1==s3);//true
System.out.println(s1.equals(s2));//false (没有重写Object类中equals方法)
//true(重写了Object类中的equals方法,比较是当前两个对象的内容(成员变量)是否相同)
}
}
Object类中clone()方法(了解)
protected Object clone() :(浅克隆)
throws CloneNotSupportedException
创建并返回该对象的副本
注意事项:
Object 类的 clone 方法执行特定的复制操作。
首先,如果此对象的类不能实现接口 Cloneable,则会抛出 CloneNotSupportedException
finalize():跟垃圾回收器有关系(当前一个对象没有更多引用的时候,被GC回收,调用这个finalize())
System.gc(): 手动垃圾回收器
String:
String 类代表字符串。
Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现。字符串是常量,一旦被赋值,其值不能更改!(此处指的是地址值)
构造方法:
1)String():空参构造
2)public String(char[] value) :将字符数组转换成字符串
3)String(char[] value, int offset, int count):将字符数组的一部分转换成字符串
4)public String(byte[] bytes) :将字节数转换成字符串
5)public String(byte[] bytes, int offset, int length):将字节数组一部分转换成字符串内容
6)String(String original) :直接传递一个字符串常量值 String(“hello”);
public class StringDemo {
public static void main(String[] args) {
//String():空参构造
String s1 = new String() ;
System.out.println(s1+"---"+s1.length());
System.out.println("---------------------");
//public String(char[] value)//将字符数组转换成字符串
char[] chs = {'a','b','c','d','e'} ;
//String s2 = new String(chs) ;//将字符数组的一部分转换成字符串
String s2 = new String(chs, 2, 3) ;
System.out.println(s2);//"abcde"
System.out.println("---------------------");
//public String(byte[] bytes) //将字节数转换成字符串
byte[] bytes = {97,98,99,100,101} ;
//String s3 = new String(bytes) ; //97---->ASCII码表中对应的字符 a ,b,c...
String s3 = new String(bytes, 1, 2) ;
System.out.println("---------------------");
String s4 = new String("hello") ;//类似于:String s4 = "hello" ;(此处与String s4="hello";有所不同前者:在堆内存开辟空间,并且常量还要在常量存储一份(两个)后者:直接使用常量在常量值找,是否存在,如果存在返回地址 (一个))
System.out.println(s4);
System.out.println(s4.length());
}
}
判断功能的常用方法:
public boolean equals (Object anObject): (常用)
比较两个字符串内容是是否相同(区分大小写)
public boolean equalsIgnoreCase (String anotherString):
比较两个字符串的内容是否相同,不区分大小写
public boolean isEmpty(): 判断对象是否为空
public boolean contains(String s):判断字符串是否包含在指定字符串中
public class StringDemo {
public static void main(String[] args) {
String s1 = "helloWorld" ;
String s2 = "HelloWORLD" ;
// public boolean equals (Object anObject)://比较两个字符串内容是是否相同(区分大小写)
System.out.println("equals:"+s1.equals(s2));//false
//public boolean equalsIgnoreCase (String anotherString):
System.out.println("eqalsIgoreCase:"+s1.equalsIgnoreCase(s2));//true
//public boolean isEmpty(): 判断对象是否为空
System.out.println(s1.isEmpty());
// public boolean contains(String s):
System.out.println("contains:"+s1.contains("ak47"));
System.out.println("contains:"+s1.contains("oWo"));
System.out.println("-------------------------------");
//public boolean startsWith(String prefix)
System.out.println("startsWith:"+s1.startsWith("hel"));
//public boolean endsWith(String suffix) :
System.out.println("endsWith:"+s1.endsWith("ld"));
}
}
== 和equals的区别:
== :连接的是两个对象,比较的两个对象地址值是否相同(如果是对象比较的是地址值);
大部分jdk提供的一些常用类---->都已经重写了equals方法
某个对象.equals(传递一个对象):默认比较是地址值,String类重写了equals方法,所以比较字符串的内容是否相同
获取功能的方法(重要)
public int length () :返回此字符串的长度。
public String concat (String str) :将指定的字符串连接到该字符串的末尾。(拼接的功能)
public char charAt (int index) :返回指定索引处的 char值。
public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
public String substring (int beginIndex, int endIndex) :
返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
public class StringDemo {
public static void main(String[] args) {
//定义一个字符串
String s = "hellojava" ;
//public int length () :返回此字符串的长度
System.out.println("length:"+s.length());
public String concat (String str) :将指定的字符串连接到该字符串的末尾
String s1 = "ee" ;
String s2 = s.concat(s1) ;
System.out.println("s2:"+s2);
System.out.println("------------------");
//public char charAt (int index) :返回指定索引处的 char值。
System.out.println("charAt:"+s.charAt(4));
//有一个字符串数据,需要将字符串数据遍历! (普通for和charAt()相结合,就可以遍历字符串)
//public int indexOf (String str)
System.out.println("indexOf:"+s.indexOf("owo"));
System.out.println("-----------------------------");
//public String substring (int beginIndex):从指定位置默认截取到末尾(返回被截取的字符串)
System.out.println("substring:"+s.substring(5));//java
//public String substring (int beginIndex, int endIndex):包前不包后(包左不包右)
System.out.println("substring:"+s.substring(5, 9));
System.out.println(s.toUpperCase());
}
}
新的体会:本周学习了常用类,由于常用类比较多,先总结一部分,随后在将剩下的补上