object
1.toString方法
该方法用得比较多,一般子类都有覆盖。该方法返回的是该Java对象的内存地址经过哈希算法得出的int类型的值在转换成十六进制。这个输出的结果可以等同的看作Java对象在堆中的内存地址。
public class Test {
public static void main(String[] args) {
Object o1 = new Object();
System.out.println(o1.toString()); //java.lang.Object@16d3586
}
}
2.equals方法
该方法是非常重要的一个方法。一般equals和==是不一样的,但是在Object中两者是一样的。子类一般都要重写这个方法。
equals 与 == 操作符的区别总结如下:
- 若 == 两侧都是基本数据类型,则判断的是左右两边操作数据的值是否相等
- 若 == 两侧都是引用数据类型,则判断的是左右两边操作数的内存地址是否相同。若此时返回 true , 则该操作符作用的一定是同一个对象。
- Object 基类的 equals 默认比较两个对象的内存地址,在构建的对象没有重写 equals 方法的时候,与 == 操作符比较的结果相同。
- equals 用于比较引用数据类型是否相等。
public class Test {
public static void main(String[] args) {
String a = "abc";
String b = "abc";
String c = new String("abc");
String d = new String("abc");
System.out.println(a==b);//true
System.out.println(c==d);//false
System.out.println(a.equals(b));//true
System.out.println(c.equals(d));//true
System.out.println(b.equals(d));//true
System.out.println(a.hashCode()); //96354
System.out.println(b.hashCode()); //96354
System.out.println(c.hashCode()); //96354
System.out.println(d.hashCode()); //96354
}
}
3.hashCode方法
该方法用于哈希查找,重写了equals方法一般都要重写hashCode方法。这个方法在一些具有哈希功能的Collection中用到。
一般必须满足obj1.equals(obj2)==true。可以推出obj1.hash- Code()==obj2.hashCode(),但是hashCode相等不一定就满足equals。不过为了提高效率,应该尽量使上面两个条件接近等价。
4.getClass方法
final方法,获得运行时类型。
class A{
public void func(){
}
}
class B extends A{
}
public class Test {
public static void main(String[] args) {
A a = new A();
B b = new B();
A ab = new B();
System.out.println(a.getClass()+" "+A.class); //class A class A
System.out.println(b.getClass()+" "+B.class); //class B class B
System.out.println(ab.getClass()); //class B
ab = a;
System.out.println(ab.getClass()); //class B
}
}
5.finalize方法
该方法用于释放资源。因为无法确定该方法什么时候被调用,很少使用。
6.clone方法
保护方法,实现对象的浅复制,只有实现了Cloneable接口才可以调用该方法,否则抛出CloneNotSupportedException异常。
//浅克隆
/*在对b2中b2.aInt进行赋值后,原b1中的b1.aInt的值不会发生改变,而b2.unCA的值会改变,这是因为b2.unCA和b1.unCA是仅仅指向同一个对象的不同引用,所以调用Object类中clone()方法对于基本数据类型来说可以得到新的内存空间而对于引用数据类型来说只是生成了一个新的引用,这种被称为"浅clone"*/
class UnCloneA {
private int i;
public UnCloneA(int ii) {
i = ii;
}
public void doublevalue() {
i *= 2;
}
public String toString() {
return Integer.toString(i);
}
}
class CloneB implements Cloneable {
public int aInt;
public UnCloneA unCA = new UnCloneA(111);
public Object clone() {
CloneB o = null;
try {
o = (CloneB) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
public class CloneMain {
public static void main(String[] a) {
CloneB b1 = new CloneB();
b1.aInt = 11;
System.out.println("before clone,b1.aInt = " + b1.aInt); //11
System.out.println("before clone,b1.unCA = " + b1.unCA); //111
CloneB b2 = (CloneB) b1.clone();
b2.aInt = 22;
b2.unCA.doublevalue();
System.out.println("=================================");
System.out.println("after clone,b1.aInt = " + b1.aInt); //11
System.out.println("after clone,b1.unCA = " + b1.unCA); //222
System.out.println("=================================");
System.out.println("after clone,b2.aInt = " + b2.aInt); //22
System.out.println("after clone,b2.unCA = " + b2.unCA); //222
}
}
//深克隆
//b1.unCA与b2.unCA指向了两个不同的UnCloneA实例
class UnCloneA implements Cloneable {
private int i;
public UnCloneA(int ii) {
i = ii;
}
public void doublevalue() {
i *= 2;
}
public String toString() {
return Integer.toString(i);
}
public Object clone() {
UnCloneA o = null;
try {
o = (UnCloneA) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
class CloneB implements Cloneable {
public int aInt;
public UnCloneA unCA = new UnCloneA(111);
public Object clone() {
CloneB o = null;
try {
o = (CloneB) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
o.unCA = (UnCloneA) unCA.clone();
return o;
}
}
public class CloneMain {
public static void main(String[] a) {
CloneB b1 = new CloneB();
b1.aInt = 11;
System.out.println("before clone,b1.aInt = " + b1.aInt); //11
System.out.println("before clone,b1.unCA = " + b1.unCA); //111
CloneB b2 = (CloneB) b1.clone();
b2.aInt = 22;
b2.unCA.doublevalue();
System.out.println("=================================");
System.out.println("after clone,b1.aInt = " + b1.aInt); //11
System.out.println("after clone,b1.unCA = " + b1.unCA); 111
System.out.println("=================================");
System.out.println("after clone,b2.aInt = " + b2.aInt); //22
System.out.println("after clone,b2.unCA = " + b2.unCA); //222
}
}
7.wait方法
wait方法就是使当前线程等待该对象的锁,当前线程必须是该对象的拥有者,也就是具有该对象的锁。wait()方法一直等待,直到获得锁或者被中断。wait(long timeout)设定一个超时间隔,如果在规定时间内没有获得锁就返回。
调用该方法后当前线程进入睡眠状态,直到以下事件发生。
(1)其他线程调用了该对象的notify方法。
(2)其他线程调用了该对象的notifyAll方法。
(3)其他线程调用了interrupt中断该线程。
(4)时间间隔到了。
此时该线程就可以被调度了,如果是被中断的话就抛出一个InterruptedException异常。
8.notify方法
该方法唤醒在该对象上等待的某个线程。
9.notifyAll方法
该方法唤醒在该对象上等待的所有线程。
String方法
String常用方法
1 length()字符串的长度
public class StringDemo {
public static void main(String args[]) {
String site = "www.runoob.com";
int len = site.length();
System.out.println( "长度 : " + len );//14
}
}
2 charAt()返回指定索引处的字符。索引范围为从 0 到 length() - 1。
public class Test {
public static void main(String args[]) {
String s = "www.runoob.com";
char result = s.charAt(8);
System.out.println(result);
}
}
3 getchars():getChars() 方法将字符从字符串复制到目标字符数组。
//语法
//public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
rcBegin -- 字符串中要复制的第一个字符的索引。
srcEnd -- 字符串中要复制的最后一个字符之后的索引。
dst -- 目标数组。
dstBegin -- 目标数组中的起始偏移量。
//例子
public class Test {
public static void main(String args[]) {
String Str1 = new String("www.runoob.com");
char[] Str2 = new char[6];
try {
Str1.getChars(4, 10, Str2, 0);
System.out.print("拷贝的字符串为:" );//拷贝的字符串为:runoob
System.out.println(Str2 );
} catch( Exception ex) {
System.out.println("触发异常...");
}
}
}
4 getBytes()将字符串变成一个byte数组
//语法
getBytes() 方法有两种形式:
getBytes(String charsetName): 使用指定的字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
getBytes(): 使用平台的默认字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
语法
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
或
public byte[] getBytes()
参数
charsetName -- 支持的字符集名称。
//例子
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str1 = new String("runoob");
try{
byte[] Str2 = Str1.getBytes();
System.out.println("返回值:" + Str2 );
Str2 = Str1.getBytes( "UTF-8" );
System.out.println("返回值:" + Str2 );
Str2 = Str1.getBytes( "ISO-8859-1" );
System.out.println("返回值:" + Str2 );
} catch ( UnsupportedEncodingException e){
System.out.println("不支持的字符集");
}
}
}
5 toCharArray()将字符串变成一个字符数组
//例子
public class Test {
public static void main(String args[]) {
String Str = new String("www.runoob.com");
System.out.print("返回值 :" );
System.out.println( Str.toCharArray() );
}
}
6 equals()和equalsIgnoreCase()比较两个字符串是否相等,前者区分大小写,后者不区分
7 startsWith()和endsWith()判断字符串是不是以特定的字符开头或结束
startsWith() 方法用于检测字符串是否以指定的前缀开始。
语法
public boolean startsWith(String prefix, int toffset)
或
public boolean startsWith(String prefix)
参数
prefix -- 前缀。
toffset -- 字符串中开始查找的位置。
//例子
public class Test {
public static void main(String args[]) {
String Str = new String("www.runoob.com");
System.out.print("返回值 :" );
System.out.println(Str.startsWith("www") );
System.out.print("返回值 :" );
System.out.println(Str.startsWith("runoob") );
System.out.print("返回值 :" );
System.out.println(Str.startsWith("runoob", 4) );
}
}
endsWith() 方法用于测试字符串是否以指定的后缀结束。
语法
public boolean endsWith(String suffix)
参数
suffix -- 指定的后缀。
返回值
如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false。注意,如果参数是空字符串,或者等于此 String 对象(用 equals(Object) 方法确定),则结果为 true。
实例
public class Test {
public static void main(String args[]) {
String Str = new String("菜鸟教程:www.runoob.com");
boolean retVal;
retVal = Str.endsWith( "runoob" );
System.out.println("返回值 = " + retVal );
retVal = Str.endsWith( "com" );
System.out.println("返回值 = " + retVal );
}
}
8 toUpperCase()和toLowerCase()将字符串转换为大写或小写
toUpperCase() 方法用于将小写字符转换为大写。
语法
char toUpperCase(char ch)
参数
ch -- 要转换的字符。
返回值
返回转换后字符的大写形式,如果有的话;否则返回字符本身。
实例
public class Test {
public static void main(String args[]) {
System.out.println(Character.toUpperCase('a'));
System.out.println(Character.toUpperCase('A'));
}
}
toLowerCase() 方法用于将大写字符转换为小写。
语法
char toLowerCase(char ch)
参数
ch -- 要转换的字符。
返回值
返回转换后字符的小写形式,如果有的话;否则返回字符本身。
实例
public class Test {
public static void main(String args[]) {
System.out.println(Character.toLowerCase('a'));
System.out.println(Character.toLowerCase('A'));
}
}
9 concat() 连接两个String类型数据,不会改变原数据
连接两个String类型数据,不会改变原数据
public class Son1 extends Father1 {
public static void main(String[] args) {
String a = "abc";
String b = "bc";
String c = a.concat(b);
System.out.println(a); //abc
System.out.println(b); //bc
System.out.println(c); //abcbc
}
}
10 trim()去掉起始和结束的空格
trim() 方法用于删除字符串的头尾空白符。
语法
public String trim()
参数
无
返回值
删除头尾空白符的字符串。
实例
public class Test {
public static void main(String args[]) {
String Str = new String(" www.runoob.com ");
System.out.print("原始值 :" );
System.out.println( Str );
System.out.print("删除头尾空白 :" );
System.out.println( Str.trim() );
}
}
11 substring()截取字符串
substring() 方法返回字符串的子字符串。
语法
public String substring(int beginIndex)
或
public String substring(int beginIndex, int endIndex)
参数
beginIndex -- 起始索引(包括), 索引从 0 开始。
endIndex -- 结束索引(不包括)。
返回值
子字符串。
实例
public class Test {
public static void main(String args[]) {
String Str = new String("www.runoob.com");
System.out.print("返回值 :" );
System.out.println(Str.substring(4) );
System.out.print("返回值 :" );
System.out.println(Str.substring(4, 10) );
}
}
12 indexOf()和lastIndexOf()前者是查找字符或字符串第一次出现的地方,后者是查找字符或字符串最后一次出现的地方
13 compareTo()和compareToIgnoreCase()按字典顺序比较两个字符串的大小,前者区分大小写,后者不区分
按字典顺序比较两个字符串。该比较基于字符串中各个字符的 Unicode 值。按字典顺序将此 String 对象表示的字符序列与参数字符串所表示的字符序列进行比较。如果按字典顺序此 String 对象位于参数字符串之前,则比较结果为一个负整数。如果按字典顺序此 String 对象位于参数字符串之后,则比较结果为一个正整数。如果这两个字符串相等,则结果为 0;compareTo 只在方法 equals(Object) 返回 true 时才返回 0。
字典排序:如果这两个字符串不同,那么它们要么在某个索引处的字符不同(该索引对二者均为有效索引),要么长度不同,或者同时具备这两种情况。如果它们在一个或多个索引位置上的字符不同,假设 k 是这类索引的最小值;则在位置 k 上具有较小值的那个字符串(使用 < 运算符确定),其字典顺序在其他字符串之前。在这种情况下,compareTo 返回这两个字符串在位置 k 处两个char 值的差,即值。
public class Test {
public static void main(String[] args) {
String a="abc";
String b="abc";
String c="abcde";
String d="abbcde";
System.out.println(a.compareTo(b)); //0
System.out.println(a.compareTo(c)); //-2
System.out.println(a.compareTo(d)); //1
}
}
14 replace() 替换
replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。
语法:
public String replace(char oldChar,char newChar)
例子:
public class Test {
public static void main(String args[]) {
String Str = new String("hello");
System.out.print("返回值 :" );//hellT
System.out.println(Str.replace('o', 'T'));
System.out.print("返回值 :" );//heDDo
System.out.println(Str.replace('l', 'D'));
}
}
15 join 将一个数组或者List的各项通过分隔符连接成字符串
public class Son1 extends Father1 {
public static void main(String[] args) {
String[] a = {"abc","ef","gh"};
List<String> listStr = new LinkedList<String>();
listStr.add("a");
listStr.add("b");
listStr.add("c");
String b = String.join("",listStr); //连接List为字符串,没有分隔符
String c = String.join(",",a); //连接Array为字符串,以逗号为分隔符
System.out.println(b); //abc
System.out.println(c); //abc,ef,gh
}
}