Java コ コ ディ グ ガ ガ ド ラ イ ン
1: Private, PUBLIC is appropriate.
2: The difference between the original and reference type:
1 When the original type (BYTE, Boolen et al.) Do not assign memory space, so speed and memory usage are better than reference.
2 The default value of the original type is based on the type, and the referred to the default value is NULL.
3 When the original type is stored, his value is saved, and the imperative reference is his address, so the meaning of the two programs below is different.
INT i = 5;
INT j = i;
Since the value is saved, the change in I will not affect J
Integer k = new integer (5);
IntedEt L = K;
Saved is address, and the change in K will affect L
4 So pay special attention when the function is passed when the parameter is passed.
Import java.awt.point;
Class Badsample {
Void ModifyValue (int value, point point) {
Value = 10;
Point.setlocation (55, 77);
}
Void PrintValue (int value, point point) {
StringBuffer buffer = new stringbuffer ();
Buffer.Append (value);
Buffer.append (":");
Buffer.Append (Point);
System.out.println (buffer);
}
Public static void main (String [] args) {
INT INTVALUE = 0;
Point Point = New Point (0, 0);
Badsample Sample = New Badsample ();
Sample.PrintValue (intValue, point);
Sample.ModifyValue (INTVALUE, POIN);
Sample.PrintValue (intValue, point);
}
}
[実行 结果]
0: java.awt.point [x = 0, y = 0]
0: java.awt.point [x = 55, y = 77]
It can be seen that the change to Value in ModifyValue does not change the value of IntValue in Main. For the transfer of the original type parameters, it can be seen that the transfer is a copy, the change in the function of the reference type parameter affects his value.
3: Try to use local variables
Accessing local variables is more saving more than access class variables.
Class Badsample {
Private int result;
Void Sum (int [] value) {
For (int i = 0; i Result = values [i]; // violates } } } Class fixedsample { Private int result; Void Sum (int [] value) { int sum = result; For (int i = 0; i SUM = VALUES [I]; } Result = SUM; } } 4: Compare the equality of Object with equals (). Special pay attention to String, I have been committed, it is hard to find out. 5: The variables that are not used are negative NULL to avoid waste space. Class Badsample { Public static void main (String [] args) { Testclass test = new testclass (); String dimstring = "first"; Test.method (firststring); · · · · · · String secondstring = "second"; Test.method (SecondString); · · · · · · String thirdstring = "third"; Test.method (thirdstring); · · · / / Violate } } Class fixedsample { Public static void main (String [] args) { Testclass test = new testclass (); String dimstring = "first"; Test.method (firststring); · · · · · · Firststring = NULL; String secondstring = "second"; Test.method (SecondString); · · · · · · Secondstring = NULL; String thirdstring = "third"; Test.method (thirdstring); · · · · · · Thirdstring = null; // Correction 済み } } 4: Use occasions for constants Class fixedsample { Private static final int arch_size = 1000; int [] getArray () { Return new int [array_size]; // correction 済み } Private static final string start = "start"; System.out.println (Start); Public static firm string line_separator = System.getProperty ("line.separator"); : System.out.println ("Change" line_separator); } 5: At the beginning of the initialization of the variable. 6: When the List is loop, use Iterator with high efficiency. Public void doxxx (List L) { Iterator I = L.ITerator (); I.hasNext ();) { User u = (user) i.next (); } } 7: Do not change the cycle in the for loop. Public class badsample { INT BadsampleMethod () { INT SUM = 0; For (int i = 0; i <100; i ) { i = 3; // violate SUM = I; } } } Public class fixedsample { INT fixedsampleMethod () { INT SUM = 0; // Correct 済み For (int i = 0; i <100; i = 4) { SUM = I; } Return SUM; } } 8: If an instance variable does not change its value in the program, try to define it as final, because Final is synchronized, the compilation efficiency is relatively high. 9: Try not to NEW in the cycle, you will keep the resource. Public class badsample { Public void badsampleMethod (int max) { For (int i = 0; i StringBuffer SampleBuffer = NEW stringbuffer (); // violate SampleBuffer.Append ("loop:"); SampleBuffer.Append (i); System.out.println ( SampleBuffer.toString ()); } } } Public class fixedsample { Public void fixedsampleMethod (int max) { StringBuffer SampleBuffer = NEW stringbuffer (); // Correct 済み For (int i = 0; i SampleBuffer.Append ("loop:"); SampleBuffer.Append (i); System.out.println ( SampleBuffer.toString ()); Samplebuffer.setlength (0); // Early state に に } } } 10: Equals () and havehcode () are overwritten at the same time Public final class idnumber { Private final int id; Public IDNUMBER (INT ID) { THIS.ID = ID; } Public Boolean Equals (Object Object) { Boolean ISEQUAL = FALSE; IF (Object == this) { ISEQUAL = TRUE; } else if (Object InstanceOf IDNumber) { IDNUMBER IDNUM = (IDNUMBER) Object; IF (IDNum.ID == this.id) {// id の 値 が が けれ ば True す ISEQUAL = TRUE; } } Return ISEqual; } } Public Int hashcode () { Int result = 17; Result = 37 * result id; // ID attribute し し る で, じ ば ば ば じ が が る る Return Result; } 11: Call a CLASS Clone method, which must implement the Cloneable class. When you override the Clone method, you must adjust Super's Clone method. 12: When you override the Finalize method, you must adjust the Finalize method of Superze. 13: Arrse copy System.ArrayCopy (). Public class fixedsample { Int [] COPYARRAY (int [] array) { INT length = array.length; int [] copy = new int [length]; System.ArrayCopy (Array, 0, Copy, 0, Length); // Correction Return Copy; } } 14: Since Interface can achieve multiple inheritance, the abstract class is not available, so it is recommended to use Interface as much as possible. 15: Define a default constructor so that you can generate an object with class.newinstance (). 16: Avoid IF (Method () == true); directly write IF (Method ()) is relatively high. 17: Try not to change or return parameters within the method, as it may change the value of the external parameter itself, it is best to return to an OBJ in the method. 18: Overloading the ToString method as much as possible, you can make the program's debugging more convenient. 19: The method you don't want to be covered into final, so others can't cover it.