๐Ÿฅ‡ ์ •๋ณด์ฒ˜๋ฆฌ ๊ธฐ์‚ฌ ์‹ค๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ-์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜

๋‹ค์Œ java๋กœ ๊ตฌํ˜„๋œ ํ”„๋กœ๊ทธ๋žจ์„ ๋ถ„์„ํ•˜์—ฌ ๊ด„ํ˜ธ์— ๋“ค์–ด๊ฐˆ ์•Œ๋งž์€ ์˜ˆ์•ฝ์–ด๋ฅผ ์“ฐ์‹œ์˜ค

public class Test { 
	public static void main(String[] args) { 
		System.out.print(Test.check(1));
	} 
	 
	"( 1 )" String check(int num){ 
		return (num >= 0) ? "positive" : "negative";
	}
}
  • static์€ ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ๋ฉ”์†Œ๋“œ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์˜ˆ์•ฝ์–ด์ด๋‹ค.
  • main๋ฉ”์„œ๋“œ๋Š” static์ด๋‹ค. ์ฆ‰ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ์‹คํ–‰๋œ๋‹ค. ์ด๋–„,,,,
  • ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ํฌํ•จ๋œ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ ํ›„ [๊ฐ์ฒด๋ณ€์ˆ˜].[๋ฉ”์†Œ๋“œ]์˜ ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด์•ผํ•˜์ง€๋งŒ, static์„ ์ด์šฉํ•œ๋‹ค๋ฉด ๊ฐ์ฒด๋ณ€์ˆ˜ ์—†์ด [ํด๋ž˜์Šค์ด๋ฆ„].[๋ฉ”์†Œ๋“œ]๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•˜๋Š”๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์ฆ‰ ๋ฉ”์ธ ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹ˆ๋”๋ผ๋„, static๋ฉ”์„œ๋“œ ์•ˆ์—์„œ ์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์˜ค๋ฅ˜๋ฅผ ์•ผ๊ธฐํ•œ๋‹ค.

๋‹ต : static


์•„๋ž˜ ์ฝ”๋“œ์—์„œ ์ถœ๋ ฅ๋˜๋Š” ๊ฐ’์„ ์ ์œผ์‹œ์˜ค,

int increment() {
    static int x = 0;
    x += 2;
    return x;
}

int main() {
    int x = 1;
    int sum = 0;
    for (int i = 0; i < 4; i++) {
        x++;
        sum += increment();
    }
    printf("%d", sum);
}
  • static int x๋Š” "static" ์œผ๋กœ ์„ ์–ธํ•˜์—ฌ ์ „์—ญ๋ณ€์ˆ˜(์–ด๋А๊ณณ์—์„œ๋“  ์ฐธ์กฐ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜)๋กœ ์‚ฌ์šฉ๋˜์–ด ํ˜ธ์ถœ(์‚ฌ์šฉ) ์ดํ›„์—๋„ ์ดˆ๊ธฐํ™” ๋˜์ง€์•Š๋Š”๋‹ค.
  • ์ฆ‰ 2+4+6+8

๋‹ต : 20


๋‹ค์Œ ํŒŒ์ด์ฌ ์ฝ”๋“œ์˜ ์ถœ๋ ฅ๊ฐ’์„ ์ ์œผ์‹œ์˜ค.

def test(v):
	if type(v) == type(""):
		return len(v)
	elif type(v) == type(100):
		return 101
	else:
		return 20
a = "100.0"  
b = 100.0 
c = (100.0, 200.0)
print(test(a) + test(b) + test(c))
  • type(v) == type("") : v๊ฐ€ ๋ฌธ์ž์—ด ํƒ€์ž…์ด๋ฉด
  • type(v) == type(100) : v๊ฐ€ ์ •์ˆ˜ํ˜• ํƒ€์ž…์ด๋ฉด
  • 100.0์€ ์‹ค์ˆ˜ํ˜•์œผ๋กœ, ์ •์ˆ˜ํ˜• ํƒ€์ž…์ด ์•„๋‹ˆ๋‹ค.
  • a : ๋ฌธ์ž์—ด / b : ์‹ค์ˆ˜ํ˜• / c : ์‹ค์ˆ˜ํ˜• - 5 + 20 + 20

๋‹ต : 45


๋‹ค์Œ ํŒŒ์ด์ฌ ์ฝ”๋“œ์—์„œ ์ถœ๋ ฅ์„ ์ ์œผ์‹œ์˜ค

public class Main {
	public static void main(String[] args) {
		int sum = 0;
		try {
			func();
		} catch(NullPointerException e) {
			sum = sum + 1;
		} catch(Exception e) {
			sum = sum + 10;
		} finally {
			sum = sum + 100;
		}
		System.out.print(sum);
	}
	static void func() throws Exception {
		throw new NullPointerException();
	}
}
  • funcํ•จ์ˆ˜๋Š” NullPointerException์„ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ํ•จ์ˆ˜์ด๋‹ค.
  • ๋‘ ๊ฐœ์˜ catchํ•จ์ˆ˜๋Š” ์ต์…ฅ์…˜์„ ๊ฐ์ง€ํ•œ๋‹ค.
  • ์ด ๋•Œ, Exception ํƒ€์ž…์€ ๋ชจ๋“  ์ต์…‰์…˜์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
  • ํ•˜์ง€๋งŒ, ์ˆœ์„œ๋Œ€๋กœ NullPointerException์ด catch๋˜๊ณ , ํ•œ๊ฐœ์˜ case๊ฐ€ catch๋˜์—ˆ์œผ๋ฏ€๋กœ,
  • finally๋กœ ์ด๋™ํ•œ๋‹ค.

๋‹ต : 101


๋‹ค์Œ c์ฝ”๋“œ์˜ ์ถœ๋ ฅ์„ ์ ์œผ์‹œ์˜ค

class B {
	int x = 3;
	int getX() {
		return x * 2;
	}
}

class A extends B {
	int x = 7;
	@Override
	int getX() {
		return x * 3;
	}
}

public class Annotation {
	public static void main(String[] args) {
		B b1 = new A(); 
		A b2 = new A(); 
		System.out.print(b1.getX() + b1.x + b2.getX() + b2.x);
	}
}
  • b1๊ฐ์ฒด๋Š” B๊ฐ์ฒด์ด์ง€๋งŒ ํ•จ์ˆ˜๋Š” ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๋œ๋‹ค. ์ฆ‰, ํ•„๋“œ ๋ณ€์ˆ˜๋Š” 3 ํ•จ์ˆ˜๋Š” 21 -B๊ฐ์ฒด์ด์ง€๋งŒ, A๋กœ ์ดˆ๊ธฐํ™”๋ฅผ ํ• ๋•Œ, ์˜ค๋ฐ”๋ผ์ด๋”ฉ ๋œ๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค.
  • b2๊ฐ์ฒด๋Š” ํ•„๋“œ๋ณ€์ˆ˜ 7, ํ•จ์ˆ˜๋Š” 21
  • 21 + 3 + 21 + 7

๋‹ต : 52


๋‹ค์Œ c์ฝ”๋“œ์˜ ์ถœ๋ ฅ์„ ์ ์œผ์‹œ์˜ค

#include 

void swap() {
	int a = 11;
	int b = 19;
	int t = a;
	a = b;
	b = t;
}

int main() {
	int a = 11;
	int b = 19;
	swap();
	switch(a) {
		case 1:
			b += 1;
		case 11:
			b += 2;
		default:
			b += 3;
			break;
	}
	printf("%d", a-b);
}
  • swapํ•จ์ˆ˜๋Š” ์ถœ๋ ฅ๋„ ์—†์œผ๋ฉฐ, ๋ฆฌํ„ด๋„ ์—†์Œ ( c์–ธ์–ด๋Š” ์ฝœ๋ฐ”์ด๋ฒจ๋ฅ˜ ์ด๋‹ค. ์ด๋•Œ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ƒˆ๋กœ ์„ ์–ธํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ์—๋„ ํ•ด๋Œฑํ•˜์ง€ ์•Š๋Š”๋‹ค.)
  • ๋”ฐ๋ผ์„œ a ๋Š” 11๋กœ case๋ฌธ์„ ํƒ€๊ฒŒ ๋˜๊ณ , break๋ฌธ์ด ์—†์œผ๋ฏ€๋กœ default๋ฌธ์„ ํƒ€๊ฒŒ๋œ๋‹ค.
  • (break ๊ฐ€ ์žˆ์—ˆ์œผ๋ฉด default๋ฌธ์„ ํƒ€์ง€ ์•Š๋Š”๋‹ค.)

๋‹ต : -13


๋‹ค์Œ JAVA์ฝ”๋“œ์˜ ์ถœ๋ ฅ๊ฐ’์„ ์ ์œผ์‹œ์˜ค

class Connection {
 
    private static Connection _inst = null;
    private int count = 0;
    
    static public Connection get() {
        if(_inst == null) {
            _inst = new Connection();
            return _inst;
        }
        return _inst;
    }
    
    public void count() {
         count++; 
    }
    
    public int getCount() {
         return count; 
    }
}
 
 
public class main {  
 
    public static void main(String[] args) {
 
        Connection conn1 = Connection.get();
        conn1.count();
 
        Connection conn2 = Connection.get();
        conn2.count();
 
        Connection conn3 = Connection.get();
        conn3.count();
        
        conn1.count();
        System.out.print(conn1.getCount());
    }
 
}
  • ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ๊ตฌํ˜„ํ•œ ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.
  • conn1/2/3/4 ๋ชจ๋‘ ํ•œ ๊ฐ์ฒด์ด๋ฏ€๋กœ count๋ฅผ ๊ณต์œ ํ•œ๋‹ค.

๋‹ต : 4