[JAVA] API

min 🎸·2025λ…„ 1μ›” 23일

πŸ€” APIλž€ ?

Application Programming Interface(API)λŠ” μ‘μš©ν”„λ‘œκ·Έλž¨μ—μ„œ μ‚¬μš©ν•  수 μžˆλ„λ‘,
νŠΉμ • κΈ°λŠ₯을 μ‚¬μš©ν•  수 μžˆλ„λ‘ μ œκ³΅λ˜λŠ” λ©”μ„œλ“œ, 클래슀, μΈν„°νŽ˜μ΄μŠ€ λ“±μ˜ 집합을 μ˜λ―Έν•œλ‹€.

Java APIλž€?

Javaμ—μ„œ APIλŠ” Java Standard Library에 ν¬ν•¨λœ 클래슀 및 μΈν„°νŽ˜μ΄μŠ€μ˜ λͺ¨μŒμ„ λœ»ν•˜λ©°, java.lang, java.util, java.io λ“±μ˜ νŒ¨ν‚€μ§€μ— ν¬ν•¨λœ λ‹€μ–‘ν•œ κΈ°λŠ₯을 μ œκ³΅ν•œλ‹€.


πŸ’‘ Math 클래슀 (java.lang.Math)

μˆ˜ν•™ κ΄€λ ¨ 연산을 μ œκ³΅ν•˜λŠ” Java의 λ‚΄μž₯ 클래슀
λͺ¨λ“  λ©”μ„œλ“œλŠ” static λ©”μ„œλ“œ β†’ 객체 생성 없이 μ‚¬μš© κ°€λŠ₯
java.lang νŒ¨ν‚€μ§€μ— ν¬ν•¨λ˜μ–΄ μžˆμ–΄ import 없이 μ‚¬μš© κ°€λŠ₯

πŸ“Œ μ£Όμš” λ©”μ„œλ“œ

λ©”μ„œλ“œμ„€λͺ…μ˜ˆμ œ
Math.abs(x)μ ˆλŒ“κ°’ λ°˜ν™˜Math.abs(-10) // 10
Math.pow(x, y)x^y (κ±°λ“­μ œκ³±) λ°˜ν™˜Math.pow(2, 3) // 8.0
Math.sqrt(x)제곱근 λ°˜ν™˜Math.sqrt(25) // 5.0
Math.max(a, b)두 κ°’ 쀑 μ΅œλŒ€κ°’ λ°˜ν™˜Math.max(3, 7) // 7
Math.min(a, b)두 κ°’ 쀑 μ΅œμ†Œκ°’ λ°˜ν™˜Math.min(3, 7) // 3
Math.round(x)반올림 (long λ°˜ν™˜)Math.round(4.6) // 5
Math.ceil(x)올림 (double λ°˜ν™˜)Math.ceil(4.2) // 5.0
Math.floor(x)λ‚΄λ¦Ό (double λ°˜ν™˜)Math.floor(4.8) // 4.0
Math.random()0.0 이상 1.0 미만의 λ‚œμˆ˜ λ°˜ν™˜Math.random() * 10 // 0~9 사이 λ‚œμˆ˜

λ‚œμˆ˜ ν™œμš©

1. java.lang.Math ν΄λž˜μŠ€μ—μ„œ μ œκ³΅ν•˜λŠ” λ‚œμˆ˜ ν™œμš©

(int) (Math.random * κ΅¬ν•˜λ €λŠ” λ‚œμˆ˜μ˜ 갯수) + κ΅¬ν•˜λ €λŠ” λ‚œμˆ˜μ˜ μ΅œμ†Œκ°’

public static void main(String[] args) {

   /* λ‚œμˆ˜μ˜ ν™œμš© */
   /* Math.random()을 μ΄μš©ν•΄ λ°œμƒν•œ λ‚œμˆ˜λŠ” 0λΆ€ν„° 1μ „κΉŒμ§€μ˜ μ‹€μˆ˜ λ²”μœ„μ˜ λ‚œμˆ˜κ°’μ„ λ°˜ν™˜ν•œλ‹€.
    * μ›ν•˜λŠ” λ²”μœ„μ˜ λ‚œμˆ˜λ₯Ό κ΅¬ν•˜λŠ” 곡식
    *(int) (Math.random * κ΅¬ν•˜λ €λŠ” λ‚œμˆ˜μ˜ 갯수) + κ΅¬ν•˜λ €λŠ” λ‚œμˆ˜μ˜ μ΅œμ†Œκ°’ 
    */ 

   /* 1 ~ 10κΉŒμ§€μ˜ λ‚œμˆ˜ λ°œμƒ */
   int random2 = (int) (Math.random() * 10) + 1;
   System.out.println("1 λΆ€ν„° 10 μ‚¬μ΄μ˜ λ‚œμˆ˜ : " + random2);


   /* -128 ~ 127κΉŒμ§€μ˜ λ‚œμˆ˜ λ°œμƒ */
   //int random4 = (int) (Math.random() * 256) + (-128);
   int random4 = (int) (Math.random() * 256) - 128;
   System.out.println("-128 λΆ€ν„° 127κΉŒμ§€μ˜ λ‚œμˆ˜ λ°œμƒ : " + random4);
}

2. java.util.Random 을 μ΄μš©ν•œ λ‚œμˆ˜μ˜ ν™œμš©

random.nextInt(κ΅¬ν•˜λ €λŠ” λ‚œμˆ˜μ˜ 갯수) + κ΅¬ν•˜λ €λŠ” λ‚œμˆ˜μ˜ μ΅œμ†Œκ°’

public static void main(String[] args) {

   /* java.util.Random 클래슀 */
   /* java.util.Random 클래슀의 nextInt() λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•œ λ‚œμˆ˜ λ°œμƒ
    * nextInt(int bound) : 0λΆ€ν„° λ§€κ°œλ³€μˆ˜λ‘œ 전달받은 μ •μˆ˜ λ²”μœ„κΉŒμ§€μ˜ λ‚œμˆ˜λ₯Ό λ°œμƒμ‹œμΌœμ„œ μ •μˆ˜ ν˜•νƒœλ‘œ λ°˜ν™˜ */

   /* μ›ν•˜λŠ” λ²”μœ„μ˜ λ‚œμˆ˜λ₯Ό κ΅¬ν•˜λŠ” 곡식
    * random.nextInt(κ΅¬ν•˜λ €λŠ” λ‚œμˆ˜μ˜ 갯수) + κ΅¬ν•˜λ €λŠ” λ‚œμˆ˜μ˜ μ΅œμ†Œκ°’
    * */

   Random random = new Random();

   /* 1λΆ€ν„° 10κΉŒμ§€ λ‚œμˆ˜ λ°œμƒ */
   int randomNumber2 = random.nextInt(10) + 1;
   System.out.println("1 λΆ€ν„° 10 κΉŒμ§€μ˜ λ‚œμˆ˜ : " + randomNumber2);


   /* -128 λΆ€ν„° 127κΉŒμ§€μ˜ λ‚œμˆ˜ λ°œμƒ */
   //int randomNumber4 = random.nextInt(256) - 128;
   int randomNumber4 = new Random().nextInt(256) - 128;		//객체λ₯Ό μƒμ„±ν•˜μžλ§ˆμž λ°”λ‘œ λ©”μ†Œλ“œ ν˜ΈμΆœλ„ ν•  수 μžˆλ‹€.
   System.out.println("-128 λΆ€ν„° 127 κΉŒμ§€μ˜ λ‚œμˆ˜ : " + randomNumber4);
}

πŸ’‘ Scanner 클래슀 (java.util.Scanner)

μ‚¬μš©μž μž…λ ₯을 μ²˜λ¦¬ν•˜λŠ” 클래슀
ν‚€λ³΄λ“œ, 파일, λ¬Έμžμ—΄ λ“±μ—μ„œ μž…λ ₯을 받을 수 있음
java.util νŒ¨ν‚€μ§€μ— ν¬ν•¨λ˜μ–΄ μžˆμ–΄ import ν•„μš”

객체 생성: Scanner sc = new Scanner(System.in);
μž…λ ₯ λ°›κΈ°: sc.nextInt(), sc.nextLine(), sc.nextDouble() λ“±
μ‚¬μš© ν›„ λ‹«κΈ°: sc.close(); (μžμ› λˆ„μˆ˜ λ°©μ§€)

πŸ“Œ μ£Όμš” λ©”μ„œλ“œ

λ©”μ„œλ“œμ„€λͺ…μ˜ˆμ œ
nextInt()μ •μˆ˜ μž…λ ₯sc.nextInt();
nextDouble()μ‹€μˆ˜ μž…λ ₯sc.nextDouble();
nextLine()ν•œ 쀄(곡백 포함) μž…λ ₯sc.nextLine();
next()곡백 μ „κΉŒμ§€ λ¬Έμžμ—΄ μž…λ ₯sc.next();
hasNextInt()μ •μˆ˜ μž…λ ₯ μ—¬λΆ€ 확인sc.hasNextInt();
close()Scanner λ‹«κΈ° (λ©”λͺ¨λ¦¬ ν•΄μ œ)sc.close();

μ£Όμ˜μ‚¬ν•­

1. next()둜 λ¬Έμžμ—΄ μž…λ ₯ 받은 ν›„ μ •μˆ˜, μ‹€μˆ˜, 논리값 μž…λ ₯ 받을 λ•Œ

public static void main(String[] args) {

   /* μŠ€μΊλ„ˆ 주의 사항 */

   /* μŠ€μΊλ„ˆμ˜ next λ©”μ†Œλ“œλ“€μ€ μž…λ ₯ν•œ λ‚΄μš©μ„ λ²„νΌλ‘œλΆ€ν„° ν† ν°λ‹¨μœ„λ‘œ λΆ„λ¦¬ν•΄μ„œ μ½μ–΄μ˜¨λ‹€.
    * κ·Έλž˜μ„œ 크게 두 κ°€μ§€ 사항을 μ£Όμ˜ν•΄μ•Ό ν•œλ‹€.
    * 1. next()둜 λ¬Έμžμ—΄ μž…λ ₯ 받은 ν›„ μ •μˆ˜, μ‹€μˆ˜, 논리값 μž…λ ₯ 받을 λ•Œ
    * 2. μ •μˆ˜, μ‹€μˆ˜, 논리값 μž…λ ₯ ν›„ next()둜 λ¬Έμžμ—΄ μž…λ ₯받을 λ•Œ
    * */

   /* μŠ€μΊλ„ˆ 객체 생성 */
   Scanner sc = new Scanner(System.in);

   /* 1. next()둜 λ¬Έμžμ—΄ μž…λ ₯ 받은 ν›„ μ •μˆ˜, μ‹€μˆ˜, 논리값 μž…λ ₯ 받을 λ•Œ  */
   System.out.print("λ¬Έμžμ—΄μ„ μž…λ ₯ν•΄μ£Όμ„Έμš” : ");
   String str1 = sc.next();				//κ³΅λ°±μ΄λ‚˜ κ°œν–‰λ¬Έμž μ „ κΉŒμ§€λ₯Ό μ½μ–΄μ˜¨λ‹€.
   System.out.println("str1 : " + str1);

   System.out.print("숫자λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš” : ");
   int num1 = sc.nextInt();				//μ •μˆ˜ 값을 μ½μ–΄μ˜¨λ‹€.
   System.out.println("num1 : " + num1);
}

"μ•ˆλ…•ν•˜μ„Έμš” λ°˜κ°‘μŠ΅λ‹ˆλ‹€" 와 같이 곡백이 μžˆμ„ 경우 λ‘κ°œμ˜ "μ•ˆλ…•ν•˜μ„Έμš”" 와 "λ°˜κ°‘μŠ΅λ‹ˆλ‹€" 둜 뢄리가 λœλ‹€.
next()λŠ” "μ•ˆλ…•ν•˜μ„Έμš”"만 읽기 λ•Œλ¬Έμ— nextInt()κ°€ μ •μˆ˜λ‘œ 읽지 λͺ»ν•΄ 였λ₯˜κ°€ λ°œμƒν•œλ‹€.

곡백이 μžˆλŠ” λ¬Έμžμ—΄μ„ 받을거면 nextLine()을 μ΄μš©ν•˜μž.
ν˜Ήμ€ μž…λ ₯ 받은 버퍼λ₯Ό 쀄 λ‹¨μœ„λ‘œ 토큰을 λΆ„λ¦¬ν•˜κΈ° λ•Œλ¬Έμ— ν•΄λ‹Ή 라인의 토큰을 λ‹€ 읽고 λ‹€μŒ μ€„λ‘œ μ΄λ™μ‹œν‚€κ³  싢을 λ•ŒλŠ”
ν•œ 라인의 λͺ¨λ“  토큰을 μ½μ–΄μ˜€λŠ” sc.nextLine(); ν˜ΈμΆœμ„ 쀑간에 ν•œ 번 λ„£μ–΄μ€˜λ„ 해결은 λœλ‹€.

2. μ •μˆ˜, μ‹€μˆ˜, 논리값 μž…λ ₯ ν›„ nextLine()둜 λ¬Έμžμ—΄ μž…λ ₯받을 λ•Œ

μ•žμ—μ„œ 남긴 κ°œν–‰μ„ nextLine()이 읽고 λ„˜μ–΄κ°€μ„œ μž…λ ₯μ „ ν”„λ‘œκ·Έλž¨μ΄ μ’…λ£Œλœλ‹€.
ν•΄κ²°ν•˜λŠ” 방법은 κ°œν–‰μ„ 받아쀄 nextLine()을 ν•œ 쀄 더 λͺ…μ‹œν•΄μ€ŒμœΌλ‘œ 버퍼λ₯Ό λΉ„μš°λ©΄ λœλ‹€.

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.print("λ‹€μ‹œ 숫자λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš” : ");
    int num2 = sc.nextInt();
    System.out.println("num2 : " + num2);

    System.out.print("곡백이 μžˆλŠ” λ¬Έμžμ—΄μ„ ν•˜λ‚˜ μž…λ ₯ν•΄μ£Όμ„Έμš” : ");

    sc.nextLine();         // 이 뢀뢄을 μΆ”κ°€ν•΄μ€€λ‹€.

    String str2 = sc.nextLine();
    System.out.println("str2 : " + str2);
}

0개의 λŒ“κΈ€