๐Ÿ’ป Programming/Java (40)

2019๋…„ 6์›” 15์ผ ๋กœ๋˜ 1๋“ฑ ๋‹น์ฒจ๋ฒˆํ˜ธ

์ด๋ฒˆ์—๋Š” ์ž๋ฐ”๋กœ ๋กœ๋˜ ๋ฒˆํ˜ธ ์ž๋™ ์ƒ์„ฑ๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๊ธฐ์ „์— ์šฐ์„  ๋กœ๋˜์˜ ๊ทœ์น™๋ถ€ํ„ฐ ์•Œ์•„์•ผ ๊ฒ ์ฃ .

๋กœ๋˜์—๋Š” 1๋ถ€ํ„ฐ 45๊นŒ์ง€์˜ ์ˆซ์ž๊ฐ€ ์กด์žฌํ•˜๊ณ 

๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ํฌํ•จํ•˜์—ฌ ์ด 7๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋žœ๋คํ•˜๊ฒŒ ๋ฝ‘์Šต๋‹ˆ๋‹ค.

๊ฐ ์ˆซ์ž๋Š” ์ค‘๋ณต๋˜์–ด ์„ ํƒํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ํ‚ค ํฌ์ธํŠธ ๋‹จ์–ด๋Š” ๋ฐ”๋กœ ๋žœ๋ค๊ณผ ์ค‘๋ณต ์ž…๋‹ˆ๋‹ค.

์ž๋ฐ”์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ Set์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

์ž, ๊ทธ๋Ÿผ ๋žœ๋คํ•˜๊ฒŒ ์ˆซ์ž๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”์—์„œ ๋žœ๋คํ•œ ์ˆซ์ž๋ฅผ ์–ป์„ ๋•Œ๋Š” Math.random() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ด ๋ฉ”์„œ๋“œ๋Š” 0๋ถ€ํ„ฐ 1์‚ฌ์ด์˜ double ํ˜•ํƒœ์˜ ์†Œ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ด์ค๋‹ˆ๋‹ค.

์˜ค์ž‰?? ๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ 1๋ถ€ํ„ฐ 45๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์–ป๋Š”๋‹ค๋Š” ๊ฑธ๊นŒ์š”??

์ด์ œ๋ถ€ํ„ฐ ํŠธ๋ฆญ์„ ์•Œ๋ ค๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

Math.random()์€ 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ  1๋ณด๋‹ค ์ž‘์€ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

๋‚ด๊ฐ€ ์–ป๊ณ ์žํ•˜๋Š” max์ˆซ์ž๋ฅผ ๊ณฑํ•ด์ฃผ๋ฉด

0๋ถ€ํ„ฐ max๊ฐ’ ์‚ฌ์ด์˜ double ํ˜•ํƒœ์˜ ์ˆซ์ž๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ ์ด ์ˆซ์ž๋ฅผ ๋‹ค์‹œ intํ˜•ํƒœ๋กœ casting์„ ํ•ด๋ฒ„๋ฆฌ๋ฉด?

0<= ์›ํ•˜๋Š” ์ˆซ์ž < max ์‚ฌ์ด์˜ int ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ณด์—ฌ๋“œ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

public static void main(String[] args) {
  getRandomNumber();
}
	
private static void getRandomNumber() {
  final int max = 46;
  for(int i = 0; i < 10; i++) {
    int randomNumber = (int)(Math.random() * max);
    System.out.println(randomNumber);
  }
}

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

0
12
34
27
5
21
10
24
43

์œ„ ๊ฒฐ๊ณผ์—๋Š” ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์•˜์ง€๋งŒ

๋ช‡ ๋ฒˆ ์‹คํ–‰ํ•˜๋‹ค๋ณด๋ฉด ๋™์ผํ•œ ์ˆซ์ž๊ฐ€ ์—ฐ์†์œผ๋กœ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•ด์ฃผ๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด๋•Œ ํ•„์š”ํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ Set์ž…๋‹ˆ๋‹ค.

Set์€ ์ž๋ฐ”์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ ์ž ํ•  ๋•Œ ์•„์ฃผ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ด๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

Set์˜ ๊ตฌํ˜„์ฒด๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ ์ผ๋ฐ˜์ ์œผ๋กœ ๋งŽ์ด ์“ฐ์ด๋Š” ๊ฒƒ์€ HashSet์ž…๋‹ˆ๋‹ค.

์•„๋ž˜ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ํ•œ๋ฒˆ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

public static void main(String[] args) {
  removeDuplicate();
}
	
private static void removeDuplicate() {
  Set<Integer> noDuplicated = new HashSet<>();
  noDuplicated.add(30);
  noDuplicated.add(30);
  noDuplicated.add(31);
  System.out.println(noDuplicated);
}

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

[30, 31]

๋ณด์‹œ๋Š” ๋ฐ”์™€ ๊ฐ™์ด 30์„ ๋‘ ๋ฒˆ add ํ–ˆ๋Š”๋ฐ 1๊ฐœ๋งŒ ์ €์žฅ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ด๊ฒƒ์„ ์ด์šฉํ•ด์„œ ์ด๋ฏธ ๋ฝ‘์€ ์ˆซ์ž๋Š” ์ €์žฅํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๋ฒˆํ˜ธ๋ฅผ ๋ฝ‘๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜๋Š” ์™„์„ฑ๋œ ๋กœ๋˜ ๋ฒˆํ˜ธ ์ƒ์„ฑ๊ธฐ์˜ ์ƒ˜ํ”Œ์ž…๋‹ˆ๋‹ค.

private static void lotto() {
  final int min = 1;
  final int max = 45;
		
  Set<Integer> selected = new HashSet<>();
  while(true) {
    int randomNumber = (int)(Math.random() * (max - min +1) + min);
    if(selected.add(randomNumber)) {
      if(selected.size() == 6) {
        break;
      }
    }
  }
  System.out.println(selected.stream().sorted().collect(Collectors.toList()));
}

์œ„ ๋ฉ”์„œ๋“œ๋ฅผ main ํ•จ์ˆ˜์—์„œ ์‹คํ–‰์‹œํ‚ค๋ฉด

1~45๊นŒ์ง€์˜ ์ˆซ์ž๋“ค ์ค‘์—์„œ ์ค‘๋ณต๋˜์ง€ ์•Š์€ 6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

๋žœ๋ค ๋ฒˆํ˜ธ ์ƒ์„ฑํ•˜๋Š” ๋ถ€๋ถ„์ด ์กฐ๊ธˆ ์ˆ˜์ •๋œ ๊ฒƒ์„ ๋ˆˆ์น˜์ฑ„์…จ๋‚˜์š”?

์ € ์œ„์—์„œ ๋ณด์—ฌ๋“œ๋ฆฐ ๋กœ์ง์œผ๋กœ๋Š” 0์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ˆ˜์ •์„ ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ž๋ฐ”๋กœ ๋žœ๋ค ์ˆซ์ž ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋žœ๋ค ์ˆซ์ž ๊ตฌํ•˜๊ธฐ ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์œ„ ๋ฉ”์„œ๋“œ๋ฅผ 10๋ฒˆ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

[5, 17, 18, 25, 29, 33]
[9, 13, 16, 26, 30, 34]
[21, 24, 27, 30, 31, 42]
[9, 15, 17, 34, 36, 42]
[2, 3, 15, 35, 38, 41]
[8, 16, 30, 31, 36, 39]
[1, 5, 11, 40, 43, 44]
[1, 3, 4, 18, 21, 39]
[4, 7, 19, 28, 38, 40]
[7, 9, 14, 18, 30, 41]

์ด์ƒ์œผ๋กœ ์ž๋ฐ”๋กœ ์‹ฌํ”Œํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ ์ž๋™ ์ƒ์„ฑ๊ธฐ ๋งŒ๋“ค๊ธฐ ํฌ์ŠคํŒ…์„ ๋งˆ์น˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ถ๊ธˆํ•œ ์ ์ด ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์‹œ๊ณ ,

ํฌ์ŠคํŒ… ๋‚ด์šฉ์ด ๋„์›€์ด ๋˜์…จ๋‹ค๋ฉด ๊ณต๊ฐ ๊พน ๋ถ€ํƒํ•ด์š” ^-^

 

์ŠคํŠธ๋ง ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘๊ธฐ

 

์ž๋ฐ”์—์„œ ์ŠคํŠธ๋ง์„ ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ด๋“œ๋ฆฝ๋‹ˆ๋‹ค.

 

์ž๋ฐ”์—๋Š” ๊ฐ์ข… array๋กœ๋ถ€ํ„ฐ String์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” StringBuilder๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ƒ์„ฑ์ž์— String์„ ๊ทธ๋Œ€๋กœ ๋„ฃ์–ด์„œ ์ƒˆ๋กœ์šด ์ŠคํŠธ๋ง์„ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ตฌํ˜„๋˜์–ด์žˆ๋Š” reverse ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹จ ํ•œ ์ค„๋กœ ์ŠคํŠธ๋ง์„ ๊ฑฐ๊พธ๋กœ ๋ฐฐ์—ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

String a = "abcdefg";

String b = new StringBuilder(a).reverse().toString();

System.out.println(a);
System.out.println(b);

์ถœ๋ ฅ ๊ฒฐ๊ณผ 

abcdefg

gfedcba

 

์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

 

๋žœ๋ค ์ˆซ์ž๋“ค, ์ถœ์ฒ˜ : https://openclipart.org/detail/254729/random-numbers-cloud

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Java๋กœ ๋žœ๋คํ•œ ์ˆซ์ž๋ฅผ ํš๋“ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.

 

์ž๋ฐ”์—๋Š” Math ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

 

ํ•ด๋‹น ํด๋ž˜์Šค์—๋Š” ๋‹ค์–‘ํ•œ ์ˆ˜ํ•™์ ์ธ ๊ณ„์‚ฐ์„ ์œ„ํ•ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” static ๋ฉ”์„œ๋“œ๋“ค์ด ์—ฌ๋Ÿฌ๊ฐœ ์žˆ๋Š”๋ฐ,

๊ทธ ์ค‘์— ํ•˜๋‚˜๊ฐ€ random() ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

 

์ด ๋ฉ”์„œ๋“œ๋Š” 0์—์„œ 1์‚ฌ์ด์˜ ๋žœ๋คํ•œ double ํƒ€์ž… ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

javadoc ๋ฌธ์„œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๋ช…์ด ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.

 

random

public static double random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

 

์ฆ‰, 0.0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ  1.0๋ณด๋‹ค ์ž‘์€ double(์‹ค์ˆ˜)ํ˜• ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 

 

์ด๊ฒŒ 0~1 ์‚ฌ์ด์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์–ด๋–ป๊ฒŒ ๋žœ๋คํ•œ ์ˆซ์ž๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์„๊นŒ์š”?? 

 

์˜ˆ๋ฅผ ๋“ค์–ด 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ  100๋ณด๋‹ค ์ž‘์€ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

int max = 100;

int randomInt = (int) (Math.random() * max);

 

์œ„ ์ฝ”๋“œ์—์„œ (int) (Math.random() * max) ๋ถ€๋ถ„์€ 0 <= ๊ฒฐ๊ณผ๊ฐ’ < max (100) ์˜ ๋ฒ”์œ„ ๋‚ด์˜ ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

Math.random()์˜ ์ตœ์†Œ๊ฐ’์ด 0์ด๊ณ  ์ตœ๋Œ€๊ฐ’์ด 1.0๋ณด๋‹ค ์ž‘๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . 

 

์ด๋ฒˆ์—๋Š” 100์„ ํฌํ•จํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

int max = 100;

int randomInt = (int) Math.round(Math.random() * max);

 

Math.round() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด 0<= ๊ฒฐ๊ณผ๊ฐ’ <= max (100) ๋ฒ”์œ„ ๋‚ด์˜ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•  ๊ฒฝ์šฐ 0๊ณผ 100์ด ๋‚˜์˜ฌ ํ™•๋ฅ ์€ 1~99๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ๋ณด๋‹ค 50% ์ ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

0์ด ๋‚˜์˜ค๋ ค๋ฉด Math.round()์˜ ๊ฐ’์ด 0~0.004999...๊ฐ€ ๋‚˜์™€์•ผ ํ•˜๊ณ 

100์ด ๋‚˜์˜ค๋ ค๋ฉด 0.95~0.9999...๊ฐ€ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ด ๊ฒฝ์šฐ 0.5%์˜ ํ™•๋ฅ ์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

1์ด ๋‚˜์˜ค๋ ค๋ฉด 0.005~0.014999..๊ฐ€ ๋‚˜์˜ค๋ฉด ๋˜๊ณ , 2, 3, 4..., 99 ๊นŒ์ง€๋Š” ๋ชจ๋‘ ๋™์ผํ•œ ํ™•๋ฅ ์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์ด ๊ฒฝ์šฐ 1%์˜ ํ™•๋ฅ ์„ ๊ฐ–๊ฒŒ ๋˜์ฃ .

 

๋”ฐ๋ผ์„œ ๊ณ ๋ฅด๊ฒŒ ๋ถ„ํฌํ•˜๋Š” 0~100 ์‚ฌ์ด์˜ ๊ฐ’(๊ฒฝ๊ณ„ํฌํ•จ)์„ ์–ป์œผ๋ ค๋ฉด ์ข€ ๋” ๊ณ ๋ฏผ์„ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒํ•ด์•ผ 0~100 ์‚ฌ์ด์˜ ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ๋™์ผํ•œ ํ™•๋ฅ ๋กœ ์–ป์„ ์ˆ˜ ์žˆ์„๊นŒ์š”??

 

int max = 100;
int randomInt = (int) (Math.random() * (max + 1));

๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. max๋ฅผ ํฌํ•จํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ max + 1์„ ๊ณฑํ•ด์ฃผ๋ฉด ๋งŒ์‚ฌOK์ž…๋‹ˆ๋‹ค. 

 

์ž, ๊ทธ๋Ÿผ ์ตœ์†Œ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ์ •ํ•ด์ฃผ๊ณ  ์‹ถ์œผ๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”?

 

int min = 90, max = 100;
int randomInt = (int) (Math.random() * (max + 1 - min)) + min;

์œ„ ์ฒ˜๋Ÿผ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์ฆ‰, ๋žœ๋คํ•˜๊ฒŒ ์ตœ๋Œ€๊ฐ’์—์„œ ์ตœ์†Œ๊ฐ’์„ ๋บ€ ๋งŒํผ์˜ ์ˆซ์ž๋ฅผ ๋งŒ๋“  ๋’ค์— ์ตœ์†Œ๊ฐ’์„ ๋”ํ•ด์ฃผ๋Š”๊ฑฐ์ฃ . 

 

์—ฌ๊ธฐ์„œ๋Š” ์ตœ๋Œ€๊ฐ’ 100์—์„œ 1์„ ๋”ํ•˜๊ณ  ์ตœ์†Œ๊ฐ’ 90์„ ๋นผ์„œ ์ตœ์†Œ 0~ ์ตœ๋Œ€ 10์ด ๋‚˜์˜ค๋„๋ก ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. 

 

๊ฑฐ๊ธฐ๋‹ค๊ฐ€ min(90)๊ฐ’์„ ๋”ํ•ด์ฃผ๋ฉด 0+90 ~ 10+90 ์‚ฌ์ด์˜ ์ˆ˜, ์ฆ‰, 90~100 ์‚ฌ์ด์˜ ์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ฒŒ๋˜์ฃ . 

 

ํ•˜๋‚˜ํ•˜๋‚˜ ๋”ฐ๋ผ๊ฐ€๋‹ˆ ์–ด๋ ต์ง€ ์•Š์ฃ ?

 

์˜ค๋Š˜๋„ ์ฆ๊ฑฐ์šด ์ฝ”๋”ฉํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค~

 

 

์ž๋ฐ”์—์„œ ์ˆซ์ž ํŒ๋ณ„ํ•˜๊ธฐ

 

Java์˜ ์ŠคํŠธ๋ง์ด ์ˆซ์ž(์ •์ˆ˜, int)์ธ์ง€ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ• ๋‘ ๊ฐ€์ง€๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค

 

 

1) ์ŠคํŠธ๋ง์˜ ๊ฐ ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ˆœํšŒํ•˜๋ฉด์„œ ascii ์ฝ”๋“œ ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•

 

์ฒซ ๋ฒˆ์งธ๋กœ ์†Œ๊ฐœํ•ด๋“œ๋ฆด ๋ฐฉ๋ฒ•์€ ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด(์ŠคํŠธ๋ง)์˜ ๊ฐ ๋ฌธ์ž๊ฐ€ 0~9 ascii ์ฝ”๋“œ๊ฐ’ ์‚ฌ์ด์— ์žˆ๋Š” char์ธ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

0~9์˜ ascii ์ฝ”๋“œ๊ฐ’์€ ์‹ญ์ง„์ˆ˜๋กœ 48~57 ์ž…๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ String์„ char[]๋กœ ๋ณ€ํ™˜ํ•œ ๋‹ค์Œ loop๋ฅผ ๋Œ๋ฉด์„œ ๊ฐ char๊ฐ€ 48~57์‚ฌ์ด์˜ ๊ฐ’์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜์—ฌ ์ˆซ์ž์ธ์ง€๋ฅผ ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.

 

์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

String str = "not a number";

boolean isNumber = true;

for(char c : str.toCharArray()) {

  if(c >= 48 && c<= 57) {
        continue;
  } else {
        isNumber = false;
        break;
  }
}
System.out.println("Is string a number ? answer: " + isNumber);

 

2) Integer.parseInt ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

 

๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ Integer.parseInt ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

parseInt ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ javadoc ๋ฌธ์„œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๋ช…์ด ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.

public static int parseInt(String s)
                    throws NumberFormatException
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to theparseInt(java.lang.String, int) method.

 

Parameters:
s - a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException - if the string does not contain a parsable integer.

 

 

์ฆ‰, ๋ฌธ์ž๋กœ ํ‘œํ˜„๋œ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ int๋กœ returnํ•˜๋Š”๋ฐ์š”

์ž…๋ ฅ๋ฐ›์€ ์ŠคํŠธ๋ง์ด ์ˆซ์ž๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ NumberFormatException์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ ์œ„ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ NumberFormatException์˜ ๋ฐœ์ƒ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ํ•ด๋‹น ์ŠคํŠธ๋ง์ด ์ˆซ์ž์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

String str = "421303";

boolean isNumber = false;

try {

Integer.parseInt(str);

isNumber = true;

} catch (NumberFormatException e) {

// do nothing

}

System.out.println("Is string a number ? answer: " + isNumber);

 

Integer ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ wrapper ํด๋ž˜์Šค๋“ค์—๋„ Long.parseLong, Double.parseDouble ๋ฉ”์„œ๋“œ๋“ค์ด ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

 

 

์œ„ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์™ธ์— ๋˜ ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ์œผ์‹  ๋ถ„๋“ค ์ œ๋ณด๋ฐ›์Šต๋‹ˆ๋‹ค ใ…Ž

 

๋‚ด์šฉ์ด ๋„์›€์ด ๋˜์…จ๋‹ค๋ฉด ๊ณต๊ฐ ๊พน ๋ˆ„๋ฅด๊ณ  ๊ฐ€์ฃผ์„ธ์š”~

 

 

 

 

AWS ์ž๊ฒฉ ์ฆ๋ช… ๋งŒ๋“ค๊ธฐ



  Security Credentials ์„ค์ •ํ•˜๊ธฐ

AWS SDK๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•  ๋•Œ ์—…๋กœ๋“œ ๊ถŒํ•œ์„ ์–ป๊ธฐ ์œ„ํ•ด Security Credentials์„ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ „์ฒด์ ์ธ ํ๋ฆ„์€ ์‚ฌ์šฉ์ž๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ถŒํ•œ์„ ์„ค์ •ํ•˜๊ณ  Access key์™€  Secret key๋ฅผ ๋ฐœ๊ธ‰๋ฐ›๋„๋ก ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.



 1  ๋‚ด ๋ณด์•ˆ ์ž๊ฒฉ ์ฆ๋ช…(My Security Credentials) ์„ ๋ˆŒ๋Ÿฌ Security Credentials ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.




 2  IAM ์‚ฌ์šฉ์ž ์‹œ์ž‘ํ•˜๊ธฐ(Get Started with IAM Users) ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ๋‹ค์Œ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. 



 3  ์‚ฌ์šฉ์ž ์ถ”๊ฐ€(Add User) ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด์„œ ์œ ์ €๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.



 4  ์‚ฌ์šฉ์ž ์ด๋ฆ„(User name)์„ ์ง€์ •ํ•˜๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹ ์•ก์„ธ์Šค(Programmatic access) ๋ฅผ ์„ ํƒํ•œ ๋’ค ๋‹ค์Œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ธฐ ์ „์— ์•„๋ž˜์ชฝ์œผ๋กœ ํ™”๋ฉด์„ ์Šคํฌ๋กคํ•˜๋ฉด ๊ถŒํ•œ๊ฒฝ๊ณ„์„ค์ • ํ•ญ๋ชฉ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ถŒํ•œ ๊ฒฝ๊ณ„์—†์ด ์ƒ์„ฑ์„ ์„ ํƒํ•˜๊ณ  ๋‹ค์Œ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.




 5  ๊ธฐ์กด ์ •์ฑ… ์ง์ ‘ ์—ฐ๊ฒฐ์„ ์„ ํƒํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด S3๋ผ๊ณ  ๊ฒ€์ƒ‰ํ•˜๋ฉด ๊ด€๋ จ ์ •์ฑ… ๋ชฉ๋ก์ด ์ถœ๋ ฅ๋˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” AmazonS3FullAccess๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.



 6  ๊ฒ€ํ† (Review)๋ฅผ ํ†ตํ•ด ์˜ฌ๋ฐ”๋ฅธ ์„ค์ •์„ ํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์‚ฌ์šฉ์ž ๋งŒ๋“ค๊ธฐ(Create user)๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.



 7  ์ƒ์„ฑ๋œ ์‚ฌ์šฉ์ž์˜ ์•ก์„ธ์Šค ํ‚ค ID(Access key ID)์™€ ๋น„๋ฐ€ ์•ก์„ธ์Šค ํ‚ค(Secret access key)๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋น„๋ฐ€์•ก์„ธ์Šค ํ‚ค๋Š” "ํ‘œ์‹œ"๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋ณด์—ฌ์ง‘๋‹ˆ๋‹ค.



AWS S3์— ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•  ๋•Œ ์—ฌ๊ธฐ์— ๋ณด์—ฌ์ง€๋Š” ์•ก์„ธ์Šค ํ‚ค ID์™€ ๋น„๋ฐ€ ์•ก์„ธ์Šค ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒ์„ฑ๋œ ์‚ฌ์šฉ์ž๋Š” ์–ธ์ œ๋“ ์ง€ "๋‚ด ๋ณด์•ˆ ์ž๊ฒฉ ์ฆ๋ช…" ๋ฉ”๋‰ด๋ฅผ ํ†ตํ•ด ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.




  AWS SDK Java jar ํŒŒ์ผ ๋‹ค์šด ๋ฐ›๊ธฐ

AWS-SDK for Java - https://aws.amazon.com/ko/sdk-for-java/

์œ„ ๋งํฌ๋ฅผ ๋”ฐ๋ผ ๋“ค์–ด๊ฐ€๋ฉด ์šฐ์ธก ์ƒ๋‹จ์— SDK๋‹ค์šด๋กœ๋“œ ๋˜๋Š” ์ดํด๋ฆฝ์Šค์šฉ ํˆดํ‚ท์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํŠผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ €๋Š” ๋ฉ”์ด๋ธ์„ ์‚ฌ์šฉํ• ๊ฑด๋ฐ ์ง์ ‘ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๊ฑฐ๋‚˜ ํˆดํ‚ท์„ ์จ๋ณด๊ณ  ์‹ถ์œผ์‹  ๋ถ„๋“ค์€ ์œ„ ๋งํฌ๋ฅผ ์ด์šฉํ•ด์ฃผ์„ธ์š”.


maven์„ ์ด์šฉํ•œ๋‹ค๋ฉด 

1
2
3
4
5
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.11.386</version>
</dependency>


์ด ๊ธ€์ด ํฌ์ŠคํŒ… ๋˜๋Š” ์‹œ์ ์— ์ตœ์‹ ๋ฒ„์ „์€ 1.11.388์ž…๋‹ˆ๋‹ค. minor ๋ฒ„์ „์ด ํ•˜๋ฃจ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ์ฆ๊ฐ€ํ•˜๊ณ ์žˆ์–ด์š” ใ…Ž



  AWS s3 ํŒŒ์ผ ์—…๋กœ๋“œ ํ•˜๊ธฐ(AWS S3 Upload for java )

์‹ค์ œ๋กœ S3์— ์—…๋กœ๋“œ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” S3์— ๊ถŒํ•œ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ถŒํ•œ์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ACCESS_KEY, SECRET_KEY ๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด๋Š” ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

# Access Key, Secret Key ์ƒ์„ฑ๋ฐฉ๋ฒ• - http://keichee.tistory.com/298



์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜์ฒ˜๋Ÿผ ํŒŒ์ผ์„ ์ „๋‹ฌ๋ฐ›์•„ ์•„๋งˆ์กด S3์— accesskey์™€  secretkey๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ถŒํ•œ์„ ์–ป๊ณ  ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•˜๋ฉด๋ฉ๋‹ˆ๋‹ค. 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class AWSService {
    private static final String BUCKET_NAME = "bucket_name";
    private static final String ACCESS_KEY = "ACCESS_KEY";
    private static final String SECRET_KEY = "SECRET_KEY";
    private AmazonS3 amazonS3;
 
    public AWSService() {
        AWSCredentials awsCredentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
        amazonS3 = new AmazonS3Client(awsCredentials);
    }
 
    public void uploadFile(File file) {
        if (amazonS3 != null) {
            try {
                PutObjectRequest putObjectRequest =
                        new PutObjectRequest(BUCKET_NAME + "/sub_dir_name"/*sub directory*/, file.getName(), file);
                putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead); // file permission
                amazonS3.putObject(putObjectRequest); // upload file
 
            } catch (AmazonServiceException ase) {
                ase.printStackTrace();
            } finally {
                amazonS3 = null;
            }
        }
    }
}
 




์ž๋ฐ”๋กœ ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๊ฐ€ OOM์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํž™๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์–ด๋Š ์ง€์ ์—์„œ ์ฆ๊ฐ€ํ•˜๋Š”์ง€ ํŒŒ์•…์„ ํ•ด์•ผํ•œ๋‹ค. ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์†Œ์Šค๋ ˆ๋ฒจ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์™€ ์†Œ์Šค๋ ˆ๋ฒจ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋กœ ๋‚˜๋‰˜๋Š”๋ฐ, ์†Œ์Šค๋ ˆ๋ฒจ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด ์ž๋ฐ” ํž™ ๋คํ”„๋ฅผ ๋– ์„œ ํ™•์ธ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์†Œ์Šค๋ ˆ๋ฒจ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ, ์ฆ‰, ๊ฐœ๋ฐœ ์‹œ์ ์—์„œ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์–˜๊ธฐํ•ด๋ณด๋ ค ํ•œ๋‹ค.


์ž๋ฐ”๋Š” ๊ฐœ๋ฐœ์ž๋“ค์—๊ฒŒ ์œ ์šฉํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๊ณ ์žˆ์œผ๋ฉฐ, ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ๊ด€๋ จํ•˜์—ฌ Runtime ํด๋ž˜์Šค์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๊ณ ์žˆ๋‹ค.


Java 8 API ๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๋ช…์ด ๋˜์–ด์žˆ๋‹ค.


Class Runtime



  • public class Runtime
    extends Object
    Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.

    An application cannot create its own instance of this class.

์ฆ‰, ๋ชจ๋“  ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ๋“ค์€ ํ•˜๋‚˜์˜ Runtime ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ, ์ด Runtime์ธ์Šคํ„ด์Šค๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๊ณ ์žˆ๋Š” ํ™˜๊ฒฝ๊ณผ ์ธํ„ฐํŽ˜์ด์Šคํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค. ์ฆ‰, ์ž๋ฐ”ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๊ณ ์žˆ๋Š” ์‹œ์Šคํ…œ์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. (๋ช…๋ น์–ด ์‹คํ–‰๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.)


๊ทธ๋Ÿผ Runtime ํด๋ž˜์Šค์—์„œ ์–ด๋–ค ๋ฉ”์„œ๋“œ๋“ค์„ ์ œ๊ณตํ•˜๋Š”์ง€ ํ•œ๋ฒˆ ๋ณด์ž.


Modifier and Type

Method and Description
voidaddShutdownHook(Thread hook)
Registers a new virtual-machine shutdown hook.
intavailableProcessors()
Returns the number of processors available to the Java virtual machine.
Processexec(String command)
Executes the specified string command in a separate process.
Processexec(String[] cmdarray)
Executes the specified command and arguments in a separate process.
Processexec(String[] cmdarray, String[] envp)
Executes the specified command and arguments in a separate process with the specified environment.
Processexec(String[] cmdarray, String[] envp, File dir)
Executes the specified command and arguments in a separate process with the specified environment and working directory.
Processexec(String command, String[] envp)
Executes the specified string command in a separate process with the specified environment.
Processexec(String command, String[] envp, File dir)
Executes the specified string command in a separate process with the specified environment and working directory.
voidexit(int status)
Terminates the currently running Java virtual machine by initiating its shutdown sequence.
longfreeMemory()
Returns the amount of free memory in the Java Virtual Machine.
voidgc()
Runs the garbage collector.
InputStreamgetLocalizedInputStream(InputStream in)
Deprecated. 
As of JDK 1.1, the preferred way to translate a byte stream in the local encoding into a character stream in Unicode is via the InputStreamReader and BufferedReader classes.
OutputStreamgetLocalizedOutputStream(OutputStream out)
Deprecated. 
As of JDK 1.1, the preferred way to translate a Unicode character stream into a byte stream in the local encoding is via the OutputStreamWriterBufferedWriter, and PrintWriter classes.
static RuntimegetRuntime()
Returns the runtime object associated with the current Java application.
voidhalt(int status)
Forcibly terminates the currently running Java virtual machine.
voidload(String filename)
Loads the native library specified by the filename argument.
voidloadLibrary(String libname)
Loads the native library specified by the libname argument.
longmaxMemory()
Returns the maximum amount of memory that the Java virtual machine will attempt to use.
booleanremoveShutdownHook(Thread hook)
De-registers a previously-registered virtual-machine shutdown hook.
voidrunFinalization()
Runs the finalization methods of any objects pending finalization.
static voidrunFinalizersOnExit(boolean value)
Deprecated. 
This method is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock.
longtotalMemory()
Returns the total amount of memory in the Java virtual machine.
voidtraceInstructions(boolean on)
Enables/Disables tracing of instructions.
voidtraceMethodCalls(boolean on)
Enables/Disables tracing of method calls.


์œ„ ๋ฉ”์„œ๋“œ๋“ค์ด Runtime ํด๋ž˜์Šค์—์„œ ์ œ๊ณตํ•˜๊ณ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋“ค์ด๋‹ค.

์ด ์ค‘์—์„œ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ จ ๋ฉ”์„œ๋“œ๋“ค์ด ๋ช‡ ๊ฐœ ๋ณด์ธ๋‹ค.

์ฒซ ๋ฒˆ์งธ๋กœ freeMemory๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด ๋ฉ”์„œ๋“œ๋Š” ํž™๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์—์„œ free๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ฐ”์ดํŠธ๋‹จ์œ„๋กœ ๊ณ„์‚ฐํ•ด์„œ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  maxMemory์™€ totalMemory๊ฐ€ ์žˆ๋Š”๋ฐ totalMemory๋Š” ํ˜„์žฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‚ฌ์šฉํ•˜๊ณ ์žˆ๋Š” ์ด ํž™ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, maxMemory๋Š” OOM(OutOfMemory)์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค. ์—ญ์‹œ ๋ฐ”์ดํŠธ ๋‹จ์œ„์ด๋ฉฐ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ด ๋ฉ”๋ชจ๋ฆฌ ํ•œ๊ณ„๋ฅผ ๋„˜์–ด์„œ๋Š” ์ˆœ๊ฐ„ OOM์ด ๋ฐœ์ƒํ•˜๋ฉด์„œ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์ด ์ค‘๋‹จ๋œ๋‹ค.


์‚ฌ์šฉ์˜ˆ๋ฅผ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.


// ํ˜„์žฌ ํž™ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜

long heapSize = Runtime.getRuntime().totalMemory(); 


// ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํ• ๋‹น๋œ ํž™๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ. ์ด ์‚ฌ์ด์ฆˆ๋ฅผ ๋„˜์–ด์„œ๋ฉด OOM ๋ฐœ์ƒ

long heapMaxSize = Runtime.getRuntime().maxMemory();


// ํ˜„์žฌ ๋‚จ์•„์žˆ๋Š” free๋ฉ”๋ชจ๋ฆฌ. ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์ฆ๊ฐ์„ ๋ฐ˜๋ณต

long heapFreeSize = Runtime.getRuntime().freeMemory();


ํ•ด๋‹น ์‚ฌ์ด์ฆˆ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์€ ๋’ค KB, MB๋‹จ์œ„๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ฝ˜์†”์— ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ์ฝ๊ธฐ๊ฐ€ ํŽธํ•˜๋‹ค.

์•ˆ๋…•ํ•˜์„ธ์š”, ์ผ€์ด์น˜์ž…๋‹ˆ๋‹ค.


์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ž๋ฐ”๋กœ ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ฝ์–ด์„œ ์ถœ๋ ฅํ•ด๋ณด๋Š” ์†Œ์Šค๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.


์šฐ์„  CLI ํ™˜๊ฒฝ์ด๋“  ๋‹ค๋ฅธ IDE ํˆด ํ™˜๊ฒฝ์ด๋“  Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋ฉ”์ธ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜์ฃ .


๊ทธ๋ž˜์„œ ํด๋ž˜์Šค ํŒŒ์ผ์„ ๋งŒ๋“ค๋ฉด public void main(String[] args) {} ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด์„œ ์“ฐ์ฃ .


์ด๋•Œ  main๋ฉ”์„œ๋“œ์˜ ์ธ์ž๋กœ ๋ฐ›๋Š” ์ € String[] args ๊ฐ€ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•œ ํ†ต๋กœ์ž…๋‹ˆ๋‹ค.


์ปค๋งจ๋“œ ๋ผ์ธ ๋ช…๋ น์œผ๋กœ java myapp a.txt  ๋ผ๊ณ  ์ž…๋ ฅ์„ ํ•ด์„œ ์‹คํ–‰ํ•˜๋ฉด ์ € args์— a.txt๋ผ๋Š” ์ŠคํŠธ๋ง ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


์ฆ‰, ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅ๋ฐ›๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์ฃ .


ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅ๋ฐ›์•˜์œผ๋ฉด, ์šฐ๋ฆฌ๋Š” ๊ทธ ํŒŒ์ผ์„ ์—ด์–ด์„œ ์•ˆ์— ์žˆ๋Š” ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋Š” ๊ฒ๋‹ˆ๋‹ค.


๊ทธ๋ ‡๋‹ค๊ณ  ๋ฌด์ž‘์ • ํŒŒ์ผ์„ ์—ด ์ˆ˜๋Š” ์—†์„ ๊ฒ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ทธ ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด?? ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋„ ํ•ด์ค˜์•ผ ๊ฒ ์ฃ ?


์ž, ๊ทธ๋Ÿผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ฐจ๊ทผ์ฐจ๊ทผ ํ•ด ๋ณผ๊นŒ์š”?


์šฐ์„  ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๊ณ  ๋ฉ”์ธ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค.


public class ReadFileAndPrint {     public static void main(String[] args) {     } }


ReadFileAndPrint๋ผ๋Š” ์ด๋ฆ„์˜ ์ž๋ฐ”ํŒŒ์ผ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด์„œ ๋ฉ”์ธํ•จ์ˆ˜๋ฅผ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค.


์ž, ๊ทธ๋Ÿผ ์ด์ œ ๋ญ˜ ํ•ด์•ผ ํ•  ๊นŒ์š”? ์ € ์œ„์—์„œ ๋ง์”€๋“œ๋ฆฐ ๊ฒƒ์ฒ˜๋Ÿผ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅ๋ฐ›์•„์„œ args๋กœ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๊ฒƒ์€ ์ž๋ฐ”์—์„œ ์•Œ์•„์„œ ์ž๋™์œผ๋กœ ํ•ด์ค„ํ…Œ๋‹ˆ


์šฐ๋ฆฌ๋Š” ๊ทธ ์ „๋‹ฌ๋ฐ›์€ ํŒŒ์ผ๋ช…์„ ๊ฐ€์ง€๊ณ  ์ง€์ง€๊ณ  ๋ณถ๊ณ  ํ•˜๋ฉด ๋˜๊ฒ ์ฃ ?


์ผ๋‹จ ์ž…๋ ฅ ๋ฐ›์€ ํŒŒ์ผ๋ช…์ด ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ํ™•์ธํ•ด์„œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.



public static void main(String[] args) {
		
	// 1. ์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
	if ( args.length == 0 ){
		System.out.println("์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค.");
		System.exit(0);
	}
}


์œ„ ์ฝ”๋“œ๋Š” args๋กœ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’์ด ์žˆ๋Š”์ง€ ์ŠคํŠธ๋ง ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๊ฒ€์‚ฌํ•ด์„œ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์ด ์—†์œผ๋ฉด ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜๋„๋ก ํ•ด๋†“์•˜์Šต๋‹ˆ๋‹ค.


์ž, ๊ทธ๋Ÿผ ์‹ค์ œ๋กœ ์ž…๋ ฅ ๋ฐ›์€ ๊ฐ’์ด ์žˆ์„ ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•  ๊นŒ์š”?


๊ทธ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์œผ๋กœ ํŒŒ์ผ์„ ์ฝ์–ด๋“ค์—ฌ์•ผ ๊ฒ ์ฃ ?


์ด์ œ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.



// 2. ์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ
File file = new File(args[0]);

if ( file.exists() ){

}


์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ํ•œ๊ฐœ๋ผ๋Š” ๊ฐ€์ • ํ•˜์— args๋ฐฐ์—ด์˜ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๊ฐ’๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


ํŒŒ์ผ์„ ์ž๋ฐ”์†Œ์Šค๋กœ ์ฝ์–ด๋“ค์ผ ๋•Œ๋Š” File ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. File ์ƒ์„ฑ์ž์— ํŒŒ์ผ๋ช…์„ ๋‹ด๊ณ ์žˆ๋Š” args[0] ์„ ์ „๋‹ฌํ•˜์—ฌ new ํ‚ค์›Œ๋“œ๋กœ ์ƒ์„ฑํ•˜๋ฉด ํ•ด๋‹น ์ด๋ฆ„์˜ ํŒŒ์ผ์„ File ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.


๊ทธ๋ฆฌ๊ณ  if ๋ฌธ์„ ํ†ตํ•ด์„œ ํ•ด๋‹น ํŒŒ์ผ๋ช…์„ ๊ฐ–๋Š” ํŒŒ์ผ์ด ์‹ค์ œ๋กœ ์‹œ์Šคํ…œ ์ƒ์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ์„ ํ•ฉ๋‹ˆ๋‹ค.


if ๋ฌธ ์•ˆ์—์„œ๋Š” ํ•ด๋‹น ํŒŒ์ผ๋ช…์„ ๊ฐ–๋Š” ํŒŒ์ผ์ด ์‹ค์ œ๋กœ ์กด์žฌํ–ˆ์„ ๋•Œ ํ•ด์•ผํ•  ์ผ์„ ์ฝ”๋“œ๋กœ ์ ์–ด์ฃผ๋ฉด ๋˜๊ฒ ์ฃ .


์šฐ๋ฆฌ๋Š” File  ๊ฐ์ฒด๋ฅผ BufferedReader ๋ฅผ ์ด์šฉํ•ด์„œ ์ข€๋” ๋‹ค๋ฃจ๊ธฐ ์‰ฌ์šด ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ€ ๊ฒ๋‹ˆ๋‹ค.


์ด์ œ if ๋ฌธ ์•ˆ์— ๋‹ค์Œ ์†Œ์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณผ๊นŒ์š”?



// 3. ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ฝ์–ด์„œ ์ถœ๋ ฅ BufferedReader br = new BufferedReader(new FileReader(file)); while( br.ready() ){ System.out.println(br.readLine()); } br.close();


์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด File ๊ฐ์ฒด์ธ file์„ FileReader๋กœ ๊ฐ์‹ผ ๋‹ค์Œ ๋‹ค์‹œ BufferedReader๋กœ ๊ฐ์ŒŒ์Šต๋‹ˆ๋‹ค.


์ด๋ ‡๊ฒŒ ํ•˜๋ฉด BufferedReader ์˜ readLine() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์†์‰ฝ๊ฒŒ ๋ผ์ธ ๋‹จ์œ„๋กœ ํŒŒ์ผ์„ ์ฝ์–ด๋“ค์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๋‚ด์šฉ์ด ์กด์žฌํ•˜๋Š”์ง€๋Š” br.reader() ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ณ  true๊ฐ€ ๋ฐ˜ํ™˜๋  ๊ฒฝ์šฐ์— ๊ณ„์†ํ•ด์„œ ํ•œ์ค„์”ฉ ์ฝ์–ด๋‚ด๋ ค๊ฐ€๋ฉด์„œ ์ถœ๋ ฅํ•˜๋„๋ก ๋กœ์ง์ด ๊ตฌ์„ฑ๋˜์–ด์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์ž, ๊ทธ๋Ÿผ ์ „์ฒด ์†Œ์Šค๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ณด๋„๋ก ํ•˜์ฃ .



public class ReadFileAndPrint {     public static void main(String[] args) throws IOException {

// 1. ์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ if ( args.length == 0 ){ System.out.println("์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค."); System.exit(0); } // 2. ์ž…๋ ฅ๋ฐ›์€ ํŒŒ์ผ์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ File file = new File(args[0]); if ( file.exists() ){     // 3. ํŒŒ์ผ์ด ์กด์žฌํ•˜๋ฉด, ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ฝ์–ด์„œ ์ถœ๋ ฅ     BufferedReader br = new BufferedReader(new FileReader(file));     while( br.ready() ){ System.out.println(br.readLine());     }     br.close(); }

}



์ด์ œ ์ž๋ฐ” ํด๋ž˜์Šค ํŒŒ์ผ์„ ์ปดํŒŒ์ผํ•˜๊ณ  ์‹คํ–‰ํ•  ๋•Œ ์ธ์ž๋กœ ์›ํ•˜๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ์ ˆ๋Œ€๊ฒฝ๋กœ๋ช…์„ ๋„ฃ์–ด์ฃผ์„ธ์š”. 


์ƒ๋Œ€๊ฒฝ๋กœ๋ช…์„ ๋„ฃ์„ ๊ฒฝ์šฐ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ํŒŒ์ผ์„ ๋ชป ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


ํŒŒ์ผ์˜ ๋‚ด์šฉ์ด ์ž˜ ์ถœ๋ ฅ๋œ๋‹ค๋ฉด ์ž˜ ๋”ฐ๋ผ์˜ค์‹ ๊ฒ๋‹ˆ๋‹ค.


์–ด๋•Œ์š” ? ๋ณ„๋กœ ์–ด๋ ต์ง€ ์•Š์ฃ ??


์ด์ƒ์œผ๋กœ ์ž๋ฐ”๋กœ ํ…์ŠคํŠธ ํŒŒ์ผ ์ฝ์–ด์„œ ์ถœ๋ ฅํ•˜๊ธฐ ํฌ์ŠคํŒ…์„ ๋งˆ์น˜๊ฒ ์Šต๋‹ˆ๋‹ค.


๊ถ๊ธˆํ•˜์‹  ์  ์žˆ์œผ๋ฉด ๋Œ“๊ธ€๋‹ฌ์•„์ฃผ์„ธ์š” ^-^

์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๋‹ค๋ณด๋ฉด ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์˜ root ๊ฒฝ๋กœ๋ฅผ ํ•„์š”๋กœ ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค.


๊ทธ๋Ÿด ๊ฒฝ์šฐ ์•„๋ž˜์ฒ˜๋Ÿผ 


public static void main(String[] args) {
       
      System.out.println("Current Working Directory = " + System.getProperty("user.dir"));
}

System.getProperty ๋ฉ”์„œ๋“œ์— user.dir ์„ ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์˜ root ๊ฒฝ๋กœ(absolute path)๋ฅผ String ์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.


Use 'System.getProperty("user.dir")' method for getting current working directory as absolute path. The method returns a string value of the root path of the java project as an absolute path.



์•ˆ๋…•ํ•˜์„ธ์š” ์ผ€์ด์น˜์ž…๋‹ˆ๋‹ค. 


์˜ค๋Š˜์€ ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ํฌ์ŠคํŒ…ํ•ฉ๋‹ˆ๋‹ค.



1. Object to byte[]


ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
  out = new ObjectOutputStream(bos);   
  out.writeObject(yourObject);
  byte[] yourBytes = bos.toByteArray();
  ...
} finally {
  try {
    if (out != null) {
      out.close();
    }
  } catch (IOException ex) {
    // ignore close exception
  }
  try {
    bos.close();
  } catch (IOException ex) {
    // ignore close exception
  }
}


2. byte[] to Object


ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = null;
try {
  in = new ObjectInputStream(bis);
  Object o = in.readObject(); 
  ...
} finally {
  try {
    bis.close();
  } catch (IOException ex) {
    // ignore close exception
  }
  try {
    if (in != null) {
      in.close();
    }
  } catch (IOException ex) {
    // ignore close exception
  }
}


๊ธฐ๋ณธ์ ์ธ ์ž๋ฐ”๋ฅผ ์ด์šฉํ•˜๋ฉด ์ €๋ ‡๊ฒŒ ํ•˜์‹œ๋ฉด ๋˜๋Š”๋ฐ ์•„ํŒŒ์น˜ ์œ ํ‹ธ์„ ์ด์šฉํ•˜๋ฉด ์ข€ ๋” ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


ApacheUtils ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์šด ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.



To Serialize:

byte[] data = SerializationUtils.serialize(yourObject);

To Deserialize:

YourObject yourObject = (YourObject) SerializationUtils.deserialize(byte[] data)
๋‹ฌ๋ž‘ ํ•œ์ค„๋กœ Object -> byte[] ๋„ ๊ฐ€๋Šฅํ•˜๊ณ  ์—ญ์œผ๋กœ๋„ ๊ฐ€๋Šฅํ•˜๋„ค์š”.

์ฐธ ๊ฐ„๋‹จํ•˜์ฅฌ์ž‰~?

์ด์ƒ ์ผ€์ด์น˜์˜€์Šต๋‹ˆ๋‹ค.

์ฆํ”„ํ•˜์„ธ์š”~ 




1. ๊ฐœ์š”
Cass Loader ๋ž€ abstrace class๋กœ์จ Bytecode๋ฅผ ์ฝ์–ด ๋“ค์—ฌ์„œ class ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š”
์—ญํ™œ์„ ๋‹ด๋‹นํ•œ๋‹ค.

Class Loader๊ฐ€ Class๋ฅผ Loadingํ•˜๋Š” ์‹œ์ ์€ ComplieTime์ด ์•„๋‹Œ Run Time์— Loading์ด ๋œ๋‹ค.

ํ”„๋กœ๊ทธ๋ž˜๋จธ ๊ฐ€ SampleTest aaa = new SampleTest(); ๋ผ๋Š” ์ฝ”๋“œ๋ฅผ ์ฒ˜์Œ ์‹คํ–‰ํ•˜๋ฉด
JVM์€ SampleTest๋ผ๋Š” Class๋ฅผ Class Loader๋ฅผ ํ†ตํ•ด์„œ SampleTest.clas์˜ ByteCode๋ฅผ
์ตœ์ดˆ๋กœ ๋ฉ”๋ชจ๋ฆฌ์— Loadํ•˜๊ฒŒ ๋œ๋‹ค. 



2. ClassLoader ๊ธฐ์ˆ ์  ํŠน์ง•
   - Hierarchical 
     Class Loader๋Š” Hierarchical ํ•˜๊ฒŒ ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๊ณ , Parent class laoder์—์„œ, child class loader๋ฅผ
     ๊ฐ–๋Š”๊ฒƒ๊ณผ ๊ฐ™์ด Class Loader๊ฐ„์˜ ๊ณ„์ธตํ˜• ๊ตฌ์กฐ๋ฅผ ๊ฐ–๊ฒŒ ๋œ๋‹ค. ์ตœ์ƒ์œ„ Class Loader๋Š”  
     "bootstrap"   Class Loader๊ฐ€ ์œ„์น˜ ํ•˜๊ฒŒ ๋œ๋‹ค.

   - Delegate load request
      Class loading์—๋Š” ์ผ์ •ํ•œ ๊ทœ์น™์ด ํ•„์š”ํ•˜๋‹ค. ์ด๋Š” Class Loader๊ฐ€ ๊ณ„์ธกํ˜•์˜ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ 
      ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์–ด๋Š ์‹œ์ ์— Class ๋กœ๋”ฉ ์š”์ฒญ์„ ๋ฐ›์œผ๋ฉด ์ƒ์œ„ Class Loader๊ฐ€ Lodingํ•œ Class๊ฐ€ 
      ์šฐ์„ ๊ถŒ์„ ๊ฐ€์ง„๋‹ค. 

   - Have visibility constraint
     Child Class Loader๋Š” Parent Class Loader์˜ Clss๋ฅผ ์ฐพ์„์ˆ˜ ์žˆ์ง€๋งŒ, ๊ทธ ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

   - Cannot unload classes
      Class Loader์— ์˜ํ•ด์„œ Loading๋œ Class๋“ค์€ unload ๋ ์ˆ˜๊ฐ€ ์—†๋‹ค.



3. Class Loader ์œ„์ž„
   -

์˜†์˜ ๊ทธ๋ฆผ์€ class loader delegation model ์ด๋‹ค.
     ์ด๋Š” Loading์š”์ฒญ์„ ์„œ๋กœ ์—๊ฒŒ
     ์ „๋‹ฌ ํ•˜๋Š” ClassLoader์˜ ๊ทธ๋ž˜ํ”„
     ์ด๋‹ค. 

     ์˜†์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด Class Loader
     ๋“ค์€ ํ•˜๋‚˜์˜ delegation parent์™€
     ํ•จ๊ป˜์ƒ์„ฑ๋˜๊ณ  ๋‹ค์Œ ์œ„์น˜ ์—์„œ
     Class๋ฅผ ์ฐพ๋Š”๋‹ค.

     * ๋‹ค์Œ ์œ„์น˜๋ž€
      Cache, Parent, Self

    CalssLoader๊ฐ€ ์ตœ์ดˆ ํ•˜๋Š” ์ผ์€
    ์ „์—๋„ ๊ฐ™์€ Class๋ฅผ Loading
    ํ•˜๋„๋ก ์š”์ฒญ์„ ๋ฐ›์•˜๋Š”์ง€๋ฅผ
    ๋จผ์ € ํŒŒ์•…ํ•œ๋‹ค.

    ๋งŒ์•ฝ ์š”์ฒญ์„ ๋ฐ›์€์ ์ด ์žˆ์œผ๋ฉด,
    Cache์— ์ €์žฅ๋œ Class๋ฅผ
    ๋ฆฌํ„ด ํ•œ๋‹ค.

    ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ,
    Parent Class๋ฅผ Loading ํ• 
    ๊ธฐํšŒ๋ฅผ ์ค€๋‹ค.

    ์ด๋Ÿฌํ•œ ๋‘ ๋‹จ๊ณ„๋“ค์ด ๋ฐ˜๋ณต๋˜๊ณ  Depth First ๋ฐฉ์‹์œผ๋กœ ํƒ์ƒ‰์ด ์ง„ํ–‰ ๋œ๋‹ค.

    ๋งŒ์•ฝ Parent Class์—๋„ ํ•ด๋‹น Class๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด (ClassNotFoundException)
    ClassLoader๋Š” Class์˜ ์†Œ์Šค์— ๋Œ€ํ•œ ๊ณต์œ (์ž‘์—…ํ•œ ์ฝ”๋“œ)์˜ ๊ฒฝ๋กœ (Self)๋ฅผ ๊ฒ€์ƒ‰ํ•œ๋‹ค.

    Parent ClassLoader๋Š” Class๋ฅผ ์ฒ˜์Œ์œผ๋กœ loadingํ•  ๊ธฐํšŒ๊ฐ€ ์ฃผ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— Root์—
    ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ClassLoader์— ์˜ํ•ด ๋กœ๋”ฉ ๋œ๋‹ค.

   ๊ฐ€์žฅ ํ•ต์‹ฌ์ ์ธ bootstrap class๋“ค์€ java.lang.Object๊ฐ™์€ ์ •ํ™•ํ•œ ํด๋ž˜์Šค ๋ฒ„์ „์ด ๋กœ๋”ฉ๋˜์—ˆ๋Š”์ง€๋ฅผ
   ํ™•์ธํ•˜๊ณ , Class Loader๊ฐ€ ๊ทธ ์ž์ฒด ๋˜๋Š” ๋ถ€๋ชจ(Parent)์— ์˜ํ•ด์„œ Loading ๋˜์—ˆ๋Š”์ง€๋ฅผ
   Class๊ฐ€ ๋ถˆ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ์œ„์—์„œ ๋งํ–ˆ๋‹ค ์‹ถ์ด ์ž์‹์— ์˜ํ•ด Loading๋œ Class ๋“ค์€ ๋ณผ ์ˆ˜ ์—†๋‹ค. 


   - Bootstrap Class Loader
   ๋‹ค๋ฅธ ClassLoader๋“ค๊ณผ๋Š” ๋‹ฌ๋ฆฌ Bootstrap Class Loader ๋Š” ์ž๋ฐ” ์ฝ”๋“œ์— ์˜ํ•ด์„œ
   instanceํ™” ๋  ์ˆ˜ ์—†๋‹ค
   Bootstrap ClassLoader ๋Š” Boot ClassPath์—์„œ ํ•ต์‹ฌ ์‹œ์Šคํ…œ Class๋“ค์„ ๋กœ๋”ฉํ•˜๋Š”๋ฐ
   ์ด๊ฒƒ์€ ์ผ๋ฐ˜์ ์œผ๋กœ JavaHome์˜ jre/lib ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” JAR ํŒŒ์ผ๋“ค์ด๋‹ค.
   ๋‹จ -Xbootclasspath ์˜ต์…˜์œผ๋กœ classpath๋ฅผ ์ถ”๊ฐ€ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.


   - Extension Class Loader
   ์ฃผ๋กœ jre/lib/ext ๋””๋ ‰ํ† ๋ฆฌ์— ์œ„์น˜ํ•œ Extension ๋””๋ ‰ํ† ๋ฆฌ์—์„œ Class ๋“ค์„ Loading ํ•œ๋‹ค. 
   ์‚ฌ์šฉ์ž์˜ classpath๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  ๋‹ค์–‘ํ•œ ๋ณด์•ˆ ํ™•์žฅ ๊ฐ™์€ ์ƒˆ๋กœ์šด ํ™•์ž์œผ๋กœ ์‰ฝ๊ฒŒ
   ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณต ํ•œ๋‹ค.

   - System Class Loader 
   CLASSPATH ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ์ง€์ •๋œ ๊ฒฐ๋กœ์—์„œ ์ฝ”๋“œ๋ฅผ ๋กœ๋”ฉ ํ•œ๋‹ค. 
   ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด Class Loader๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ƒ์„ฑํ•œ ๋ชจ๋“  ClassLoader์˜ Parent ์ด๋‹ค. 


  
4. Class Loding ๋‹จ๊ณ„ 
   - Class Loadig์€ Loading, Linking, initialzing ๋‹จ๊ณ„๋กœ ๋‚˜๋ˆ„์–ด ์ง„๋‹ค.

   - ๋Œ€๋ถ€๋ถ„์˜ Class Loading๊ณผ ๊ด€๋ จ๋œ ๋ฌธ์ œ๋Š” ์ด ์„ธ๋‹จ๊ณ„ ์ค‘ ํ•œ ๋‹จ๊ณ„์—์„œ ๋ฐœ์ƒํ•œ๋‹ค.

   - ๋กœ๋”ฉ ๋‹จ๊ณ„๋Š” ํ•„์š”ํ•œ Class File์„ ๋ฐฐ์น˜ํ•˜๊ณ  ByteCode๋กœ Loadingํ•˜๋Š” ๋‹จ๊ณ„ ์ด๋‹ค.

   - JVM์˜ ๋กœ๋”ฉ ํ”„๋กœ์„ธ์Šค๋Š” Class ๊ฐ์ฒด์— ๋งค์šฐ ๊ธฐ๋ณธ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
     ๋ฉ”์†Œ๋“œ, ํ•„๋“œ, ๊ธฐํƒ€ ์ฐธ์กฐ Class ๋“ค์ด ์ด ๋‹จ๊ณ„์—์„œ๋Š” ๋‹ค๋ฃจ์–ด์ง€์ง€ ์•Š๋Š”๋‹ค.

 
   - Linking์€ ์„ธ ๋‹จ๊ณ„ ์ค‘์—์„œ ๊ฐ€์žฅ ๋ณต์žกํ•œ ๋‹จ๊ณ„์ด๋‹ค. ๋‹ค๋ฆ„๊ณผ ๊ฐ™์ด ์„ธ ๊ฐœ์˜ ์ฃผ์š” ๋‹จ๊ณ„๋“ค๋กœ ๋‚˜๋‰œ๋‹ค. 
     ใ„ฑ. ByteCode ํ™•์ธ - ByteCode๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ ํ•˜๋Š”์ง€ ํ™•์ธ ํ•œ๋‹ค. 
     ใ„ด. Class Preparation - Class ์—์„œ ์ •์˜๋œ ํ•„๋“œ, ๋ฉ”์†Œ๋“œ, ์ธํ„ฐํŽ˜์ž‡ ๋“ค์„ ๋‚˜ํƒ€๋‚ด๋Š” 
                                       ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์ค€๋น„ํ•œ๋‹ค. 
     ใ„ท. Resolving - Class ์— ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ์ฐธ์กฐ๋œ ๋ชจ๋“  Class๋“ค์„ Loadingํ•œ๋‹ค. 
                         - super class, interface, fild, method signature, local fild

   - initialize ๋‹จ๊ณ„ ๋™์•ˆ, Class ๋‚ด์— ํฌํ•จ๋œ ์ •์  initializer๋“ค์ด ์‹คํ–‰๋œ๋‹ค. ์ด ๋‹จ๊ณ„์˜ ๋์— ๊ฐ€์„œ๋Š” 
     ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™” ๋œ๋‹ค. 

   - ์ด ์„ธ ๋‹จ๊ณ„๋ฅผ ์ง€๋‚˜๋ฉด Class๋Š” ์™„์ „ํžˆ ๋กœ๋”ฉ๋˜๊ณ  ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋œ๋‹ค. 
     Class Loading์€ Lazy๋ฐฉ์‹์œผ๋กœ ์ˆ˜ํ–‰ ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ถ€ Class Loading Processor ๋Š” 
     Loading ์‹œ์ ์™ธ์— ์ฒ˜์Œ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ˆ˜ํ–‰๋  ์ˆ˜๋„ ์žˆ๋‹ค.   


5. Explicit Loading vs Implicit Loading
   - ํด๋ž˜์Šค๊ฐ€ ๋กœ๋”ฉ๋˜๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ์‹์˜ ์ฐจ์ด
   - Explicit : ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ๋“ค ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ๋”ฉ๋  ๋•Œ ๋ฐœ์ƒํ•œ๋‹ค.
                  - (java.lang.ClassLoader).loadClass()
                  - Class.forName()
   - ์ด๋Ÿฌํ•œ ๋ฉ”์†Œ๋“œ๋“ค ์ค‘ ํ•˜๋‚˜๊ฐ€ ํ˜ธ์ถœ ๋˜๋ฉด ์ด๋ฆ„์ด ์ธ์ž๋กœ ์ง€์ •๋œ ํด๋ž˜์Šค๊ฐ€ ํด๋ž˜์Šค ๋กœ๋”์— ์˜ํ•ด์„œ
     ๋กœ๋”ฉ
๋œ๋‹ค. 
     ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ, ๋กœ๋”๋Š” ํด๋ž˜์Šค๋ฅผ ๋กœ๋”ฉํ•˜๊ธฐ ์œ„ํ•ด delegation model์„ ์‚ฌ์šฉํ•œ๋‹ค.
 
   - Implicit : Class ๊ฐ€ ๋ ˆํผ๋Ÿฐ์Šค, ์ธ์Šคํ„ด์Šคํ™”, ๋ช…์‹œ์  ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์„ ์ œ์™ธํ•œ ์ƒ์†์˜ ๊ฒฝ์šฐ ๋กœ๋”ฉ๋˜๋Š” ๋ฐฉ์‹
                   ์ด ๊ฒฝ์šฐ ์Šฌ๊ทธ๋จธ๋‹ˆ ๋กœ๋”ฉ์ž‘์—…์€ ์ดˆ๊ธฐํ™”๋˜๊ณ  JVM์€ ํ•„์š”ํ•œ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๊ฒฐ์ •ํ•˜์—ฌ
                   ๋งŒ์•ฝ ๋กœ๋”ฉ์ด ๋˜์–ด ์žˆ์œผ๋ฉด ๋ ˆํผ๋Ÿฐ์Šค๋งŒ ๋ฆฌํ„ด๋˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ธ๋ฆฌ๊ฒŒ์ด์…˜ ๋ชจ๋ธ์„
                   ์‚ฌ์šฉํ•˜์—ฌ ํด๋ž˜์Šค๋ฅผ ๋กœ๋”ฉํ•˜๊ฒŒ ๋œ๋‹ค. 

   


์ฐธ๊ณ  ์ž๋ฃŒ:
http://www.ibm.com/developerworks/java/library/j-dclp1/index.html?S_TACT=105AGX02&S_CMP=EDU
๋ฐ ์ธํ„ฐ๋„ท ๋ฌธ์„œ



์ถœ์ฒ˜ : http://liebus.tistory.com/30



์ž๋ฐ”๋Š” JVM์„ ์‹œ์Šคํ…œ ์œ„์— ์˜ฌ๋ ค์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ”Œ๋žซํผ์— ๋…๋ฆฝ์ ์ž…๋‹ˆ๋‹ค.


๊ทธ๋Ÿฐ๋ฐ ๋ผ์ธ๊ตฌ๋ถ„์ž๋Š” ์œˆ๋„์šฐ ๊ณ„์—ด์ด๋ƒ ์œ ๋‹‰์Šค ๊ณ„์—ด์ด๋ƒ์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.


์ด๋Ÿด๋•Œ ์ž๋ฐ”์—์„œ๋Š” ์‹œ์Šคํ…œ ํ”„๋กœํผํ‹ฐ๋กœ ๋ผ์ธ๊ตฌ๋ถ„์ž๊ฐ€ ๋ญ”์ง€๋ฅผ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


System.getProperty("line.separator");

์œ„ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ˜„์žฌ ์‹œ์Šคํ…œ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ผ์ธ๊ตฌ๋ถ„์ž๋ฅผ String ์œผ๋กœ ์–ป์–ด์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์ž๋ฐ”์—๋Š” Dateํด๋ž˜์Šค๊ฐ€ ๋‘ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค.

java.util.Date์™€ java.sql.Date. ์ด ๋‘ ๊ฐ€์ง€ ์ฐจ์ด์ ๋„ ์•Œ์•„๋ณด๊ณ  ํ˜„์žฌ ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ„๋„ ๊ตฌํ•ด๋ณด์ž.

์•„๋ž˜ ์†Œ์Šค๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์†Œ์Šค์ด๋‹ค.

 

public class SimpleDateTest {
    public static void main(String[] args) {
         

        // 1. util.Date๋กœ ์–ป์–ด์˜จ ํ˜„์žฌ์‹œ๊ฐ„์„ SimpleDateFormat์„ ์ด์šฉํ•ด์„œ ์—ฐ๋„, ์›”, ์ผ, ์‹œ๊ฐ„, ๋ถ„, ์ดˆ ๊นŒ์ง€ ํ‘œํ˜„ํ•˜๋„๋ก ํ•ด๋ณด์•˜๋‹ค. ๋ถ‰์€ ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„์€ ์ž์‹ ์ด ์›ํ•˜๋Š” ํฌ๋งท์œผ๋กœ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์˜ˆ๋ฅผ๋“ค๋ฉด "yy/MM/dd"์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋ง์ด๋‹ค.
        String currentDate = new SimpleDateFormat("yyMMddhhmmss").format(new java.util.Date());
        System.out.println(currentDate);

 

        // 2. sql.Date๋กœ ์–ธ์–ด์˜จ ํ˜„์žฌ์‹œ๊ฐ„์„ 1๋ฒˆ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ถœ๋ ฅํ•ด๋ณธ๋‹ค. sql.Date๋Š” ์ƒ์„ฑ์‹œ longํƒ€์ž…์˜ ์‹œ๊ฐ„๊ฐ’์„ ์š”๊ตฌํ•œ๋‹ค. System.currentTimeMillis()๋Š” ํ˜„์žฌ์‹œ๊ฐ„์„ longํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฏ€๋กœ ์ด๋ฅผ ์ด์šฉํ–ˆ๋‹ค.  

        currentDate = new SimpleDateFormat("yyMMddhhmmss").format(

                                                    new java.sql.Date(System.currentTimeMillis()));
        System.out.println(currentDate); 


        // 2. util.Date๋ฅผ ์ด์šฉํ•˜์—ฌ ์–ป์–ด์˜จ  Date
        Date date = new java.util.Date();
        System.out.println(date);

 

        // 3. sql.Date๋ฅผ ์ด์šฉํ•˜์—ฌ ์–ป์–ด์˜จ Date
        Date sqlDate = new java.sql.Date(System.currentTimeMillis());
        System.out.println(sqlDate);
    }


๋ฃจํ”„๋ฅผ ํ•œ๋ฒˆ ๋งŒ ์‚ฌ์šฉํ•œ ์ž๋ฐ” ๊ตฌ๊ตฌ๋‹จ ์†Œ์Šค


์ž๋ฐ”๋กœ ๊ตฌ๊ตฌ๋‹จ ์†Œ์Šค๋ฅผ ์ž‘์„ฑํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

continue, break๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ๋ฃจํ”„๋„ ์ค‘๋ณต ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ•˜๋‚˜์˜ loop๋ฌธ์„ ์‚ฌ์šฉํ•œ ๊ตฌ๊ตฌ๋‹จ์ž…๋‹ˆ๋‹ค. 


Below java source code prints out multiplication table. 

It does not use nested-loop. It only uses one for-loop. 



public class gugudan {

public static void main(String args[]){

     System.out.println("๊ตฌ๊ตฌ๋‹จ์ž…๋‹ˆ๋‹ค.");
     int i = 1,b = 1;

     while ( i < 10 ) {
          if( b == 10 ) {

              i+=1; 

              b=1;

          }
          if ( i != 10 ) { 

              System.out.println(i+"*"+b+"="+i*b);
              b++;
          }
     }
}

}


์›๋ณธ ๋ฌธ์ œ๋Š” ์ด๊ณณ์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


๊ฐ„๋žตํ•˜๊ฒŒ ํ•œ๊ธ€๋กœ ๋ฌธ์ œ๋ฅผ ๋ฒˆ์—ญํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.


int[]๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ 3 ๋ฐ”๋กœ ๋’ค์— 4๋ฅผ ์œ„์น˜ํ•˜๋„๋ก ์ •๋ ฌํ•œ ๋’ค ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

์ธ์ž๋กœ ๋“ค์–ด์˜จ int[]์—์„œ ์ˆซ์ž 3๊ณผ ์ˆซ์ž4์˜ ๊ฐœ์ˆ˜๋Š” ํ•ญ์ƒ ๋™์ผํ•˜๋ฉฐ, ์ˆซ์ž 3 ๋’ค์—๋Š” ํ•ญ์ƒ 3๊ณผ 4๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ์ˆ˜๊ฐ€ ๋“ค์–ด์˜ต๋‹ˆ๋‹ค. ์ˆซ์ž3์€ ์ •๋ ฌํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ๋‹ค๋ฅธ ๋ชจ๋“  ์ˆ˜ ๋“ค์€ ์œ„์น˜๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ฒซ ์ˆซ์ž 3์€ ํ•ญ์ƒ ์ˆซ์ž 4๋ณด๋‹ค ๋จผ์ € ์ถœํ˜„ํ•ฉ๋‹ˆ๋‹ค.


๋ณดํ†ต int[]๋ณด๋‹ค๋Š” Listํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ๊ฐ€ ์‰ฝ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” api๊ฐ€ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .

๊ทธ๋ž˜์„œ List๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

์šฐ์„  int[] ๋ฅผ ๋ฃจํ”„๋ฅผ ๋Œ๋ ค์„œ (3 ์ด ์ถœํ˜„ํ•˜๋Š” ์œ„์น˜ + 1)๊ณผ (4์˜ ์œ„์น˜)๊ฐ€ ์–ด๋””์ธ์ง€๋ฅผ ๋‘๊ฐœ์˜ ArrayList์— ๋‚˜๋ˆ ์„œ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค.

์–ด์ฐจํ”ผ 3๊ณผ 4์˜ ์ถœํ˜„ ๋นˆ๋„๊ฐ€ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‘๊ฐœ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋Š” ํ•ญ์ƒ ๋™์ผํ•˜๊ฒ ์ฃ .

๊ทธ๋ ‡๊ฒŒ ๋ฃจํ”„๋ฅผ ํ•œ๋ฒˆ ๋Œ๋ฉด์„œ ์œ„์น˜์ •๋ณด๋ฅผ ๊ฐ–๊ณ ์žˆ๋‹ค๊ฐ€, ์œ„์น˜์ •๋ณด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฃจํ”„๋Œ๋ฉด์„œ ํ•ด๋‹น ์œ„์น˜์˜ ์ˆซ์ž๋“ค์˜ ์œ„์น˜๋ฅผ ์„œ๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์ฃ . ( 3์˜ ์œ„์น˜ +1 )์„ ์ €์žฅํ•˜๋Š” ์ด์œ ๋Š” 3 ๋ฐ”๋กœ ๋‹ค์Œ์˜ ์ˆซ์ž์™€ 4์˜ ์œ„์น˜๋ฅผ ์„œ๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.


์ €๋Š” ์—ฌ๊ธฐ์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‘๊ฐœ๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ๋ฆฌ์ŠคํŠธํ•˜๋‚˜์— ๋งต๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งต์—๋Š” ( 3์˜ ์œ„์น˜+1)๊ณผ 4์˜ ์œ„์น˜๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒ ์ฃ .


public int[] fix34(int[] nums) {
  List<Integer> posOfFour = new ArrayList<Integer>();
  List<Integer> posOfThree = new ArrayList<Integer>();
  for ( int i = 0; i < nums.length; i++ ){
      if ( nums[i] == 4 ){
        posOfFour.add(i);
      }
      if ( nums[i] == 3 ){
         posOfThree.add(i+1);
      }
  }
  for ( int j = 0; j < posOfThree.size(); j++ ){
     int temp = nums[(int)posOfThree.get(j)];
     nums[(int)posOfThree.get(j)] = nums[(int)posOfFour.get(j)];
     nums[(int)posOfFour.get(j)] = temp;
  }
  return nums;
}

๋ฌธ์ œ๋Š” ์ด๊ณณ์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด int[]๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ span์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.


์šฐ์„  span์ด๋ผ๋Š” ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ์•Œ์•„์•ผ ํ•˜๋Š”๋ฐ์š”.

๋ฌธ์ œ์—์„œ๋Š” span์ด๋ผ๋Š” ๊ฒƒ์„ "๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ํŠน์ • ๊ฐ’ a์™€ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ๋™์ผํ•œ ๊ฐ’ a ์‚ฌ์ด์˜ ๊ธธ์ด" ๋ผ๊ณ  ์„ค๋ช…ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์ด ๊ธธ์ด์—๋Š” ๊ฐ€์žฅ ์™ผ์ชฝ๊ฐ’๊ณผ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ๊ฐ’์˜ ์œ„์น˜๋ฅผ ํฌํ•จํ•œ ๊ธธ์ด์ž…๋‹ˆ๋‹ค.


์ฆ‰, int[ 1, 2, 1 ] ์„ ์ธ์ž๋กœ ๋ฐ›์•˜์„ ๊ฒฝ์šฐ 1์— ๋Œ€ํ•œ span์€ 3์ด ๋ฉ๋‹ˆ๋‹ค.

๊ฐ€์žฅ ์™ผ์ชฝ์˜ 1๊ณผ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์˜ 1์„ ํฌํ•จํ•œ ๊ธธ์ด๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด์ฃ .

๋งŒ์•ฝ int[ 1, 2, 1, 3, 4, 2 ]๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•˜๋‹ค๋ฉด,


1์— ๋Œ€ํ•œ span = 3

2์— ๋Œ€ํ•œ span = 5

3์— ๋Œ€ํ•œ span = 1

4์— ๋Œ€ํ•œ span = 1


์ด ๋ฉ๋‹ˆ๋‹ค.


์ด๋ ‡๊ฒŒ ๊ณ„์‚ฐํ–ˆ์„ ๋•Œ maxSpan์€ 2์— ๋Œ€ํ•œ span๊ฐ’์ธ 5๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์ œ๊ฐ€ ํ’€์–ด๋ณธ ํ•ด๋‹ต์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ญ ๊ทธ๋ƒฅ ๋ฃจํ”„๋ฅผ ์ค‘๋ณต์œผ๋กœ ๋Œ๋ ค์„œ ํ•ด๊ฒฐํ–ˆ๋Š”๋ฐ ์ด๊ฑด ์ •๋ง ์ตœ์•…์˜ ํ•ด๋‹ต์ด ๋˜๊ฒ ์ฃ .

์™ ๋งŒํ•˜๋ฉด ์ค‘๋ณต ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค. ์†๋„์ €ํ•˜๊ฐ€ ์ƒ๋‹นํ•˜๋‹ˆ๊นŒ์š”.

๋ฌธ์ œ์—์„œ ๋งํ•˜๊ธธ ์†๋„๋Š” ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค๊ณ  ํ•ด์„œ ๊ทธ๋ƒฅ ๋ฐ”๋กœ ์ƒ๊ฐ๋‚˜๋Š” ๋Œ€๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 O(n์ œ๊ณฑ)์ด ๋˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด๋ฏธ ์ฒดํฌํ–ˆ๋˜ ์ˆซ์ž์ธ์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.

์œ„์—์„œ ๋“  ์˜ˆ์ œ๋ฅผ ์ธ์šฉํ•ด์„œ ์„ค๋ช…๋“œ๋ฆฌ์ž๋ฉด,


int [ 1, 2, 1, 3, 4, 2 ]๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•˜์„ ๊ฒฝ์šฐ์— 1๊ณผ 2๋Š” ๊ฐ๊ฐ ๋‘๋ฒˆ์”ฉ ๋“ค์–ด๊ฐ€์žˆ๋Š”๋ฐ์š”

์ด๋•Œ ๋‘๋ฒˆ์งธ๋กœ ๋‚˜์˜ค๋Š” ์ˆซ์ž๋“ค์— ๋Œ€ํ•ด์„œ๋Š” ์‚ฌ์‹ค ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†์ฃ . ์ด๋ฏธ ์ฒซ๋ฒˆ์งธ๋กœ ๋‚˜์˜จ 1๊ณผ 2๋ฅผ ๋งŒ๋‚ฌ์„ ๋•Œ span๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ๋™์ผํ•œ ์ˆซ์ž๋“ค์ด ๋‘๊ฐœ ์ด์ƒ ๋‚˜์˜ค๊ฒŒ๋œ๋‹ค๋ฉด ์ด๋ฏธ ๊ฒ€์‚ฌํ–ˆ๋˜ ์ˆซ์ž๋“ค์— ๋Œ€ํ•œ span์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ถˆํ•„์š”ํ•œ ๊ณ„์‚ฐ์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ณ„์‚ฐ์„ ์—†์• ๊ธฐ ์œ„ํ•ด์„œ ํ•œ๋ฒˆ ์ฒดํฌํ–ˆ๋˜ ์ˆซ์ž์™€ ๋™์ผํ•œ ์ˆซ์ž๋ฅผ outer most for loop์—์„œ ๋งŒ๋‚˜๊ฒŒ๋˜๋ฉด ์ฒดํฌํ–ˆ๋˜ ์ˆซ์ž์ธ์ง€๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” first inner loop๋ฅผ ๋Œ๊ณ  ์ฒดํฌํ–ˆ๋˜ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ second inner loop์—์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


๋˜ํ•œ, second inner๋ฃจํ”„์—์„œ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๋•Œ์—๋„ int[]์˜ ํ˜„์žฌ ์ˆซ์ž์˜ ์œ„์น˜์—์„œ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ์ˆซ์ž๋“ค์„ ๊ฐ€์ง€๊ณ  ๋น„๊ต๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ์ขŒ์ธก์— ์žˆ๋Š” ์ˆซ์ž๋“ค ์ค‘์—๋Š” ์ด ์ˆซ์ž์™€ ๋™์ผํ•œ ์ˆซ์ž๊ฐ€ ์—†๋‹ค๋Š” ์ „์ œ๊ฐ€ ํ•„์š”ํ•œ๋ฐ ์ด ์ „์ œ๋ฅผ ๋ฐ”๋กœ ์œ„์— ์„ค๋ช…๋“œ๋ฆฐ first inner loop์—์„œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .


์ด๋Ÿฐ์‹์œผ๋กœ ํ•˜๊ฒŒ๋  ๊ฒฝ์šฐ ์ตœ์•…์˜ ๊ฒฝ์šฐ( int[]์— ๋“ค์–ด์žˆ๋Š” ์ˆซ์ž๊ฐ€ ๋ชจ๋‘ ๋‹ค๋ฅผ ๊ฒฝ์šฐ )์—๋„ O(n์ œ๊ณฑ) ๊นŒ์ง€๋Š” ์•ˆ๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


์ค‘๋ณต for loop๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋Š” ํ–ˆ์ง€๋งŒ ๋‚˜๋ฆ„ ์†๋„๋„ ์‹ ๊ฒฝ์„ ์“ด ๋กœ์ง์ด๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์ฃ .



public int maxSpan(int[] nums) {
  if ( nums.length == 0 ) return 0;
  int maxSpan = 1;
  int[] checkedNumbers = new int[nums.length];
  int checkedNumbersPos = 0;

 

  // Outer Most For Loop

  for ( int i = 0; i < nums.length ; i++ ){
     boolean checked = false;


     // First Inner Loop
     for( int checkedItem : checkedNumbers ){
        if ( checkedItem == nums[i] ){
           checked = true;
        }
     }
     if ( !checked ){
         checkedNumbers[checkedNumbersPos] = nums[i];


         // Second Inner Loop
         for ( int j = i + 1; j < nums.length ; j++ ){
            if ( nums[j] == nums[i] ){
               int tempSpan = j - i + 1;
               if ( tempSpan > maxSpan ){ maxSpan = tempSpan;}
            }
         }
     }
  }
  return maxSpan;
}

๋ฌธ์ œ๋Š” ์ด๊ณณ์— ์žˆ์Šต๋‹ˆ๋‹ค.


public int noTeenSum(int a, int b, int c) {
  int sum = 0;
  sum += fixTeen(a);
  sum += fixTeen(b);
  sum += fixTeen(c);
  return sum;
}
public int fixTeen(int n){
    if ( n > 12 && n < 20 ){
         if ( n != 15 && n != 16 ){
              return 0;
         }
    }
    return n;
}

์ฝ”๋”ฉ๋ฐ•์ฅ ๋‹ท์ปด์˜ ๋ฌธ์ œํ’€์ด์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ œ๊ฐ€ ์ง์ ‘ ์ฝ”๋”ฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.


๋ฌธ์ œ๋Š” ์ด๊ณณ์—์„œ ํ™•์ธ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


public int luckySum(int a, int b, int c) {

  if ( a == 13 ){ return 0;}
  if ( b == 13 ){ return a;}
  if ( c == 13 ){ return a+b;}
  return a + b + c;
}

์ด๋ฒˆ์—๋Š” ์ฝ”๋”ฉ๋ฐ•์ฅ ๋‹ท์ปด์˜ loneSum ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ๋Š” ์ด๊ณณ์— ๊ฐ€์‹œ๋ฉด ํ™•์ธ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


public int loneSum(int a, int b, int c) {
   int sum = 0;
   sum += a;
   if ( a == b ){
       sum -= b;
   }else{
       sum += b;
   }
   if ( a== c || b == c ){
       sum -= c;
   }else{

       sum+= c;

   }
   return ( sum < 0 ? 0 : sum );

}

[์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ]


์ด ๋ฌธ์ œ๋Š” ์ผ์ข…์˜ online judge ์‚ฌ์ดํŠธ์ธ ์ฝ”๋”ฉ๋ฑƒ(์ฝ”๋”ฉ๋ฐ•์ฅ) ๋‹ท์ปด์— ๋‚˜์™€์žˆ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๋ฌธ์ œ๋Š” ์ด๊ณณ์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


์•„๋ž˜ ํ•ด๋‹ต์€ ์ œ๊ฐ€ ์ง์ ‘ ์ฝ”๋”ฉํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.


public boolean makeBricks(int small, int big, int goal) {
  if ( (goal - small <= big*5) && ( goal%5 <= small ) ){
      return true;
  }
  return false;
}