| The Java Language | CS 161 - Java |
"This is a String"
Tip: If you need to change a String's contents a lot, use a StringBuffer object.
is better thanpublic String toString() { StringBuffer result = new StringBuffer(); result.append( "Name:" ); result.append( name ); result.append( "\nGMU Id:" ); result.append( GMUid ); result.append( "\nemail:" ); result.append( email ); result.append( "\ndate:" ); result.append( now ); return result.toString(); }
The latter creates 8 String objects as a result of all the + operators.public String toString() { String result; result = "Name:" + name + "\nGMU Id:" + GMUid + "\nemail:" + email + "\ndate:" + now; return result; }
|
jwd |