item 56 Jung inchul - JAVA-JIKIMI/EFFECTIVE-JAVA3 GitHub Wiki
Effective Java 3e μμ΄ν 56λ₯Ό μμ½ν λ΄μ© μ λλ€.
μλ°λ μ μμ€μ½λ νμΌμμ λ¬Έμν μ£Όμμ΄λΌλ νΉμν ννλ‘ κΈ°μ λ μ€λͺ μ μΆλ € API λ¬Έμλ‘ λ³νν΄μ€λ€.
μλ° λ²μ μ λ°λΌ μλ°λ νκ·Έλ λ°μ ν΄μλ€.
- μλ° 5 β @literal, @code
- μλ° 6 β @implSpec
- μλ° 9 β @index
APIλ₯Ό μ¬λ°λ‘ λ¬Έμννλ €λ©΄ 곡κ°λ λͺ¨λ ν΄λμ€, μΈν°νμ΄μ€, λ©μλ, νλ μ μΈμ λ¬Έμν μ£Όμμ λ¬μμΌ νλ€. μ§λ ¬νν μ μλ ν΄λμ€λΌλ©΄ μ§λ ¬ν ννμ κ΄ν΄μλ μ μ΄μΌ νλ€.
...
* {@code AbstractQueuedLongSynchronizer}.
*
* <p>This class is Serializable, but all fields are transient,
* so deserialized conditions have no waiters.
*
* @since 1.6
*/
public class ConditionObject implements Condition, java.io.Serializable {κΈ°λ³Έ μμ±μμλ λ¬Έμν μ£Όμμ λ¬ λ°©λ²μ΄ μμΌλ κ³΅κ° ν΄λμ€λ μ λ κΈ°λ³Έ μμ±μλ₯Ό μ¬μ©νλ©΄ μλλ€.
public class Person(){
...
public Person(String name, int age){
this.name = name;
this.age = age;
}
}λ©μλμ© λ¬Έμν μ£Όμμλ ν΄λΉ λ©μλμ ν΄λΌμ΄μΈνΈ μ¬μ΄μ κ·μ½μ λͺ λ£νκ² κΈ°μ ν΄μΌ νλ€.
-
howκ° μλwhatμ κΈ°μ ν΄μΌ νλ€. - μ μ 쑰건(
precondition)μ λͺ¨λ λμ΄ν΄μΌ νλ€. - λ©μλκ° μ±κ³΅μ μΌλ‘ μνλ νμ λ§μ‘±ν΄μΌ νλ μ¬ν쑰건λ λͺ¨λ λμ΄ν΄μΌ νλ€.
- μ μ 쑰건μ
@throwsνκ·Έλ‘ λΉκ²μ¬ μμΈλ₯Ό μ μΈνμ¬ μμμ μΌλ‘ κΈ°μ νλ€. -
@paramνκ·Έλ₯Ό μ΄μ©ν΄ κ·Έ 쑰건μ μν₯ λ°λ λ§€κ°λ³μμ κΈ°μ ν΄μΌ νλ€.
κ΄λ‘μ @param νκ·Έμ @return νκ·Έμ μ€λͺ
μ ν΄λΉ λ§€κ°λ³μκ° λ»νλ κ°μ΄λ λ°νκ°μ μ€λͺ
νλ λͺ
μ¬κ΅¬λ₯Ό μ΄λ€. λλ¬Όκ²λ λͺ
μ¬κ΅¬ λμ μ°μ ννμμ μ°κΈ°λ νλ€. BigIntegerμ API λ¬Έμλ₯Ό μ°Έκ³ ν΄λ³΄λ©΄ λλ€.
/**
* Returns a BigInteger whose value is {@code (this * val)}.
*
* @implNote An implementation may offer better algorithmic
* performance when {@code val == this}.
*
* @param val value to be multiplied by this BigInteger.
* @return {@code this * val}
*/
public BigInteger multiply(BigInteger val) {
return multiply(val, false);
}@throws μ μ μ¬μ©ν {@code} νκ·Έλ λ κ°μ§ ν¨κ³Όλ₯Ό κ°μ§λ€.
- νκ·Έλ‘ κ°μΌ λ΄μ©μ
μ½λμ© ν°νΈλ‘ λ λλ§νλ€. - νκ·Έλ‘ κ°μΌ λ΄μ©μ ν¬ν¨λ HTML μμλ λ€λ₯Έ μλ°λ
νκ·Έλ₯Όλ¬΄μνλ€.
ν΄λμ€λ₯Ό μμμ©μΌλ‘ μ€κ³ν λλ μκΈ°μ¬μ© ν¨ν΄(self-use patten)μ λν΄μλ λ¬Έμμ λ¨κ²¨ λ€λ₯Έ νλ‘κ·Έλλ¨Έμκ² κ·Έ λ©μλλ₯Ό μ¬λ°λ‘ μ¬μ μνλ λ°©λ²μ μλ €μ€μΌ νλ€. μκΈ°μ¬μ© ν¨ν΄μ μλ° 8μ μΆκ°λ @implSpec νκ·Έλ‘ λ¬Έμννλ€.
@implSpec μ£Όμμ ν΄λΉ λ©μλμ νμ ν΄λμ€ μ¬μ΄μ κ³μ½μ μ€λͺ
νμ¬, νμ ν΄λμ€λ€μ΄ κ·Έ λ©μλλ₯Ό μμνκ±°λ super ν€μλλ₯Ό μ΄μ©ν΄ νΈμΆν λ κ·Έ λ©μλκ° μ΄λ»κ² λμ νλμ§λ₯Ό λͺ
νν μΈμ§νκ³ μ¬μ©νλλ‘ ν΄μ€μΌ νλ€.
/**
* Returns a sequential {@code Stream} with this collection as its source.
*
* <p>This method should be overridden when the {@link #spliterator()}
* method cannot return a spliterator that is {@code IMMUTABLE},
* {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
* for details.)
*
* @implSpec
* The default implementation creates a sequential {@code Stream} from the
* collection's {@code Spliterator}.
*
* @return a sequential {@code Stream} over the elements in this collection
* @since 1.8
*/
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}API μ€λͺ
μ HTML λ©νλ¬Έμλ₯Ό ν¬ν¨μν€λ €λ©΄ @literal νκ·Έλ‘ κ°μΈλ©΄ λλ€. **@code νκ·Έμ λΉμ·νμ§λ§ μ½λ ν°νΈλ‘ λ λλ§νμ§λ μλλ€.**
/**
* A suspect, such as Colonel Mustard or {@literal Mrs. Peacock}.
*/
public class Suspect { ... }μλ° 10λΆν°λ {@summary}λΌλ μμ½ μ€λͺ μ μ© νκ·Έκ° μΆκ°λμ΄, νκ²° κΉλνκ² μ²λ¦¬ν μ μλ€.
/**
*{@summary A suspect, such as Colonel Mustard or @literal Mrs. Peacock.}
*/
public class Suspect { ... }λ©μλμ μμ±μμ μμ½ μ€λͺ
μ ν΄λΉ λ©μλμ μμ±μμ λμμ μ€λͺ
νλ (μ£Όμ΄κ° μλ) λμ¬κ΅¬μ¬μΌ νλ€.
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}ν΄λμ€, μΈν°νμ΄μ€, νλμ μμ½ μ€λͺ
μ λμμ μ€λͺ
νλ λͺ
μ¬μ μ΄μ΄μΌ νλ€.
/**
* The {@code double} value that is closer than any other to
* <i>pi</i>, the ratio of the circumference of a circle to its
* diameter.
*/
public static final double PI = 3.14159265358979323846;μλ° 9λΆν°λ μλ°λ
μ΄ μμ±ν HTML λ¬Έμμ κ²μ(μμΈ) κΈ°λ₯μ΄ μΆκ°λμ΄ κ΄λν API λ¬Έμλ€μ νκ²° μμν΄μ‘λ€.
* This method complies with the {@index IEEE 754} standard.μ λ€λ¦ νμ
μ΄λ μ λ€λ¦ λ©μλλ₯Ό λ¬Έμνν λλ λͺ¨λ νμ
λ§€κ°λ³μμ μ£Όμμ λ¬μμΌ νλ€.
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
*
* @author Josh Bloch
* @see HashMap
* @see TreeMap
* @see Hashtable
* @see SortedMap
* @see Collection
* @see Set
* @since 1.2
*/
public interface Map<K, V> { ... }μ΄κ±° νμ
μ λ¬Έμνν λλ μμλ€μλ μ£Όμμ λ¬μμΌ νλ€.
μ λν μ΄μ νμ μ λ¬Έμνν λλ λ©€λ²λ€μλ λͺ¨λ μ£Όμμ λ¬μμΌ νλ€.
/**
* An instrument section of a symphony orchestra.
*/
public enum OrchestraSection {
/** Woodwinds, such as flute, clarinet, and oboe. */
WOODWIND,
/** Brass instruments, such as french horn and trumpet */
BRASS,
/** Percussion instruments, such as timpani and cymbals. */
PERCUSSION
}API λ¬Έμνμμ μμ£Ό λλ½λλ μ€λͺ
μ΄ λ κ°μ§ μμΌλ, λ°λ‘ μ€λ λ μμ μ±κ³Ό μ§λ ¬ν κ°λ₯μ±μ΄λ€. ν΄λμ€ νΉμ μ μ λ©μλκ° μ€λ λ μμ νλ κ·Έλ μ§ μλ , μ€λ λ μμ μμ€μ λ°λμ API μ€λͺ
μ ν¬ν¨ν΄μΌ νλ€.
μλ°λ
μ λ©μλ μ£Όμμ 'μμ'μν¬ μ μλ€. λν {@InhertitDocs} νκ·Έλ₯Ό μ¬μ©ν΄ μμ νμ
μ λ¬Έμν μ£Όμμ μ¬μ¬μ©ν μ μλ€λ λ»μ΄λ€.
public abstract class AbstractMemberBuilder extends AbstractBuilder {
/**
* Returns true if this subbuilder has anything to document.
*
* @return true if this subbuilder has anything to document
*/
public abstract boolean hasMembersToDocument();
...
}public class MethodBuilder extends AbstractMemberBuilder {
/**
* {@inheritDoc}
*/
@Override
public boolean hasMembersToDocument() {
return !methods.isEmpty();
}
}