<Java SE 详解> Object类与String类-
1、相等性的比较:
1> 对于原生数据类型来说,比较的是左右两边的值是否相等,例如boolean, int, char;
2> 对于引用类型数据来说,比较的是左右两边的引用是否指向同一个对象,或者说左右两边的引用地址值是否相同;
例:
public class InstanceTest
{
public static void main(String[] args)
{
People p = new Man();
Man m = (Man)p;
System.out.println(p == m); //print true
}
}
class People
{
}
class Man extends People
{
}
2、java.lang.Object类:java.lang包在使用的时候无需显式导入,编译时由编译器自动帮助我们导入。
当打印引用的时候,System.out.println()实际上会打印出引用所指向的对象的toString()方法的返回值,因为每个类都直接或间接地继承自Object,而Object类中定义了toString(),因此每个类都会有toString()这个方法。
3、Object类中equals()方法定义在Object类当中,因此Java中的每个类都一定会具有该方法。
Object类中equals()方法实现,如下JDK中源代码:
public boolean equals(Object obj) {
return (this == obj);
}
原Object类中的equals方法是比较两个Object对象的地址是否相等。对于Object类的equals()方法来说,它等价于“==”符号。
4、据统计,平均在一个项目中60%的部分都需要使用到String类,因为用户的输入大多数都是字符串,因此String类是非常常用的一个类。
5、对于String类的equals()方法来说,它是判断当前字符串与传递进来的字符串的内容是否一致。
6、对于String对象的相等性判断来说,请使用equals方法,而不要使用==,因为==比较的是两个字符串对象的地址是同一个地址,而不是比较字符串中的内容。
例: 二十九-30:00
package cn.edu.bupt.common;
public class EqualsTest
{
public static void main(String[] args)
{
Student st1 = new Student("zhangsan");
Student st2 = new Student("zhangsan");
System.out.println(st1.equals(st1));
System.out.println(st1 == st2);
}
}
class Student
{
private String name;
public Student(String name)
{
this.name = name;
}
@Override
public boolean equals(Object obj)
{
if(super.equals(obj))
{
return true;
}
if(obj instanceof Student)
{
Student anotherStudent = (Student)obj;
return this.name.equals(anotherStudent.name);
}
return false;
}
}
7、String对象是常量,其对象一旦创建完毕就无法改变。当使用 “+” 拼接字符串时,会生成新的String对象,而不是向原有的String对象中追加字符串内容。
8、JVM维护一个String Pool (字符串池,分配与栈中)东东。当使用字面量形式直接给字符串变量赋值时,实际上完成两件事情: 三十-10:00
1> 首先检查字符串池中有无内容为 字符串字面量 的这样一个字符串对象,如果没有则将该字符串字面量对象放入字符串池中,字符串引用再指向该字符串对象;
2> 如果已经在字符串池中存在目标字面量对象,则不会创建新的字符串字面量对象,而是将当前字符串池中的字符串字面量对象的引用返回。
例:String s = "aaa";
查找StringPool中是否存在"aaa"这个对象,如果不存在,则在StringPool中创建一个“aaa”对象,然后将String Pool中的这个“aaa”对象的引用地址返回赋给String引用变量s,这样s会指向StringPool中的这个“aaa”;如果存在,则不创建新的对象,直接将String Pool中的这个“aaa”对象地址返回来,赋给s引用。
9、String s = new String("aaa"); 三十-19:00
new 一定会创建新的对象,存放于堆中。
整个过程为:当使用new创建新的“aaa”字符串对象的时候,首先检查StringPool中是否存在“aaa”字符串,如果存在,不在StringPool中去创建“aaa”这个对象了,而是在堆中创建“aaa”字符串对象,并将这个堆中的字符串对象引用返回;如果不存在,则先在StringPool中创建一个值为“aaa”的字符串对象,然后再于堆中创建一个“aaa”字符串对象,并将堆中的字符串对象引用返回。不管哪种情况,最后s均指向堆中创建的字符串对象;于此同时,StringPool中和堆中均存在值为“aaa”的对象。
10、Java维护这样一个字符串池的目的是因为效率问题,因为String在日常开发中是最常用的类型,主要是基于String是一个常量基础。
11、方法String.intern()
该方法返回的一定是在StringPool(字符串池)中的字符串对象引用。
见JDK帮助文档,一下是摘自其中:
“A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true. ”
12、java.lang.StringBuffer类 三十一-12:00
StringBuffer与String不同之处是,StringBuffer是一个变量类型,而String是一个常量类型。StringBuffer在生成之后可以对其中的字符串进行修改。
StringBuffer对于线程是安全的,对于StringBuffer类,它提供了很多的重载的append方法,方便向现有的字符串末尾追加相关对象内容。
例如:
package cn.bupt.edu.string;
public class StringTest
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();
sb.append("hello").append(" world").append(new Integer(12));
System.out.println(sb.toString());
}
}
append方法返回的是当前所操作StringBuffer对象的引用,而不是返回一个新的StringBuffer对象。
对于String或StringBuffer类型对象进行拼接操作的时候,如果拼接的变量类型是非字符串类型,这样在进行拼接操作之前首先需要将这些非字符串变量转换成为字符串类型,然后在进行拼接操作。
如:
package cn.bupt.edu.string;
public class StringTest
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();
sb.append("hello").append(false).append(new Integer(12));
System.out.println(sb.toString());
String s = "abc";
int h = 100;
boolean b = false;
String str = s + h + b;
System.out.println(str);
}
}
13、对于使用 “+” 拼接字符串的问题 三十一-25:00
见下例:
package cn.bupt.edu.string;
public class StringTest
{
public static void main(String[] args)
{
int m = 100;
int n = 200;
String s = "abc";
System.out.println(m + n + s); //print 300abc
System.out.println(s + m + n); //print abc100200
System.out.println("100" + n); //print 100200
System.out.println(m + n); //print 300
}
}
一般 "+" 用于数值运算,或是字符串拼接,不能用在其他地方。
另外: Java中没有运算符重载的机制
浙公网安备 33010602011771号