ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ“Œ java.lang ํŒจํ‚ค์ง€

: ์ž๋ฐ” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ๊ฐ€์žฅ ๊ธฐ๋ณธ์ด ๋˜๋Š” ํด๋ž˜์Šค๋“ค์„ ํฌํ•จ

: import๋ฌธ ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ (์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ import๋ฌธ ์ถ”๊ฐ€)

https://docs.oracle.com/javase/8/docs/api/java/lang/package-summary.html

 

java.lang (Java Platform SE 8 )

Interface Summary  Interface Description Appendable An object to which char sequences and values can be appended. AutoCloseable An object that may hold resources (such as file or socket handles) until it is closed. CharSequence A CharSequence is a readabl

docs.oracle.com

 

โœ”๏ธ Object ํด๋ž˜์Šค

: ๋ชจ๋“  ํด๋ž˜์Šค์˜ ์ตœ๊ณ  ์กฐ์ƒ

https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html

 

Object (Java Platform SE 8 )

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup. The general contract of fi

docs.oracle.com

โ–ช๏ธ equls(Object obj)

: ์ฐธ์กฐ๋ณ€์ˆ˜ ๊ฐ’์œผ๋กœ ํŒ๋‹จ (๊ฐ์ฒด์˜ ๊ฒฝ์šฐ, ์ธ์Šคํ„ด์Šค ์ฃผ์†Œ๊ฐ’ ๋น„๊ต!)

: ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ’์ด ์•„๋‹Œ ์ €์žฅ๋œ ๋‚ด์šฉ์œผ๋กœ ํŒ๋‹จํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ!

 

โ–ช๏ธ hashCode()

: ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ’์œผ๋กœ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜

: ๋‹ค๋ฅธ ๊ฐ์ฒด๋Š” ๊ฐ™์€ ํ•ด์‹œ์ฝ”๋“œX in 32bit JVM, ๋‹ค๋ฅธ ๊ฐ์ฒด๋„ ๊ฐ™์€ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ๊ฐ€์งˆ์ˆ˜๋„ in 64bit JVM (32bit JVM์—์„œ 4byte์ฃผ์†Œ๊ฐ’์„ ์“ฐ์ง€๋งŒ, 64bit JVM์—์„œ๋Š” 8byte ์ฃผ์†Œ๊ฐ’์„ ์“ฐ๊ธฐ ๋•Œ๋ฌธ, ํ•ด์‹œ์ฝ”๋“œ๋Š” 4byte!)

โ• equls()๋ฅผ ๊นŠ์€ ๋ณต์‚ฌ๋กœ ๋น„๊ตํ•˜๊ณ  ์‹ถ์–ด์„œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ–ˆ๋‹ค๋ฉด, hashCode()๋„ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•ด์ฃผ์–ด์•ผ ํ•จ! (๊ฐ™์€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๊ฐ’์„ ๊ฐ€์ง€๋ฉด ๊ฐ™์€ ๊ฐ์ฒด๋กœ ๋ณด๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๊ธฐ ๋•Œ๋ฌธ์—)

 

โ–ช๏ธ toString()

: ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ œ๊ณต (๋ณดํ†ต ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์„œ ์‚ฌ์šฉ!)

: ๊ธฐ๋ณธ์€ ํด๋ž˜์Šค๋ช…@ํ•ด์‹œ์ฝ”๋“œ

Card@19e0bfd

โ— (์ฐธ๊ณ ) ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ๋•Œ, ์กฐ์ƒํด๋ž˜์Šค์— ์ •์˜๋œ ์ ‘๊ทผ๋ฒ”์œ„๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ๋„“์–ด์•ผ ํ•จ!

 

โ–ช๏ธ clone()

: ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์˜ ๊ฐ’๋งŒ ๋ณต์‚ฌ (์–•์€ ๋ณต์‚ฌ)

: ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด Cloneable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ (์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ๋ณดํ˜ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐœ๋ฐœ์ž๊ฐ€ clone์„ ํ—ˆ์šฉํ–ˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผํ•จ!)

int[] arr = {1, 2, 3, 4};
int[] arrCopy = arr.clone(); // int[] arrCopy = new int[arr.length]; System.arraycopy(arr, 0, arrCopy, 0, arr.length);

 

โ•๊ณต๋ณ€ํ™˜ ํƒ€์ž…

: JDK1.5๋ถ€ํ„ฐ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ๋•Œ ์กฐ์ƒ ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜ํƒ€์ž…์„ ์ž์† ํด๋ž˜์Šค์˜ ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝ ํ—ˆ์šฉ! (๋ฒˆ๊ฑฐ๋กœ์šด ํ˜•๋ณ€ํ™˜↓)

 

๐Ÿ™‹๐Ÿป‍โ™€๏ธ ๊นŠ์€ ๋ณต์‚ฌ? ์–•์€ ๋ณต์‚ฌ?

๐Ÿ’ก ์–•์€ ๋ณต์‚ฌ: ๋‹จ์ˆœํžˆ ๊ฐ์ฒด์— ์ €์žฅ๋œ ๊ฐ’(์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜)์„ ๊ทธ๋Œ€๋กœ ๋ณต์ œํ•  ๋ฟ, ๊ฐ์ฒด๊ฐ€ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๊นŒ์ง€ ๋ณต์ œX (์›๋ณธ๊ณผ ๋ณต์ œ๋ณธ์ด ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๊ณต์œ )

๐Ÿ’ก ๊นŠ์€ ๋ณต์‚ฌ: ์›๋ณธ์ด ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๊นŒ์ง€ ๋ณต์ œ (์›๋ณธ๊ณผ ๋ณต์ œ๋ณธ์ด ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ)

 

โ–ช๏ธ getClass()

: ์ž์‹ ์ด ์†ํ•œ ํด๋ž˜์Šค์˜ Class ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜

Class class = new Card().getClass(); // Card.class

 

๐Ÿ™‹๐Ÿป‍โ™€๏ธ ํด๋ž˜์Šค(Class) ๊ฐ์ฒด?

https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html

 

Class (Java Platform SE 8 )

Determines if the specified Class object represents a primitive type. There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types tha

docs.oracle.com

- ํด๋ž˜์Šค ๋‹น 1๊ฐœ๋งŒ ์กด์žฌ

- ํด๋ž˜์Šค ํŒŒ์ผ์ด ํด๋ž˜์Šค ๋กœ๋”(์‹คํ–‰ ์‹œ ํ•„์š”ํ•œ ํด๋ž˜์Šค๋ฅผ ๋™์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œํ•˜๋Š” ์—ญํ• )์— ์˜ํ•ด์„œ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐˆ ๋•Œ ์ž๋™์œผ๋กœ ์ƒ์„ฑ

- (1) ํด๋ž˜์Šค ๊ฐ์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ, (2) ์žˆ์œผ๋ฉด ๊ฐ์ฒด์˜ ์ฐธ์กฐ(์ฃผ์†Œ๊ฐ’) ๋ฐ˜ํ™˜ (3) ์—†์œผ๋ฉด ํด๋ž˜์Šค ํŒจ์Šค์— ์ง€์ •๋œ ๊ฒฝ๋กœ๋ฅผ ๋”ฐ๋ผ์„œ ํด๋ž˜์Šค ํŒŒ์ผ์„ ์ฐพ์•„ Class ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)๋กœ ๋ณ€ํ™˜ (๋ชป์ฐพ์œผ๋ฉด ClassNotFoundException ๋ฐœ์ƒ)

 

โœ”๏ธ String ํด๋ž˜์Šค

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

 

String (Java Platform SE 8 )

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argum

docs.oracle.com

 

โ— String์€ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•œ(immutable) ํด๋ž˜์Šค

: ํ•œ๋ฒˆ ์ƒ์„ฑ๋œ String ์ธ์Šคํ„ด์Šค์˜ ๋ฌธ์ž์—ด์€ ์ฝ์–ด ์˜ฌ ์ˆ˜๋งŒ ์žˆ๊ณ , ๋ณ€๊ฒฝํ•  ์ˆ˜๋Š” ์—†์Œ!

: '+' ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•˜๋Š” ๊ฒฝ์šฐ, ์ธ์Šคํ„ด์Šค ๋‚ด์˜ ๋ฌธ์ž์—ด์ด ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์ด ๋‹ด๊ธด String ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ!

: ๋”ฐ๋ผ์„œ, '+' ์—ฐ์‚ฐ์„ ํ•  ๋•Œ๋งˆ๋‹ค ๋ฉ”๋ชจ๋ฆฌ์— ์ƒˆ๋กœ์šด String ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์†๋„↓, ๋ฉ”๋ชจ๋ฆฌ ์ฐจ์ง€↑

๐Ÿ’ก ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…์ด ๋งŽ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด, String ํด๋ž˜์Šค ๋Œ€์‹  StringBuilder๋‚˜ StringBuffer ํด๋ž˜์Šค ์‚ฌ์šฉ!

 

๐Ÿ™‹๐Ÿป‍โ™€๏ธ ์ƒ์„ฑ์ž? ๋ฌธ์ž ๋ฆฌํ„ฐ๋Ÿด?

String s = new String("string"); //ํ•ญ์ƒ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
String s = "string"; //์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์žฌ์‚ฌ์šฉ (String์ด immutable์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅ!)

๐Ÿ’ก ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด(ํ•ด๋‹น ํด๋ž˜์Šค์˜ ๋ฆฌํ„ฐ๋Ÿด ๋ชฉ๋ก์— ์žˆ๋Š”)์€ ํด๋ž˜์Šค๊ฐ€ ํด๋ž˜์Šค ๋กœ๋”์— ์˜ํ•ด ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œ๋  ๋•Œ JVM๋‚ด์— ์žˆ๋Š” ์ƒ์ˆ˜ ์ €์žฅ์†Œ(constant pool)์— ์ €์žฅ

 

๐Ÿ™‹๐Ÿป‍โ™€๏ธ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜• → String

1๏ธโƒฃ ๊ธฐ๋ณธ์ž๋ฃŒํ˜• + ""

 

2๏ธโƒฃ String.valueOf()

int i = 100;
String s1 = i + "";
String s2 = Strig.valueOf(i);

 

๐Ÿ™‹๐Ÿป‍โ™€๏ธ  String ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•

1๏ธโƒฃ Integet.parseInt()

2๏ธโƒฃ Integer.valueOf()

int i = Integer.parseInt("100"); //int ๋ฆฌํ„ด
int i = Integer.valueOf("100"); //Integer ๋ฆฌํ„ด

: Integer ๋Œ€์‹  Boolean, Byte, Short, Long, Float, Double ๊ฐ€๋Šฅ!

 

โœ”๏ธ StringBuffer / StringBuilder ํด๋ž˜์Šค

https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html

 

StringBuffer (Java Platform SE 8 )

Inserts the string into this character sequence. The characters of the String argument are inserted, in order, into this sequence at the indicated offset, moving up any characters originally above that position and increasing the length of this sequence by

docs.oracle.com

: ๋‚ด๋ถ€ ๋ฌธ์ž์—ด์˜ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•œ ํด๋ž˜์Šค

: ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฌธ์ž์—ด ํŽธ์ง‘์„ ์œ„ํ•œ ๋ฒ„ํผ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ!

: ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃจ๋Š” ์ž‘์—…์„ ํ•  ๋•Œ, ๋ฒ„ํผ์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์—…ํ•˜๋ ค๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ณด๋‹ค ์ž‘์„ ๋•Œ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ์ž‘์—…์ด ์ˆ˜ํ–‰! (์ƒˆ๋กœ์šด ๊ธธ์ด์˜ ๋ฐฐ์—ด ์ƒ์„ฑ)

StringBuilder sb = new StringBuilder("string");
sb.append("Builder");
sb.toString(); //stringBuilder

๐Ÿ’ก StringBuffer์™€ StringBuilder๋Š” equals()๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜์ง€ ์•Š์•„์„œ ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ๋น„๊ตX (์ฐธ์กฐ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋น„๊ตO)

https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html

 

๐Ÿ™‹๐Ÿป‍โ™€๏ธ StringBuffer VS StringBuilder

๐Ÿ’ก StringBuffer๋Š” ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ์— ์•ˆ์ „(Thread safe)ํ•˜๋„๋ก ๋™๊ธฐํ™”๋˜์–ด ์žˆ๊ณ , StringBuilder๋Š” ์“ฐ๋ ˆ๋“œ ๋™๊ธฐํ™”X. ๊ฑฐ์˜ ๋™์ผํ•˜์ง€๋งŒ ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์ด ์•„๋‹ˆ๋ผ๋ฉด StringBuilder๊ฐ€ ๋” ์„ฑ๋Šฅ์ด ์ข‹๋‹ค!

 

โœ”๏ธ Math ํด๋ž˜์Šค

 

 

โœ”๏ธ ๋ž˜ํผ(Wrapper) ํด๋ž˜์Šค

: ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์„ ๊ฐ์ฒด๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค

: Boolean, Character, Byte, Short, Integer, Long, Float, Double

โ— ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์ด ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ๊ฒƒ์ด ์ž๋ฐ”๊ฐ€ ์™„์ „ํ•œ ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด๊ฐ€ ์•„๋‹Œ ์ด์œ (๊ฐ์ฒด์ง€ํ–ฅ์—์„œ ๋ชจ๋“  ๊ฒƒ์€ ๊ฐ์ฒด๋กœ ๋‹ค๋ฃจ์–ด์ง!) but ์„ฑ๋Šฅ์€ ๋” ์ข‹์Œ

 

- ๋ž˜ํผ ํด๋ž˜์Šค์˜ ๋น„๊ต๋Š” equals()์™€ compareTo()๋ฅผ ์‚ฌ์šฉ!

- ์ˆซ์ž์™€ ๊ด€๋ จ๋œ ๋ž˜ํผ ํด๋ž˜์Šค๋Š” Numberํด๋ž˜์Šค์˜ ์ž์†!

 

๐Ÿ™‹๐Ÿป‍โ™€๏ธ String → ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•

1๏ธโƒฃ ํƒ€์ž….parseํƒ€์ž…() ← ๊ธฐ๋ณธ ์ž๋ฃŒํ˜• ๋ฐ˜ํ™˜

2๏ธโƒฃ ํƒ€์ž….valueOf() ← ๋ž˜ํผ ํด๋ž˜์Šค ๋ฐ˜ํ™˜ (๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ›์œผ๋ฉด ์˜คํ† ๋ฐ•์‹ฑ์ด ๋จ!)

 

๐Ÿ™‹๐Ÿป‍โ™€๏ธ ์˜คํ† ๋ฐ•์‹ฑ? ์–ธ๋ฐ•์‹ฑ?

โ–ช๏ธ ์˜คํ† ๋ฐ•์‹ฑ : ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์„ ๋ž˜ํผ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋กœ ์ž๋™ ๋ณ€ํ™˜

โ–ช๏ธ ์–ธ๋ฐ•์‹ฑ : ๋ž˜ํผ ํด๋ž˜์Šค → ๊ธฐ๋ณธ ์ž๋ฃŒํ˜• 

Integer integer = 2; // ์˜คํ† ๋ฐ•์‹ฑ Integer integer = Integer.valueOf(2)
int i = integer; // ์–ธ๋ฐ•์‹ฑ int i = integer.intValue()

โ— ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์คŒ

 

๐Ÿ“Œ java.util ํŒจํ‚ค์ง€

โœ”๏ธ java.util.Objects ํด๋ž˜์Šค

https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html

 

Objects (Java Platform SE 8 )

Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[]). This method is useful for implementing Object.hashCode()

docs.oracle.com

: Math ํด๋ž˜์Šค์ฒ˜๋Ÿผ ๋ชจ๋“  ๋ฉค๋ฒ„๊ฐ€ static

: ๋„ ์ฒดํฌ์— ์œ ์šฉ

static boolean isNull(Object obj);
static boolean nonNull(Object obj);
static <T> requireNonNull(T obj) // ex. Objects.requireNonNull(name);

: Object ํด๋ž˜์Šค์™€ ๋‹ฌ๋ฆฌ equals()๋ฟ๋งŒ์ด ์•„๋‹ˆ๋ผ deepEquals(), compare() ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ(Null ์ฒดํฌ๋„ ์ž๋™์œผ๋กœ ํ•ด์คŒ)

โ• Object์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ์™€ ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋‚จ! (Objects.toString()๊ณผ ๊ฐ™์ด ํด๋ž˜์Šค ์ด๋ฆ„์„ ๋ถ™์—ฌ์ค˜์•ผํ•จ!)

 

โœ”๏ธ java.util.Random ํด๋ž˜์Šค

https://docs.oracle.com/javase/8/docs/api/java/util/Random.html

 

Random (Java Platform SE 8 )

An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.) If two instan

docs.oracle.com

: ๋‚œ์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค

new Random().nextInt(6); // [0, 6) ๋‚œ์ˆ˜ ์ƒ์„ฑ

 

๐Ÿ™‹๐Ÿป‍โ™€๏ธ Math.random()๊ณผ util.Random์˜ ์ฐจ์ด?

๐Ÿ’ก ์ฒซ๋ฒˆ์งธ๋กœ Math.random()์€ ํ•จ์ˆ˜, util.Random์€ ํด๋ž˜์Šค์ด๋ฏ€๋กœ util.Random์ด ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์„ ์ œ๊ณต!

๐Ÿ’ก Math.random()์€ ๋‚ด๋ถ€์ ์œผ๋กœ Random ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉ

double randNum = Math.random();
double randNum = new Random().nextDouble();

๐Ÿ’ก Randomํด๋ž˜์Šค๋Š” seed๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ

 

โ“Seed ๊ฐ’

: ์ปดํ“จํ„ฐ๋Š” ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Œ. ๋‹จ์ง€ ๋‚œ์ˆ˜ ์ƒ์„ฑ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑ!(์ฆ‰, ๊ฐ™์€ ์ž…๋ ฅ์„ ์ฃผ๋ฉด ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ด) ์ด๋•Œ, ์ž…๋ ฅ๊ฐ’์œผ๋กœ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์ด Seed๊ฐ’! ๊ทธ๋ฆฌ๊ณ  ๋‚œ์ˆ˜์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ์ ์œผ๋กœ seed๊ฐ’์œผ๋กœ ํ˜„์žฌ์‹œ๊ฐ„(System.currentTimeMillis())๋ฅผ ๋„ฃ์–ด์คŒ! 

 

๐Ÿ“‹ from๋ถ€ํ„ฐ to ์‚ฌ์ด์˜ ๋žœ๋ค ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ

public static int getRand(int from, int to) { //(์˜ˆ์‹œ) from: 10, to: 20
    return new Random().nextInt(Math.abs(to-from) + 1) + Math.min(from, to);
}

// 0 <= x < 1
// 0 <= x < 11 <- Random().nextInt(11)
// 10 <= x < 21

 

โœ”๏ธ java.util.regex ํŒจํ‚ค์ง€

https://docs.oracle.com/javase/8/docs/api/index.html?java/util/regex/package-summary.html 

 

Java Platform SE 8

 

docs.oracle.com

: ์ •๊ทœ์‹(Regular Expression)๊ณผ ๊ด€๋ จ๋œ ํŒจํ‚ค์ง€ (โ“์ •๊ทœ์‹ : ํŠน์ •ํ•œ ๊ทœ์น™์„ ๊ฐ€์ง„ ๋ฌธ์ž์—ด์˜ ์ง‘ํ•ฉ)

: ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ(ex. String) ์ค‘์—์„œ ์›ํ•˜๋Š” ์กฐ๊ฑด(pattern)๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ฐพ์•„๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ!

 

๐Ÿ“‹ ์‚ฌ์šฉ๋ฒ•

1๏ธโƒฃ ์ •๊ทœ์‹์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ Pattern์„ ๋งŒ๋“ค์ž!

import java.util.regex.*; //java.util.*; ์•„๋‹˜!
Pattern pattern = Pattern.compile("c[a-z]*"); //c๋กœ ์‹œ์ž‘ํ•˜๋Š” ์˜๋‹จ์–ด

2๏ธโƒฃ ํŒจํ„ด์„ ์›ํ•˜๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์™€ ๋งค์นญ!

Matcher matcher = pattern.matcher("I love cats!");

3๏ธโƒฃ Matcher ์ธ์Šคํ„ด์Šค๋ฅผ ํ™œ์šฉํ•ด์„œ ์ •๊ทœ์‹์— ๋ถ€ํ•ฉํ•˜๋Š”์ง€ ํ™•์ธ!

if(matcher.matches()) System.out.println("์ „์ฒด ๋ฌธ์ž์—ด์ด ํ•ด๋‹น ํŒจํ„ด์— ๋งค์นญ๋˜๋Š”์ง€ ๊ฒ€์‚ฌ!"); //false
if(matcher.find()) System.out.println(matcher.start() + " - " + matcher.end()); //true, 7 - 11

๐Ÿ’ก (์ฐธ๊ณ ) ์ •๊ทœ์‹ ํŒจํ„ด ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ์ฑ… + ๊ณต์‹๋ฌธ์„œ + ๊ตฌ๊ธ€๋ง ์ฐธ๊ณ 

 

โ— ์ •๊ทœ์‹ ์ผ๋ถ€๋ฅผ ๊ทธ๋ฃนํ™”(group)ํ•  ์ˆ˜๋„ ์žˆ์Œ!

https://school.programmers.co.kr/learn/courses/30/lessons/17682

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

Pattern pattern = Pattern.compile("([0-9]*)([SDT])([*|#]?)");
Matcher matcher = pattern.matcher("1D2S#10S");

while(matcher.find()) {
	System.out.print(matcher.group(1) + matcher.group(2));
    if(!matcher.group(3).isEmpty()) {
        System.out.print(matcher.group(3));
    }
    System.out.print("\n");
}
// 1D
// 2S#
// 10S

 

โœ”๏ธ java.util.Scanner ํด๋ž˜์Šค

https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

 

Scanner (Java Platform SE 8 )

Scans the next token of the input as a float. This method will throw InputMismatchException if the next token cannot be translated into a valid float value as described below. If the translation is successful, the scanner advances past the input that match

docs.oracle.com

: ์ž…๋ ฅ์†Œ์Šค(ex. String, File, InputStream, Path ...)๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๋Š” ์Šค์บ๋„ˆ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํด๋ž˜์Šค

Scanner s = new Scanner(System.in);
String input = s.nextLine();

: ์ž…๋ ฅ ๋ฐ›์„ ๊ฐ’์ด ์ˆซ์ž๋ผ๋ฉด nextInt(), nextDouble() ๋“ฑ์„ ํ†ตํ•ด ํ˜•๋ณ€ํ™˜์„ ์ค„์ผ ์ˆ˜ ์žˆ์Œ!

: ์ •๊ทœํ‘œํ˜„์‹์„ ์ด์šฉํ•ด์„œ ๋ผ์ธ ๋‹จ์œ„ ๊ฒ€์ƒ‰ ๊ฐ€๋Šฅ!

String line = "100, 200, 300";
Scanner s = new Scanner(line).useDelimiter(", ");

while(s.hasNextInt()) {
    System.out.print(s.nextInt() + " ");
} //100 200 300

 

โœ”๏ธ java.util.StringTokenizer ํด๋ž˜์Šค

 

 

๐Ÿ“Œ java.math ํŒจํ‚ค์ง€

โœ”๏ธ java.math.BigInteger ํด๋ž˜์Šค

 

โœ”๏ธ java.math.BigDecimal ํด๋ž˜์Šค

 

๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2025/05   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ