🐯[TIL] 250604-004

byoΒ·2025λ…„ 6μ›” 9일

πŸ’« JAVA


βœ”οΈ μ—°μ‚°μž

βœ… 논리 μ—°μ‚°μž

논리 μ—°μ‚°μžλŠ” 쑰건과 쑰건 μ‚¬μ΄μ˜ 닀리 역할을 ν•˜λ©°,
λ³΅μž‘ν•œ 쑰건을 λͺ…ν™•ν•˜κ²Œ ν‘œν˜„ν•  수 있게 도와쀀닀.

μ’…λ₯˜μ΄λ¦„μ„€λͺ…
&&논리 AND두 쑰건이 λͺ¨λ‘ true일 λ•Œλ§Œ κ²°κ³Όκ°€ true
||논리 ORν•˜λ‚˜λΌλ„ true이면 κ²°κ³Όκ°€ true
!논리 NOT쑰건의 값을 λ°˜λŒ€λ‘œ λ’€μ§‘μŒ

결과값은 Booleanκ°’μœΌλ‘œ λ°˜ν™˜

πŸ’‘ &, |도 μ‚¬μš©ν•  수 μžˆλŠ”λ°,

int A = 2;
if(A > 0 && A > 1){
	System.out.println("μ°Έ");
}
if(A > 0 & A > 1){
	System.out.println("μ°Έ");
}

λ‹€λ₯Έ 점은 ( &, | )λŠ” ( &&, || )와 달리 이미 μ‘°κ±΄μ‹μ˜ κ²°κ³Όκ°€ 결정이 났닀고 해도 λͺ¨λ“  쑰건을 λκΉŒμ§€ κ²€μ‚¬ν•œλ‹€.

βœ… 비ꡐ μ—°μ‚°μž

비ꡐ μ—°μ‚°μžλŠ” 쑰건 νŒλ‹¨μ˜ 핡심 도ꡬ이며,
μ°Έ/κ±°μ§“μ˜ κ²°κ³Όλ₯Ό 톡해 ν”„λ‘œκ·Έλž¨ 흐름을 μ œμ–΄ν•œλ‹€.

μ—°μ‚°μžμ˜λ―Έμ£Όλ‘œ μ“°μ΄λŠ” 상황 μ˜ˆμ‹œ
==κ°™λ‹€λΉ„λ°€λ²ˆν˜Έ 일치 확인
!=κ°™μ§€ μ•Šλ‹€μž˜λͺ»λœ μž…λ ₯ 탐지
>, <크기 λΉ„κ΅λ‚˜μ΄, μ˜¨λ„, μˆ˜λŸ‰ λ“± 숫자 νŒλ‹¨
>=, <=λ²”μœ„ λΉ„κ΅μ΅œμ†Œ/μ΅œλŒ€ 쑰건

βœ”οΈ 반볡문


βœ… for

β€œfor 문은 반볡이 ν•„μš”ν•œ λͺ¨λ“  μƒν™©μ—μ„œ
κ°€μž₯ λ¨Όμ € λ– μ˜¬λ €μ•Ό ν•  κ°•λ ₯ν•œ 도ꡬ닀.

핡심 κ°œλ…μ„€λͺ… μš”μ•½
λ°˜λ³΅λ¬Έκ°™μ€ μ½”λ“œλ₯Ό μ—¬λŸ¬ 번 μ‹€ν–‰ν•˜λŠ” ꡬ쑰
for λ¬Έμ΄ˆκΈ°μ‹, 쑰건식, 증감식을 ν¬ν•¨ν•œ 반볡문
반볡 쑰건쑰건이 true일 λ™μ•ˆ 반볡 μˆ˜ν–‰
ν™œμš© μ˜ˆμ‹œλͺ©λ‘ 좜λ ₯, ꡬꡬ단, μ‹œκ³„ λ“± 반볡 둜직
πŸ”΄πŸŸ‘πŸŸ’

public static void run2() {
    for (int i = 1; i <=9; i++) {
        for (int j = 1; j <=9; j++) {
            System.out.printf("%d x %d = %2d  ", j, i, i*j);
        }
        System.out.println();
    }
}

▢️ κ²°κ³Ό

1 x 1 =  1  2 x 1 =  2  3 x 1 =  3  4 x 1 =  4  5 x 1 =  5  6 x 1 =  6  7 x 1 =  7  8 x 1 =  8  9 x 1 =  9  
1 x 2 =  2  2 x 2 =  4  3 x 2 =  6  4 x 2 =  8  5 x 2 = 10  6 x 2 = 12  7 x 2 = 14  8 x 2 = 16  9 x 2 = 18  
1 x 3 =  3  2 x 3 =  6  3 x 3 =  9  4 x 3 = 12  5 x 3 = 15  6 x 3 = 18  7 x 3 = 21  8 x 3 = 24  9 x 3 = 27  
1 x 4 =  4  2 x 4 =  8  3 x 4 = 12  4 x 4 = 16  5 x 4 = 20  6 x 4 = 24  7 x 4 = 28  8 x 4 = 32  9 x 4 = 36  
1 x 5 =  5  2 x 5 = 10  3 x 5 = 15  4 x 5 = 20  5 x 5 = 25  6 x 5 = 30  7 x 5 = 35  8 x 5 = 40  9 x 5 = 45  
1 x 6 =  6  2 x 6 = 12  3 x 6 = 18  4 x 6 = 24  5 x 6 = 30  6 x 6 = 36  7 x 6 = 42  8 x 6 = 48  9 x 6 = 54  
1 x 7 =  7  2 x 7 = 14  3 x 7 = 21  4 x 7 = 28  5 x 7 = 35  6 x 7 = 42  7 x 7 = 49  8 x 7 = 56  9 x 7 = 63  
1 x 8 =  8  2 x 8 = 16  3 x 8 = 24  4 x 8 = 32  5 x 8 = 40  6 x 8 = 48  7 x 8 = 56  8 x 8 = 64  9 x 8 = 72  
1 x 9 =  9  2 x 9 = 18  3 x 9 = 27  4 x 9 = 36  5 x 9 = 45  6 x 9 = 54  7 x 9 = 63  8 x 9 = 72  9 x 9 = 81  

βœ… for each

πŸ”† 1. κΈ°λ³Έ κ°œλ…

  • foreachλŠ” μžλ°”μ˜ ν–₯μƒλœ for λ¬Έ(enhanced for loop)을 μ˜λ―Έν•¨
  • λ°°μ—΄μ΄λ‚˜ Collection 같은 반볡 κ°€λŠ₯ν•œ 객체λ₯Ό κ°„λ‹¨ν•˜κ²Œ μˆœνšŒν•  λ•Œ μ‚¬μš©
  • 인덱슀 없이 μš”μ†Œμ— 직접 μ ‘κ·Όν•  수 μžˆμ–΄ κ°„κ²°ν•˜κ³  μ•ˆμ „
String[] fruits = {"apple", "banana", "cherry"};

for (String fruit : fruits) {
    System.out.println(fruit);
}

πŸ’‘ μœ„ μ½”λ“œμ—μ„œ fruit은 fruits λ°°μ—΄μ˜ 각 μš”μ†Œλ₯Ό ν•˜λ‚˜μ”© κ°€μ Έμ˜΄

πŸ”† 2. List, Set λ“± μ»¬λ ‰μ…˜μ—μ„œλ„ μ‚¬μš© κ°€λŠ₯

import java.util.List;

List<Integer> numbers = List.of(1, 2, 3, 4, 5);

for (int num : numbers) {
    System.out.println(num);
}

❗ 단, Map은 key와 valueλ₯Ό ν•¨κ»˜ 닀루기 μœ„ν•΄ entrySet()을 μ‚¬μš©ν•΄μ•Ό 함

import java.util.Map;

Map<String, Integer> scores = Map.of("Alice", 90, "Bob", 85);

for (Map.Entry<String, Integer> entry : scores.entrySet()) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
}

πŸ”† 3. λ‚΄λΆ€μ μœΌλ‘œλŠ” Iterator 기반

  • foreachλŠ” 컴파일 μ‹œ λ‚΄λΆ€μ μœΌλ‘œ *Iteratorλ₯Ό μ‚¬μš©ν•˜μ—¬ 순회
  • λ”°λΌμ„œ μ»¬λ ‰μ…˜μ΄ Iterable을 κ΅¬ν˜„ν•΄μ•Ό μ‚¬μš© κ°€λŠ₯

πŸ”† 4. 단점과 주의점

  • μΈλ±μŠ€κ°€ ν•„μš”ν•  λ•ŒλŠ” 뢀적합
  • μš”μ†Œ μ‚­μ œ λ“± ꡬ쑰 변경은 ConcurrentModificationException이 λ°œμƒν•  수 있음
List<String> list = new ArrayList<>(List.of("a", "b", "c"));

for (String s : list) {
    if (s.equals("b")) {
        list.remove(s); // ❌ μ˜ˆμ™Έ λ°œμƒ κ°€λŠ₯
    }
}

πŸ’‘ 이런 κ²½μš°μ—λŠ” 일반 forλ¬Έμ΄λ‚˜ Iteratorλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 μ•ˆμ „

πŸ” μš”μ•½

νŠΉμ§•μ„€λͺ…
κ°„κ²°ν•¨μΈλ±μŠ€ 없이 μš”μ†Œ 직접 μ ‘κ·Ό
적용 λŒ€μƒλ°°μ—΄, Iterable μ»¬λ ‰μ…˜ (List, Set, Map.entrySet() λ“±)
λ‹¨μ μΈλ±μŠ€κ°€ ν•„μš”ν•œ 경우, μ‚­μ œ/μ‚½μž… 같은 λ³€κ²½ μ‹œ μ‚¬μš© λΆˆκ°€

βœ… while

β€œwhile은 λ¨Όμ € 검사, do-while은 λ¨Όμ € μ‹€ν–‰!
상황에 따라 λ°˜λ³΅λ¬Έμ„ λ˜‘λ˜‘ν•˜κ²Œ μ„ νƒν•˜μž!

πŸ”΄πŸŸ‘πŸŸ’

public static void run() {
    int i = 0;
    while (i < 5) {
        System.out.println(i); //λ°˜λ³΅ν•  μ½”λ“œ
        i++; //κ°’ 증가,κ°μ†Œ
    }
}

public void doWhileCase() {
    int input = 2;
    do {
        System.out.println("μ•ˆλ…•ν•˜μ„Έμš” 자판기 μž…λ‹ˆλ‹€.");
        //ν‚€μž…λ ₯
        switch (input) {
            case 1:
                System.out.println("콜라");
                break;
            case 2:
                System.out.println("사이닀");
                break;
            case 3:
                System.out.println("λ¬Ό");
                break;
            default:
                System.out.println("잘λͺ» 고름");
        }
        input = 0;
    } while (input != 0);
}

▢️ κ²°κ³Ό
0
1
2
3
4
μ•ˆλ…•ν•˜μ„Έμš” 자판기 μž…λ‹ˆλ‹€.
사이닀

πŸ’« CS

βœ”οΈ μ΄μ§„μˆ˜μ™€ μ‹­μœ‘μ§„λ²•

μ‹­μ§„μˆ˜0123456789101112131415
μ΄μ§„μˆ˜01101110010111011110001001101010111100110111101111
μ‹­μœ‘μ§„μˆ˜0123456789ABCDEF

↔️ 이진법 μ‹­μœ‘μ§„λ²• 비ꡐ

ν•­λͺ©μ΄μ§„μˆ˜μ‹­μœ‘μ§„μˆ˜
기호0, 10–9, A–F
κΈ°λ³Έ λ‹¨μœ„1 bit4 bits (4λΉ„νŠΈ = 1자리)
μ‚¬μš© λͺ©μ μ»΄ν“¨ν„° λ‚΄λΆ€ μ²˜λ¦¬κ°€λ…μ„±μ„ μœ„ν•œ ν‘œν˜„ 방식
μ‚¬μš© 사둀CPU μ—°μ‚°, λ©”λͺ¨λ¦¬ μ €μž₯ λ“±μ£Όμ†Œ, 색상, 디버깅 μ½”λ“œ λ“±
πŸ”΄πŸŸ‘πŸŸ’

int number =  12345678;
public void run() {
    String binaryString = Integer.toBinaryString(number); //2μ§„μˆ˜
    String octalString = Integer.toOctalString(number); //8μ§„μˆ˜
    String hexString = Integer.toHexString(number); //16μ§„μˆ˜
    String decimalString = Integer.toString(number); //10μ§„μˆ˜
    System.out.println("Integer to Binary: " + binaryString);
    System.out.println("Integer to Octal: " + octalString);
    System.out.println("Integer to Hex: " + hexString);
    System.out.println("Integer to Decimal: " + decimalString);
}

▢️결과
Integer to Binary: 101111000110000101001110
Integer to Octal: 57060516
Integer to Hex: bc614e
Integer to Decimal: 12345678
profile
πŸ—‚οΈ hamstern

0개의 λŒ“κΈ€