关于序列化:
序列化的是对象,自然是保存对象的数据,
序列化的过程中只有属性可以被序列化,方法不可以。同时一旦属性被transient或者static修饰。属性不可序列化。
声明为static和transient类型的成员数据不能被串行化。因为static代表类的状态, transient代表对象的临时数据。
String/StringBuffer/StringBuilder类是对字符串的抽象?
String类是一个不可变类,实例创建后不可改变
StringBuffer是一个可变类,实力创建后允许改变,是线程安全的,性能略低
可以将StringBuffer的实例转换成Sting的实例
StringBuilder是一个可变类,实力创建后允许改变,不是线程安全的,性能略高
String/StringBuffer/StringBuilder都实现了CharSequence接口
CharSequence接口源码?
public interface CharSequence {
int length();
char charAt(int index);
public String toString();
}
String类源码?
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
private final char value[];//决定String是一个不可变类
private static final long serialVersionUID = -6849794470754667710L;
private int hash; // Default to 0
public String() {//构造方法
this.value = "".value;
}
public String(String original) {//构造方法
this.value = original.value;
this.hash = original.hash;
}
public String(char value[]) {//构造方法
this.value = Arrays.copyOf(value, value.length);
}
public String(StringBuffer buffer) {//构造方法
synchronized(buffer) {
this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
}
}
public String(StringBuilder builder) {//构造方法
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
public int length() {
return value.length;
}
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
public boolean equals(Object anObject) {//重写父类Object方法
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
public int hashCode() {//重写父类Object方法
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
补充一个刚刚用过的String类的方法format,利用这个方法我们可以实现小数位数的格式化,并且四舍五进
System.out.println(total/z);
String string=String.format("%.2f",total/z);//保留两位小数,并且四舍五入格式化
System.out.println(string);
因为String类是不可变的,因此会额外产生许多临时的String实例,因此使用StringBuffer/StringBuilder来代替
这些提供了许多改变字符序列的方法
append()/reverse()/insert()/delete()/replace()
StringBuffer源码?线程安全
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
private transient char[] toStringCache;
static final long serialVersionUID = 3388685877147921107L;
public StringBuffer() {
super(16);
}
public StringBuffer(int capacity) {
super(capacity);
}
public StringBuffer(String str) {
super(str.length() + 16);
append(str);
}
public synchronized StringBuffer append(StringBuffer sb) {
toStringCache = null;
super.append(sb);
return this;
}
public synchronized StringBuffer delete(int start, int end) {
toStringCache = null;
super.delete(start, end);
return this;
}
public synchronized StringBuffer insert(int index, char[] str, int offset,
int len)
{
toStringCache = null;
super.insert(index, str, offset, len);
return this;
}
public synchronized StringBuffer reverse() {
toStringCache = null;
super.reverse();
return this;
}
}
StringBuilder源码?线程不安全
ublic final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
static final long serialVersionUID = 4383685877147921099L;
public StringBuilder() {
super(16);
}
public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}
public StringBuilder append(char c) {
super.append(c);
return this;
}
public StringBuilder insert(int index, char[] str, int offset,
int len)
{
super.insert(index, str, offset, len);
return this;
}
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}
}