Skip to content

Commit

Permalink
Hello Java!
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueHorn07 committed Jan 2, 2025
1 parent 9765145 commit 51e3981
Showing 1 changed file with 135 additions and 0 deletions.
135 changes: 135 additions & 0 deletions _posts/development/java/2024-12-19-hello-java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
---
title: "Hello, Java!"
toc: true
toc_sticky: true
categories: ["Kafka"]
excerpt: "β˜•οΈ"
---

# Javaμ—λŠ” ν‘œμ€€ μŠ€νŽ™κ³Ό κ΅¬ν˜„μ²΄κ°€ 있음.

Javaλž€ 이런이런 κΈ°λŠ₯을 λ§Œμ‘±ν•΄μ•Ό ν•œλ‹€λŠ” ν‘œμ€€ μŠ€νŽ™μ΄ 있음! 그리고 그런 μŠ€νŽ™μ— 맞게 κ΅¬ν˜„ν•œ "κ΅¬ν˜„μ²΄"κ°€ 있음.

- OpenJDK (Oracle)
- Corretto (Amazon)

κ΅¬ν˜„μ²΄ λ§ˆλ‹€ λ™μž‘μ€ λ™μΌν•˜μ§€λ§Œ, λ‚΄λΆ€μ μœΌλ‘œ 각자의 μž…λ§›μ— 맞게 μ΅œμ ν™”ν•œ 뢀뢄듀이 μžˆμ„ κ²ƒμž„. Corretto 같은 κ²½μš°λŠ” AWS μ„œλΉ„μŠ€λž‘ ꢁ합이 μ’‹λ‹€κ³  함.

Java ν‘œμ€€ μŠ€νŽ™μ—λŠ” `javac` 컴파일러, Run Lib, JVMκ³Ό 같은 것듀이 μžˆμ–΄μ•Ό ν•œλ‹€κ³  λͺ…μ‹œ λ˜μ–΄ 있음.

# Java 컴파일

`javac` μš”κ²Œ Javaμ—μ„œμ˜ μ»΄νŒŒμΌλŸ¬μž„. Cμ—μ„œμ˜ `gcc` 같은 λ…€μ„μž„.

`java`λŠ” java 컨파일된 `.class` νŒŒμΌμ„ μ‹€ν–‰ν•˜λŠ” λ…€μ„μž„.

# Static Method & Variable

클래슀 μΈμŠ€ν„΄μŠ€κ°€ μ•„λ‹ˆλΌ 클래슀 μžμ²΄μ— κ³ μ •λœ λ©€λ²„μž„. κ·Έλž˜μ„œ 클래슀 μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“€μ§€ μ•Šκ³ λ„ Methodλ‚˜ Variable을 ν˜ΈμΆœν•  수 μžˆμŠ΅λ‹ˆλ‹€.

`static` Variable은 λͺ¨λ“  ν΄λž˜μŠ€μ—μ„œ κ³΅μœ ν•˜κ²Œ λ©λ‹ˆλ‹€. λ©”λͺ¨λ¦¬ μ„Έκ·Έλ¨ΌνŠΈ κ΄€μ μ—μ„œ 보면 `DATA` μ„Έκ·Έλ¨ΌνŠΈμ— μ €μž₯λ˜λŠ” λ³€μˆ˜λΌκ³  λ³Ό 수 있고, ν΄λž˜μŠ€λΌλŠ” μ˜μ—­μ— λ°”μΈλ”©λœ Global Variable둜 받아듀이면 될 것 κ°™μŠ΅λ‹ˆλ‹€.

Static와 non-static Method의 μ°¨μ΄λŠ” κ·Έ Methodλ₯Ό ν˜ΈμΆœν•  λ•Œ, `this` ν‚€μ›Œλ“œκ°€ ν•„μš”ν•˜λƒ μ•„λ‹ˆλƒ μž…λ‹ˆλ‹€. λ§Œμ•½ `this`λ₯Ό 톡해 클래슀 μΈμŠ€ν„΄μŠ€κ°€ κ°–λŠ” 값에 μ ‘κ·Όν•  ν•„μš”κ°€ μžˆλ‹€λ©΄, 그건 instance method μž…λ‹ˆλ‹€. λ§Œμ•½ 그럴 ν•„μš”κ°€ μ—†λ‹€λ©΄ static method둜 κ΅¬ν˜„ν•΄λ„ λ©λ‹ˆλ‹€!

# CLASSPATH

`java`와 `javac`μ—μ„œ μ–΄λ–€ 클래슀λ₯Ό ν˜ΈμΆœν•  것인지 μ§€μ‹œν•˜λŠ” ν™˜κ²½ λ³€μˆ˜ μž…λ‹ˆλ‹€.

Kafka Connectorλ₯Ό λ„μšΈ λ•Œ, μ–΄λ–€ Connector Plugin을 써야 할지도 μš” `CLASSPATH` ν™˜κ²½ λ³€μˆ˜λ‘œ 경둜λ₯Ό μ „λ‹¬ν•˜μ˜€μŠ΅λ‹ˆλ‹€. [[ref]](/2024/12/17/kafka-connector-standalone-mode/)

λ§Œμ•½ CLI ν™˜κ²½μ—μ„œ μ‚¬μš©ν•œλ‹€λ©΄,

```bash
$ javac --class-path /path/to/class MyClass.java
$ java --class-path /path/to/class MyClass
```

와 같은 ν˜•μ‹μœΌλ‘œ μ‚¬μš©ν•©λ‹ˆλ‹€.


# λ°°μ—΄ μ„ μ–Έ

```java
ArrayList<Integer> numbers = new ArrayList<>();
```

μ΄λ•Œ, `int`λΌλŠ” μ •μˆ˜ν˜•μ΄ μžˆλŠ”λ°, μ™œ `Integer`λ₯Ό μ‚¬μš©ν–ˆλŠλƒμž„. μ΄μœ λŠ” `ArrayList`λŠ” Java의 Generic 클래슀이기 λ•Œλ¬Έμ—, 객체 νƒ€μž…λ§Œ μ§€μ›ν•œλ‹€κ³  함. κ·Έλž˜μ„œ `int` native νƒ€μž…μ΄ μ•„λ‹ˆλΌ `Integer`λΌλŠ” 객체 νƒ€μž…μ„ 써야 ν•œλ‹€κ³  함.


# `java.lang` νŒ¨ν‚€μ§€

- `System`
- `Integer`, `Double`, `String`
- `Object`

"νŒ¨ν‚€μ§€"μ§€λ§Œ, λͺ¨λ“  μžλ°” μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ μžλ™μœΌλ‘œ `import` 됨. κ·Έλž˜μ„œ `System.out.println()`을 μ“Έ λ•Œ,


# Java 11

[[10λΆ„ ν…Œμ½”ν†‘] 호호의 Java 11](https://youtu.be/LcIyHlE2NlA?si=W9-4PvfezZYmLYZW)



# μ–΄λ–»κ²Œ κ³΅λΆ€ν•˜κ³  μžˆλŠ”μ§€?

μΈν”„λŸ°μ—μ„œ μš” κ°•μ˜κ°€ 무료둜 ν’€λ €μžˆκΈΈλž˜ 일단 μš” κ°•μ˜λ‘œ μ‹œμž‘ ν–ˆμŠ΅λ‹ˆλ‹€.

"[κΉ€μ˜ν•œμ˜ μžλ°” μž…λ¬Έ - μ½”λ“œλ‘œ μ‹œμž‘ν•˜λŠ” μžλ°” 첫걸음](https://www.inflearn.com/course/%EA%B9%80%EC%98%81%ED%95%9C%EC%9D%98-%EC%9E%90%EB%B0%94-%EC%9E%85%EB%AC%B8)"

λ¬΄λ£ŒλΌμ„œ 뢀담감 없이 μ‹œμž‘ν•΄λ³Έ 것 κ°™κ³ , μ²˜μŒμ— JavaλΌλŠ” 언어와 μƒνƒœκ³„λ₯Ό μ„€λͺ…ν•˜λŠ” 뢀뢄이 μ°Έ μ’‹μ•˜μŠ΅λ‹ˆλ‹€. κ·Έ μ™Έμ˜ 뒷뢀뢄은 κ°œλ°œμ„ μ™„μ „ 처음 ν•˜λŠ” μ‚¬λžŒ μœ„μ£Όλ‘œ 맞좰져 μžˆμ–΄μ„œ λΉ λ₯΄κ²Œ λ„˜κ²ΌμŠ΅λ‹ˆλ‹€. μœ„μ˜ 강사뢄이 μ„€λͺ…ν•˜λŠ” 것듀 μ€‘μ—λŠ” 쀑급 ~ κ³ κΈ‰ 뢀뢄이 μ œκ°€ 더 재밌게 곡뢀 ν•΄λ³Ό 수 μžˆλŠ” μ£Όμ œλ“€μΈ 것 κ°™μŠ΅λ‹ˆλ‹€ γ…Žγ…Ž

μ§€κΈˆμ€ λ°±μ€€μ—μ„œ 문제λ₯Ό ν’€λ©΄μ„œ μ–Έμ–΄μ˜ 맛을 느껴보고 μžˆμŠ΅λ‹ˆλ‹€ γ…Žγ…Ž

# λ°±μ€€μ—μ„œ Java μž…μΆœλ ₯

백쀀을 ν’€ λ•ŒλŠ” μž…μΆœλ ₯을 λ°›κΈ° μœ„ν•΄ κ³ λ €ν•΄μ•Ό ν•  점듀이 항상 μžˆλ‹€.

## Scanner

```java
Scanner sc = new Scanner(System.in)
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
sc.close();
```

## BufferedReader, BufferedWriter

λ°±μ€€ 문제λ₯Ό 풀닀보면, λΆ„λͺ… 둜직과 μ‹œκ°„λ³΅μž‘λ„κ°€ μ΅œμ μΈλ°λ„ "μ‹œκ°„μ΄ˆκ³Ό"κ°€ λœ¨λŠ” κ²½μš°κ°€ μžˆλ‹€.

이것은 `Scanner`와 `System.out.print()`κ°€ μž…μΆœλ ₯을 μ²˜λ¦¬ν•˜λŠ” μ‹œκ°„μ΄ λŠλ €μ„œ 그런 κ²ƒμœΌλ‘œ "버퍼링" κΈ°μˆ μ„ μ“°λŠ” `BufferedReader`와 `BufferedWriter`λ₯Ό μ‚¬μš©ν•˜λ©΄ λœλ‹€. μš” 버퍼링은 μ΄λ²ˆμ— λ³΅ν•™ν•΄μ„œ 듀은 운영체제(OS) μˆ˜μ—…μ—μ„œ λ°°μ› λ˜ λ‚΄μš©μΈλ°, μ΄λ ‡κ²Œ 금방 λ‹€μ‹œ λ§Œλ‚˜λ‹€λ‹ˆ γ…‹γ…‹ λ°˜κ°‘λ‹€.

```java
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

int n = Integer.parseInt(br.readLine());

br.close();
bw.flush();
bw.close();
```

μ΄λ•Œ, μ£Όμ˜ν•  점은 `bw.write(int n)`와 같이 μ •μˆ˜λ₯Ό `bw.write()` ν•  경우 이걸 숫자 String이 μ•„λ‹ˆλΌ μ•„μŠ€ν‚€μ½”λ“œλ‘œ 해석해 좜λ ₯ν•œλ‹€. κ·Έλž˜μ„œ μ •μˆ˜ 좜λ ₯ λ•ŒλŠ” `String.valueOf(int n)`으둜 String으둜 λ³€ν™˜ν•œ 후에 좜λ ₯ ν•΄μ€˜μ•Ό ν•œλ‹€.

<hr/>

# Spring

https://start.spring.io/

- Springboot?
- 내뢀에 tomcat μ„œλ²„?


## Spring Bootκ°€ 짱이닀

κ·Έλƒ₯ Spring을 μ“°κ²Œ 되면, λ‚΄μž₯ μ„œλ²„κ°€ μ—†μ–΄μ„œ λ³„λ„μ˜ tomcat μ„œλ²„λ₯Ό λ„μ›Œμ•Ό 함.

λ°˜λ©΄μ— Spring Bootμ—λŠ” tomcat μ„œλ²„κ°€ λ‚΄μž₯ λ˜μ–΄ 있음(embedded server). κ·Έλž˜μ„œ μ„œλΉ™ λ ˆμ΄μ–΄λ₯Ό λ”°λ‘œ λ””ν”Œλ‘œμ΄ ν•  ν•„μš” μ—†μŒ.

## Getter, Setter

μš”κ²Œ μžˆλŠ” κ±Έ Java Bin? κ·œμ•½ 이라고 함.

0 comments on commit 51e3981

Please sign in to comment.