Object
Object类是java中所有类的父类,因为所有类都继承Object这个类,所以它中的方法可以被任意子类调用。在java1.7中Object主要方法包含以下几种:
方法摘要 | |
---|---|
protected Object |
clone() 创建并返回此对象的一个副本。 |
boolean |
equals(Object obj) 指示其他某个对象是否与此对象“相等”。 |
protected void |
finalize() 当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。 |
Class<?> |
getClass() 返回此 Object 的运行时类。 |
int |
hashCode() 返回该对象的哈希码值。 |
void |
notify() 唤醒在此对象监视器上等待的单个线程。 |
void |
notifyAll() 唤醒在此对象监视器上等待的所有线程。 |
String |
toString() 返回该对象的字符串表示。 |
void |
wait() 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。 |
void |
wait(long timeout) 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量前,导致当前线程等待。 |
void |
wait(long timeout, int nanos) 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者其他某个线程中断当前线程,或者已超过某个实际时间量前,导致当前线程等待。 |
hashCode()方法是返回该对象的哈希码值(为int型),作用是为了提高哈希码表的性能。哈希码值我们暂时理解为地址值,将这个值用Interger.toHexString(对象名.hashCode())将其转换为16进制及是我们在eclipse中看到的地址值,后面了解后进行补充。
getClass()方法是返回此Object的运行时类(为class<?>型)。返回的Class对象是由所表示类的 static synchronized方法锁定的对象。
例子:
class Student{ } class Student1{ public void show() { System.out.println("helloworld!"); } } public class Demo1 { public static void main(String[] args) { Student s =new Student(); Student1 s1 =new Student1(); System.out.println(s.hashCode());//输出哈希码值 System.out.println(Integer.toHexString(s.hashCode()));//将int类型的哈希码值转换为16进制 System.out.println(s.getClass());//输出Object运行时的类的类名 System.out.println(s1.getClass().getName());//以 String 的形式返回此 Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称。 System.out.println("String type:"+s1.getClass().getName().toString()); System.out.println(s1);//输出全路径+@+地址值 System.out.println(s1.getClass().getName() + '@' + Integer.toHexString(s1.hashCode()));//拼接出全路径+@+地址值 System.out.println(s1.toString());//实际输出的是全路径+@+地址值 System.out.println(s1.equals(s));//因为不具有相同的哈希码值,所以不相等,输出false。 } }
toString ()
返回该对象的字符串表示(为String型)。通常toString方法会返回一个“以文本方式表示”此对象的字符串。建议子类都重写这个方法。
重写toString方法的例子:
class Student2{ private int age ; private String name ; @Override public String toString() { return "Student [age=" + age + ", name=" + name + "]"; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } } public class Test2 { public static void main(String[] args) { Student2 s = new Student2(); s.setAge(22); s.setName("aloha"); System.out.println(s.toString()); } }
equals(Object obj)
指示其他某个对象是否与此对象“相等”(地址比较,可以重写这个方法实现数值比较)。equals方法在非空对象引用上实现相等关系:自反性:对于任意非空引用值x,x.equals(x)都应返回true。 对称性:对于任何非空引用值x和y,当且仅当y.equals(x)返回true时,x.equals(y)才返回true。 传递性:对于任何非空引用值 x、y、z,如果x.equals(y)返回true,并且y.equals(x)返回值为true,则x.equals(z)应返回true。 一致性:对于任何非空引用之x和y,多次调用x.equals(y)始终返回true或始终返回false,前提是对象上equals比较中所用的信息都没有被改变。 对于任何非空引用值,x,x.equals(null)都应返回false。
注意:当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码!!
重写equals(Object obj)方法例子:
class Student4{ private String name ; private int age ; public Student4() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student4(String name, int age) { super(); this.name = name; this.age = age; } @Override public boolean equals(Object obj) {//重写的equals alt+shift+s+h if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student4 other = (Student4) obj; if (age != other.age) //age = this.age return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } class Student4plus{ } public class Demo3 { public static void main(String[] args) { Student4 s1 = new Student4(); Student4 s2 = new Student4(); System.out.println(s1.equals(s2));//重写equals后得到的是数值的比较结果 } }
clone()
若需修改一个对象,同时不想改变调用者的对象,就要制作该对象的一个本地副本。这也是本地副本最常见的一种用途。若决定制作一个本地副本,只需简单地使用clone()方法即可。Clone是“克隆”的意思,即制作完全一模一样的副本。这个方法在基础类Object中定义成“protected”(受保护)模式。但在希望克隆的任何衍生类中,必须将其覆盖为“public”模式。
clone方法的作用是创建并返回对象的一个副本。“副本”的准确含义可能依赖于对象的类,这样的目的是对于任意对象x.clone()!=x为true;x.clone().getClass()==x.getClass()也为true。Object 类的 clone 方法执行特定的复制操作。首先,如果此对象的类不能实现接口 Cloneable,则会抛出 CloneNotSupportedException。
注意,所有的数组都被视为实现接口 Cloneable。
克隆的例子:
class Student5 implements Cloneable{//若要被复制必须实现cloneable接口功能 private String name; private int age ; public Student5() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student5(String name, int age) { super(); this.name = name; this.age = age; } //必须重写克隆方法,因为clone是被protected修饰的。 @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } public class Demo4 { public static void main(String[] args) throws CloneNotSupportedException { Student5 s5 =new Student5("aloha",22); System.out.println(s5.getName()+" "+s5.getAge()); Object obj = s5.clone(); Student5 s = (Student5)obj;//向下转型,完成克隆 // Student5 s1 =(Student5)s5.clone(); System.out.println(s.getName()+" ~ "+s.getAge()); // System.out.println(s1.getName()+" ~ "+s1.getAge()); } }
Scanner
常用方法:
boolean |
hasNextInt() 如果通过使用 nextInt() 方法,此扫描器输入信息中的下一个标记可以解释为默认基数中的一个 int 值,则返回 true。 |
boolean |
hasNextLine() 如果在此扫描器的输入中存在另一行,则返回 true。 |
构造方法:
public Scanner(InputStream source): 以输入流的形式录入数据的
InputStream:字节输入流:
InputStream in = System.in ; //底层执行返回的是一个字节输入流(标准输入流)
public Scanner(InputStream source): 以输入流的形式录入数据的
InputStream:字节输入流:
InputStream in = System.in ; //底层执行返回的是一个字节输入流(标准输入流)
异常:java.util.InputMismatchException:录入的数据和接收的数据类型不匹配异常
String
String表示字符串是一个常量,它们的值在创建后不能更改。字符串是一种特殊的
引用类型
,默认值是null。
String的构造方法有:
String() 无参构造
public String(byte[] bytes) byte[]-->String
public String(byte[] bytes,int offset,int length) 将bytes[]按照offset开始的length长进行转换
public String(char[] value) 将字节数组转换为字符串
public String(char[] value,int offset,int length) 将字节数组按照offset开始的length长进行转换
public String(String Original) 将一个字符串常量构造成一个字符串对象
关于String的构造方法的应用例子:
public class Demo5 { public static void main(String[] args) { //默认给出无参构造String(){}; String s = new String(); System.out.println(s);//输出不是地址说明String类本身就是重写Object类中的toString()方法 System.out.println("----------------"); byte[] bys = {97,98,99,100,101}; String s1 = new String(bys); System.out.println(s1); System.out.println("----------------"); String s2 = new String(bys,1,2); System.out.println(s2); System.out.println("----------------"); char[] ch = {'I',' ','L','O','V','E',' ','Y','O','U' }; String s3 = new String(ch); String s4 = new String(ch, 2, 4); System.out.println(s3); System.out.println(s4); System.out.println("----------------"); String s5 = new String("aloha"); System.out.println(s5); //上面的现在堆内存中创建了对象,然后String常量区中付入 //下面的直接将字符串常量去中取出,长期占用了String常量区 String s6 = "aloha"; System.out.println(s6); } }
String的成员方法
String类底层重写了equals方法,所以比较的是内容是否相同。
String类中常用的比较方法
boolean |
contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。 |
boolean |
contentEquals(CharSequence cs) 将此字符串与指定的 CharSequence 比较。 |
boolean |
contentEquals(StringBuffer sb) 将此字符串与指定的 StringBuffer 比较。 |
boolean |
endsWith(String suffix) 测试此字符串是否以指定的后缀结束。 |
boolean |
equals(Object anObject) 将此字符串与指定的对象比较。 |
boolean |
equalsIgnoreCase(String anotherString) 将此 String 与另一个 String 比较,不考虑大小写。 |
接口charSequence所有已知实现类:
- CharBuffer, Segment, String, StringBuffer, StringBuilder 。
- 方法测试例子:
public class Demo { public static void main(String[] args) { char[] chs = {'a','b','c','d'}; String str1 = "abc"; String str2 = "abc"; String str3 = "daf"; String str4 = "def"; String str5 = "DeF"; String str = "abcdef"; System.out.println(str.contains(str1)); System.out.println(str.contains(str3)); System.out.println("-----------------"); System.out.println(str.contentEquals(str1)); System.out.println(str1.contentEquals(str2)); System.out.println("endWith的测试:"); System.out.println(str.endsWith(str4)); System.out.println(str.endsWith(str3)); System.out.println("startsWith的测试:"); System.out.println(str.startsWith(str1)); System.out.println(str.startsWith(str4)); System.out.println(str.startsWith(str4, 3));//str从3号索引开始的str4是否是str的结尾:是 System.out.println("equals的测试:"); System.out.println(str1.equals(str2));//在String类中重写了equals的方法,比较的是数值而不再是地址值。 System.out.println(str4.equalsIgnoreCase(str5));//无视大小写比较字符串 System.out.println("isEmpty的测试:"); System.out.println(str3.isEmpty()); str3 = ""; System.out.println(str3.isEmpty()); // str3 = null ; //报错了Exception in thread "main" java.lang.NullPointerException // System.out.println(str3.isEmpty()); } }
String类的常用获取功能:
public int length():获取字符串的长度
public char charAt(int index)返回指定索引处的 字符
public int indexOf(int ch)返回指定字符在此字符串中第一次出现处的索引
问题:既然传入的字符:这里为什么用int类型
'a'和97 都表示a
public int indexOf(int ch,int fromIndex)返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
public int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引
public int indexOf(String str,int fromIndex)回在此字符串中第一次出现指定字符串处的索引,从指定的索引开始搜索。
public char charAt(int index)返回指定索引处的 字符
public int indexOf(int ch)返回指定字符在此字符串中第一次出现处的索引
问题:既然传入的字符:这里为什么用int类型
'a'和97 都表示a
public int indexOf(int ch,int fromIndex)返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
public int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引
public int indexOf(String str,int fromIndex)回在此字符串中第一次出现指定字符串处的索引,从指定的索引开始搜索。
截取功能
public String substring(int beginIndex):从指定位置开始截取,默认截取到末尾,返回新的字符串
public String substring(int beginIndex, int endIndex):从指定位置开始到指定位置末尾结束,包前不包含
public String subsequence(int beginIndex,int endIndex):作用同substring,只是实现了CharSequence的接口
截取功能的例子:
public class Demo2 { public static void main(String[] args) { String s = new String("abc123c123"); String s1 = new String("123"); /** * 获取功能 */ System.out.println("length = "+s.length());//获取长度 System.out.println("index = "+s.indexOf('1'));//获取指定值第一次出现处的索引 :3 System.out.println("index = "+s.indexOf(s1));//返回字符串s1在s中第一次出现的索引:3 System.out.println("index = "+s.indexOf('c', 3));//从指定索引3开始查找第一次出现'c'的索引值 System.out.println("index = "+s.indexOf(s1, 4));//从指定索引4开始查找第一次出现"abc"的索引值 System.out.println("charAt = "+s.charAt(2));//返回指定索引2处的字符'c' /** * 截取功能 */ System.out.println("substring =" + s.substring(7));//从7索引截取到末尾 System.out.println("substring =" +s.substring(7, 8));//从7索引开始截取不截到8 System.out.println("substring =" +s.subSequence(1,3));//作用同substring,只是能够实现 CharSequence 接口 } }
String的常用转换功能
public byte[] getBytes() :将字符串转换为字节数组
public char[] toCharArray() :将字符串转换成字符数组(重点)
public static String valueOf(int i):将int类型的数据转换成字符串(重点)
这个方法可以将任何类型的数据转化成String类型
public String toLowerCase():转成小写
public String toUpperCase():字符串中所有的字符变成大写
public char[] toCharArray() :将字符串转换成字符数组(重点)
public static String valueOf(int i):将int类型的数据转换成字符串(重点)
这个方法可以将任何类型的数据转化成String类型
public String toLowerCase():转成小写
public String toUpperCase():字符串中所有的字符变成大写
常用转换类型的例子:
public class Demo4 { public static void main(String[] args) { String s = new String("Aloha0501"); byte[] byteArr =s.getBytes(); System.out.println("将字符串转换成字节数组:"); for(int x =0 ;x <byteArr.length;x++) { System.out.print(byteArr[x]+" ");//打印的是byteArr数组的ascii值 } System.out.println(); char[] charArr = s.toCharArray(); System.out.println("将字符串转换成字符数组:"); for(int x = 0 ;x <charArr.length;x++) { System.out.print(charArr[x]+" "); } System.out.println(); System.out.println("将int类型的数据转换成字符串:");//public static String valueOf(int i) int num = 100; System.out.println("int:"+num); System.out.println("String:"+String.valueOf(num)); System.out.println("全部转换为小写的字符串:"); System.out.println(s.toLowerCase()); System.out.println("全部转换为大写字符串:"); System.out.println(s.toUpperCase()); } }将一个字符串的首字母大写,其他位置小写例子:
/** * 将字符串转换为首字母大写,其余小写 */ public class Demo5 { public static void main(String[] args) { String s = new String("HelloWorld!"); String s1 = s.toLowerCase(); String s2 = s1.substring(1); String s3 = s1.substring(0, 1); String s4 = s3.toUpperCase().concat(s2); System.out.println(s4); } }
String的其他功能(代替、除空、字典 比较)
public String replace(char oldChar,char newChar):将原字符串中的某个字符替换掉成新的字符
public String replace(String oldStr,String newStr):将大串中的某个子字符串替换掉
public String trim():去除字符串两端空格
public int compareTo(String anotherString):按字典顺序比较两个字符串
public String replace(String oldStr,String newStr):将大串中的某个子字符串替换掉
public String trim():去除字符串两端空格
public int compareTo(String anotherString):按字典顺序比较两个字符串
例子:
public class Demo6 { public static void main(String[] args) { String s = new String(" Hello World 123 "); String ss = new String(" Hello World 123"); String s1 = s.replace('l', 'L');//将s字符串中的所有l替换为L System.out.println(s1); System.out.println(s.replace("LL","ll"));//明明换的是"LL"-->"ll",为啥把单独的"L"也换了? System.out.println("去掉字符串两端空格:"); System.out.println(s.trim()); System.out.println("按字典顺序比较两个字符串:"); System.out.println(s.compareTo(ss)); } }