- ν΄λμ€μ μΈμ€ν΄μ€ κ°μ μμ ν μ μλ ν΄λμ€
- String, Wrapper Class, BigInteger, BigDecimal
- μ€κ³, ꡬν, μ¬μ©μ΄ μ½λ€.
- μ€λ₯μμ κ°λ³ ν΄λμ€λ³΄λ€ ν¨μ¬ μμ νλ€.
- κ°μ²΄μ μνλ₯Ό λ³κ²½νλ λ©μλλ₯Ό μ 곡νμ§ μμ. (μ. setter)
- ν΄λμ€λ₯Ό νμ₯ν μ μλλ‘ ν¨.
- νμ ν΄λμ€μμ κ°μ²΄κ° λ³κ²½λ μ μλ€.
- λνμ μΈ λ°©λ²μ final ν€μλ
- λͺ¨λ νλλ₯Ό final λ‘ μ μΈ.
- μ¬λ¬ μ€λ λμμ μ κ·Όν΄λ κ°μ΄ λ°λμ§ μλλ€.
- λ€μ€ μ€λ λ νκ²½μμλ μμ νλ€.
- λͺ¨λ νλλ₯Ό private λ‘ μ μΈ.
- μμ μΈμλ λ΄λΆμ κ°λ³ μ»΄ν¬λνΈμ μ κ·Όν μ μλλ‘ ν¨.
- ν΄λμ€μ κ°λ³ μ°Έμ‘° κ°μ²΄ νλμ λν νλͺ©.
- ν΄λΌμ΄μΈνΈμμ μ΄ νλμ μ°Έμ‘°λ₯Ό μ»μΌλ©΄ μλλ€.
- μλ²μμλ μ κ·Όμ λ©μλλ‘ μ΄ νλκ°μ λ°ννλ©΄ μλλ€.
- μμ±μ, μ κ·Όμ, readObject λ©μλ λͺ¨λμμ λ°©μ΄μ 볡μ¬λ₯Ό μννλ€.
public final class Money {
private final String currency;
private final int amount;
private final String[] cards;
public Money(String currency, int amount) {
this.currency = currency;
this.amount = amount;
}
public Money plus(int additionMoney) {
return new Money(this.name, this.amount + additionMoney);
}
public String[] getCards() {
return cards.clone();
}
}
-
첫λ²μ§Έ νλͺ©
- ν΄λμ€μ νλ currency, amount λ₯Ό λ°κΏ μ μλ λ©μλκ° μμ.
- λμ΄λ₯Ό λ리λ λ©μλλ μλ‘μ΄ Money κ°μ²΄λ₯Ό λ°ννλ€.
ν¨μν νλ‘κ·Έλλ° : μλ£ μ²λ¦¬λ₯Ό μνμ ν¨μμ κ³μ°μΌλ‘ μ·¨κΈνκ³ μνμ κ°λ³ λ°μ΄ν°λ₯Ό λ©λ¦¬νλ νλ‘κ·Έλλ° ν¨λ¬λ€μμ νλ μμ ν¨μ : λΆμμ©μ΄ μλ ν¨μ λΆμμ©(side-effect) : ν¨μμ μ€νμ΄ μΈλΆμ μν₯μ λ―ΈμΉμ§ μκ³ , μΈλΆμ μν΄ ν¨μμ μ€νμ΄ μν₯λ°μ§ μλ ν¨μ. λμ΄λ₯Ό λ릴 λ, ν΄λΉ κ°μ²΄μ κ°(μμ±)μ λ³κ²½νλ€λ©΄ κ·Έ κ°μ²΄λ₯Ό μ¬μ©νλ λ€λ₯Έ κ°μ²΄λ€μ΄ μν₯μ λ°λλ€. λ°λΌμ μλ‘μ΄ κ°μ²΄λ₯Ό λ§λ€μ΄ λ°νν¨μΌλ‘μ¨ side-effect λ₯Ό μ€μΌ μ μλ€.
μλ‘μ΄ κ°μ²΄λ₯Ό λ°ννλ λ©μλλ λͺ λͺ κ·μΉμΌλ‘ μ μΉμ¬λ₯Ό μ¬μ©. (add λ§κ³ plus) Integer, Double κ°μ ν΄λμ€λ€μ valueOf()
-
λλ²μ§Έ νλͺ©
- ν΄λμ€λ₯Ό final λ‘ μ§μ νμ¬ νμ₯μ΄ λΆκ°λ₯νκ² ν¨.
-
μΈλ²μ§Έ νλͺ©
- λͺ¨λ νλλ₯Ό final λ‘ μ μΈ.
-
λ€λ²μ§Έ νλͺ©
- λͺ¨λ νλλ₯Ό private μΌλ‘ μ μΈ.
-
λ€μ―λ²μ§Έ νλͺ©
- κ°λ³ μ°Έμ‘° κ°μ²΄ cards λ final λ‘ μ§μ νλλΌλ μμ κ°μ λν΄μλ λΆλ³μ΄ μλ.
- κ°λ³ κ°μ²΄ cards λ₯Ό μ»μ΄μ€λ νλλ clone() μ μ΄μ©.
-
λ¨μνλ€.
- μμ±λ μμ μ μνκ° νκ΄΄λ λκΉμ§ κ°μ§
-
λΆλ³ κ°μ²΄λ κ·Όλ³Έμ μΌλ‘ μ€λ λ μμ νμ¬ λ°λ‘ λκΈ°ν ν νμ μλ€.
- Thread-safe ν ν΄λμ€λ₯Ό λ§λλ κ°μ₯ μ¬μ΄ λ°©λ².
- λΆλ³ ν΄λμ€λ νλ² λ§λ μΈμ€ν΄μ€λ₯Ό μ΅λν νμ©νλ€. (μ. public static final μ)
- λ°λΌμ λΆλ³ κ°μ²΄λ μμ¬νκ³ κ³΅μ ν μ μλ€.
java.math.BigDecimal public static BigDecimal valueOf(long val) { if (val >= 0 && val < ZERO_THROUGH_TEN.length) return ZERO_THROUGH_TEN[(int)val]; else if (val != INFLATED) return new BigDecimal(null, val, 0, 0); return new BigDecimal(INFLATED_BIGINT, val, 0, 0); } private static final BigDecimal ZERO_THROUGH_TEN[] = { new BigDecimal(BigInteger.ZERO, 0, 0, 1), new BigDecimal(BigInteger.ONE, 1, 0, 1), new BigDecimal(BigInteger.TWO, 2, 0, 1), new BigDecimal(BigInteger.valueOf(3), 3, 0, 1), new BigDecimal(BigInteger.valueOf(4), 4, 0, 1), new BigDecimal(BigInteger.valueOf(5), 5, 0, 1), new BigDecimal(BigInteger.valueOf(6), 6, 0, 1), new BigDecimal(BigInteger.valueOf(7), 7, 0, 1), new BigDecimal(BigInteger.valueOf(8), 8, 0, 1), new BigDecimal(BigInteger.valueOf(9), 9, 0, 1), new BigDecimal(BigInteger.TEN, 10, 0, 2), };
@Before public void μ€μ () { tenUnder1 = BigDecimal.valueOf(9); tenUnder2 = BigDecimal.valueOf(9); tenOver1 = BigDecimal.valueOf(12); tenOver2 = BigDecimal.valueOf(12); @Test public void BigDecimalμμ_10μ΄νλ_μ μ ν©ν°λ¦¬μ¬μ©() { Assert.assertSame(tenUnder1, tenUnder2); Assert.assertEquals(System.identityHashCode(tenUnder1), System.identityHashCode(tenUnder2)); @Test public void BigDecimalμμ_10μ΄κ³Όλ_μλ‘μ΄κ°μ²΄μμ±() { Assert.assertNotSame(tenOver1, tenOver2); Assert.assertNotEquals(System.identityHashCode(tenOver1), System.identityHashCode(tenOver2)); }
-
λΆλ³ κ°μ²΄λ κ°μ²΄μ νλλ‘ μ°λ©΄ μ’λ€.
- λΆλ³μμ 보μ₯ν μ μλ€.
- Map μ ν€λ Set μ μμλ‘ λΆλ³κ°μ²΄λ₯Ό μ¬μ©νλ©΄ λΆλ³μμ μ§ν€κΈ° μ½λ€.
-
λΆλ³ κ°μ²΄λ μ€ν¨ μμμ±μ μ 곡νλ€.
- μ€ν¨ μμμ± : λ©μλ μν μ€ μμΈκ° λ°μν΄λ κ°μ²΄λ μνλ₯Ό μ μ§νλ€. (item76)
- κ°μ΄ λ€λ₯΄λ©΄ 무쑰건 λ
립λ κ°μ²΄λ₯Ό μμ±ν΄μΌ ν¨.
- μνλ κ°μ²΄λ₯Ό λ§λ€κΈ° κΉμ§ μ¬λ¬ λ¨κ³λ₯Ό κ±°μ³μΌ νλ€λ©΄, μΈλͺ¨μλ κ°μ²΄κ° λ§μ΄ μκΈ΄λ€.
- μ λ¬Έμ μ ν΄κ²°μ μΌλ‘ λ€λ¨κ³ μ°μ°μ μ νλ κ°λ³ λλ° ν΄λμ€ λ₯Ό μμ±νλ€.
κ°λ³ λλ° ν΄λμ€ : 볡μ‘ν λ€λ¨κ³ μ°μ°μ κΈ°λ³ΈμΌλ‘ μ 곡 ν΄μ£Όλ ν΄λμ€ BigInteger μλ package-private μΌλ‘ μ¬λ¬ ν΄λμ€ μ‘΄μ¬ String μλ public μΌλ‘ μ 곡λλ StringBuilder, StringBuffer μ‘΄μ¬
-
λλ²μ§Έ κ·μΉ. νμ₯ν μ μλλ‘ ν¨
- λͺ¨λ μμ±μλ₯Ό private, package-private μΌλ‘ λ§λ€κ³ , μ μ ν©ν°λ¦¬ λ©μλ μ 곡.
-
첫, μΈλ²μ§Έ κ·μΉ. λͺ¨λ νλλ final μ΄κ³ λ³κ²½μ λ©μλκ° μμ
- μ΄λ€ λΆλ³ ν΄λμ€λ κ³μ° λΉμ©μ΄ ν° κ°μ final μ΄ μλ νλμ μΊμ±νμ¬ μ¬μ©νλ€.
public final class String implements java.io.Serializable, Comparable<String>, CharSequence { @Stable private final byte[] value; private final byte coder; private int hash; // final μ΄ μλ ν private static final long serialVersionUID = -6849794470754667710L; ... public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { hash = h = isLatin1() ? StringLatin1.hashCode(value) : StringUTF16.hashCode(value); } return h; }
- λ°λΌμ μμ κ·μΉμ λ€μκ³Ό κ°μ΄ μνν μ μλ€.
μ΄λ€ λ©μλλ κ°μ²΄μ μν μ€ μΈλΆμ λΉμΉλ κ°μ λ³κ²½ν μ μλ€.
- λλΆλΆμ κ²½μ° λΆλ³ ν΄λμ€λ‘ λ§λ€μ΄μΌ νλ€. : setter μ°μ§ λ§ κ².
- μ±λ₯ μ νλ‘ λΆλ³ ν΄λμ€ κ΅¬νμ΄ νλ€λ©΄ κ°λ³ λλ° ν΄λμ€λ₯Ό μ΄μ©.
- λΆλ³ ν΄λμ€ κ΅¬νμ΄ λΆκ°νλ©΄ λ³κ²½ κ°λ₯ λΆλΆμ μ΅μνμΌλ‘ μ€μ.
- λͺ¨λ νλλ private final μ΄μ΄μΌ νλ€.
- κ°μ²΄μ μνλ₯Ό μ΄κΈ°ν νλ λ©μλλ μμ±μ, μ μ ν©ν°λ¦¬ λ©μλ μ΄μΈμλ μμ΄μΌ ν¨.