Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- ํ๋ก๊ทธ๋๋จธ์ค ์ปฌ๋ฌ๋ง๋ถ
- ํ๋ก๊ทธ๋๋จธ์ค
- prod docker-compose
- ์๋ฒ ํฐ์ง๋ ๋์ปค ์ฌ์คํ
- ํ๋ก๊ทธ๋๋จธ์ค ํฉ์นํ์์๊ธ
- Kafka
- ๋ค์ค ์ปจํ ์ด๋
- s3 ์ด๋ฏธ์ง ๋ค์ด๋ก๋
- s3 ์ด๋ฏธ์ง ์ ์ฅ
- docker ps -a
- aws saa ํฉ๊ฒฉ
- ์ ํจ์ค ๋น๋ ์ค๋ฅ
- ์๋ฐ
- ์คํํ๋ ๋ฏธ์ค
- private subnet ec2 ๋ก์ปฌ ์ ์
- s3 log ์ ์ฅ
- redis ์กฐํ
- nGrinder
- aws ์ฟ ํฐ
- JPA
- AWS Certified Solutions Architect - Associate
- docker-compose kafka
- redis ํ ์คํธ์ฝ๋
- jvm ๋ฐ๋ฐ๋ฅ๊น์ง ํํค์น๊ธฐ
- ํ์ดํผ๋ฐ์ด์
- Codedeploy ์ค๋ฅ
- docker
- docker compose
- ์ ํจ์ค ์ค์ผ์ค๋ฌ
- Entity
Archives
- Today
- Total
๐๐ข๐๐ โ๐๐๐ ๐๐๐ก๐๐ ๐๐๐๐โง
[Java] ๋ฌธ์์ด ๋ณธ๋ฌธ
๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป/๐๐๐ฏ๐
[Java] ๋ฌธ์์ด
๐คRyusun๐ค 2022. 9. 30. 15:46โญ๋ฌธ์์ด
- ์ด๊ธฐํ
- String s = “hello”
- ์์ํ์ “hello”๋ผ๋ ๋ฌธ์์ด์ด ์๋์ง ํ์ธ, ์์ผ๋ฉด ์์ํ์ ์๋ก ๋ง๋ฆ
- String s = new String("hello");
- ํ ์์ญ์ “hello”๋ผ๋ ๊ฐ์ฒด๋ฅผ ์๋ก ์์ฑ
- String s = “hello”
public class StringInitExample {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
String s4 = new String("hello");
// ์ฃผ์๊ฐ ๋น๊ต
System.out.println(s1 == s2); // true
System.out.println(s3 == s4); // false
// ๋ด์ฉ๋ฌผ ๋น๊ต
System.out.println(s1.equals(s2)); // true
System.out.println(s3.equals(s4)); // true
}
}
- ์คํธ๋ง ํ
- ์คํธ๋ง ๋ฆฌํฐ๋ด์ ์ฌ์ฌ์ฉ๋ ๊ฐ๋ฅ์ฑ์ด ๋ง๊ธฐ ๋๋ฌธ์ ์คํธ๋ง ํ์ ์ ์ฅ
- ๋ง์ฝ ์ฌ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ์๋ ์คํธ๋ง ํ์ ์ ์ฅ๋ ์คํธ๋ง ๋ฆฌํฐ๋ด ์ฌ์ฌ์ฉ String interning
- ๋ฉ๋ชจ๋ฆฌ ์ ์ฅ
- ๋ฌธ์์ด ์ ์ฅ์ ์ํด ๊ฐ ๋ฌธ์์ ํด๋นํ๋ byte ๋ฐฐ์ด๋ก ์ ์ฅ
- String s = "hello";
// s - ๊ฐ์ฒด ๋ณ์
// "hello" - ๋ฌธ์์ด ๋ฆฌํฐ๋ด
- String s = "hello";
- ๊ฐ์ฒด ๋ณ์๋ ์คํ ๊ณต๊ฐ์ ์ ์ฅ
- ๋ฌธ์์ด ๋ฆฌํฐ๋ด์ String Pool์ ์ ์ฅ
- ๋ฌธ์์ด ์์์ ํน์ง
- ๋ณดํต ๋ฌธ์์ด์ ์์ฑ๋ ๋ฌธ์์ด ์์๋ฅผ ์ ์ฅ → String Pool์ ์ฅ
- ๋ฌธ์์ด ์ ์ฅ์ ์ํด ๊ฐ ๋ฌธ์์ ํด๋นํ๋ byte ๋ฐฐ์ด๋ก ์ ์ฅ
public class StringConcatExample {
public static void main(String[] args) {
String s = "hello";
// s.charAt(0) = 'H'; // ์๋ฌ. ์ผ๋ถ ์์ ์์ ๋ถ๊ฐ๋ฅ
s += "world"; // ๋ค์ ์ฌํ ๋นํด์ผ ํจ
// ๋ค์ ์ฌํ ๋นํ๋ฉด ์์ํ์ "Hello"๋ผ๋ ๋ฌธ์์ด ๊ฐ์ฒด ์์ฑํ๊ณ ์์ฑํ ๊ฐ์ฒด์ ์ฃผ์๊ฐ ์ ์ฅ
}
}
- ๋ฌธ์์ด ๊ด๋ จ ํจ์
- equals() - ๋ฌธ์์ด ๋ด์ฉ๋ฌผ ๋์ผ ์ฌ๋ถ ํ์ธ(๋ฌธ์ ๋น๊ตํ ๋๋ ''(์์๋ฐ์ํ) ์ฌ์ฉํด์ผํจ)
- length() - ๋ฌธ์์ด ๊ธธ์ด ๋ฐํ
- charAt() - ํน์ ์ธ๋ฑ์ค์ ๋ฌธ์ ๋ฐํ
- indexOf() - ํน์ ๋ฌธ์์ ์ธ๋ฑ์ค ๋ฐํ
- contains() - ๋ฌธ์์ด ํฌํจ ์ฌ๋ถ ํ์ธ
- compareTo() - ๋ฌธ์์ด ์ฌ์ ์ ๋น๊ต (์์ผ๋ฉด ์์, ๊ฐ์ผ๋ฉด 0, ๋ค์ ์์ผ๋ฉด ์์)
- matches() - ์ ๊ท ํํ์ ์ผ์น ์ฌ๋ถ ํ์ธ ์ ๊ท ํํ์ ์ฌ์ฉ ๊ฐ๋ฅ
- replace(๊ธฐ์กด ๋ฌธ์์ด, ๋ณ๊ฒฝํ ๋ฌธ์์ด), replaceAll(), replaceFirst() - ๋ฌธ์์ด ์นํ ์ ๊ท ํํ์ ์ฌ์ฉ ๊ฐ๋ฅ
- substring(์์์ธ๋ฑ์ค., ๋ ์ธ๋ฑ์ค) - ์ผ๋ถ ๋ฌธ์์ด ๋ฐํ
- split() - ์ ๊ท ํํ์ ๋๋ ํน์ ๋ฌธ์ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด ๋๋ ์ ๊ท ํํ์ ์ฌ์ฉ ๊ฐ๋ฅ
- trim(), strip() - ์๋ณ์ ์๋ ๊ณต๋ฐฑ ์ ๊ฑฐ ํ ๋ฐํ ์น ๊ฐ๋ฐ
- toCharArray() - ๋ฌธ์์ด → ๋ฌธ์ ๋ฐฐ์ด
- toLowerCase(), toUpperCase() - ๋ฌธ์์ด ์๋ฌธ์ / ๋๋ฌธ์๋ก ๋ณํ
- // ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ด ๋ฌธ์์ด ๋๋ฑ๋น๊ต
- System.out.println(s1.toLowerCase().equals(s2.toLowerCase())); System.out.println(s1.equalsIgnoreCase(s2)); // ์ ์ฝ๋์ ๋์ผ
- isEmpty(), isBlank() - ๋ฌธ์์ด ๊ธธ์ด 0์ธ์ง ํ์ธ / ๋ฌธ์์ด ๋น์ด์๊ฑฐ๋ whitespace์ธ์ง ํ์ธ
public class StringIsEmptyExample {
public static void main(String[] args) {
// isEmpty: ๋ฌธ์์ด ๊ธธ์ด ํ์ธ
// isBlank: ๋ฌธ์์ด์ด ๋น์ด์๊ฑฐ๋ whitespace ๋ฌธ์์ธ์ง ํ์ธ
String s1 = "";
String s2 = "\t\n ";
System.out.println(s1.isEmpty()); // true
System.out.println(s2.isEmpty()); // ๋ฌธ์์ด ๊ธธ์ด 0์ด ์๋. false
System.out.println(s1.isBlank()); // true
System.out.println(s2.isBlank()); // white space ์กด์ฌ. true
}
}
- ๋ฌธ์์ด์ ๋ค๋ฅธ ๋ฐ์ดํฐํ์
์ผ๋ก ๋ฐ๊พธ๋ ๋ฐฉ๋ฒ
- String → byte - Byte.parseByte(String str)
- String → short - Short.parseShort(String str)
- String → int - Integer.parseInt(String str)
- Strign → long → Long.parseLong(String str)
- String → float - Float.parseFloat(String str)
- String → double - Double.parseDouble(String str)
public class StringIsEmptyExample {
public static void main(String[] args) {
// isEmpty: ๋ฌธ์์ด ๊ธธ์ด ํ์ธ
// isBlank: ๋ฌธ์์ด์ด ๋น์ด์๊ฑฐ๋ whitespace ๋ฌธ์์ธ์ง ํ์ธ
String s1 = "";
String s2 = "\t\n ";
System.out.println(s1.isEmpty()); // true
System.out.println(s2.isEmpty()); // ๋ฌธ์์ด ๊ธธ์ด 0์ด ์๋. false
System.out.println(s1.isBlank()); // true
System.out.println(s2.isBlank()); // white space ์กด์ฌ. true
}
}
import java.util.Arrays;
public class StringToCharArrayExample {
public static void main(String[] args) {
// String ์์ ๋ถ๊ฐ (String Pool์ ์ ์ฅ)
String str = "this is constant string. Immutable";
// str.charAt(0) = 'T'; // ๋ถ๊ฐ๋ฅ
char[] charArr = str.toCharArray();
System.out.println(Arrays.toString(charArr));
// char[]์ ์์ ๊ฐ๋ฅ (ํ์ ์ ์ฅ)
// StringBuilder, StringBuffer๋ char[]๋ก ๊ตฌํ๋์ด์์
charArr[0] = 'T'; // ๊ฐ๋ฅ
System.out.println(Arrays.toString(charArr));
String res = String.valueOf(charArr); // ์์ ์๋ฃ. ๋ค์ ์์ํํ๊ธฐ
System.out.println(res);
}
}
- StringBuffer & StringBuilder
- String ๊ฐ์ฒด๋ ํ๋ฒ ์์ฑ๋๋ฉด ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ด ๋ณํ์ง ์์
- ๋ฌธ์์ด ๋ณ๊ฒฝ, ์์ ์ด ๋ง์๊ฒฝ์ฐ StringBuffer, StringBuilder ์ฌ์ฉ
- ๋ฐ์ดํฐ๋ฅผ Heap์ ์ ์ฅํ๋ค
- StringBuffer ์ StringBuilder ์ฐจ์ด
- StringBuffer๋ ๋๊ธฐํ ํค์๋๋ฅผ ์ง์ํ์ฌ ๋ฉํฐ์ฐ๋ ๋ ํ๊ฒฝ์์ ์์ ํ๋ค
- StringBuilder๋ ๋๊ธฐํ๋ฅผ ์ง์ํ์ง ์์ ๋ฉํฐ์ฐ๋ ๋ ํ๊ฒฝ์์ ์ฌ์ฉํ๋ ๊ฒ์ ์ ํฉํ์ง ์๋ค. ํ์ง๋ง ๋๊ธฐํ๋ฅผ ๊ณ ๋ คํ์ง ๋จ์ผ์ฐ๋ ๋์์์ ์ฑ๋ฅ์ StringBuffer ๋ณด๋ค ๋ฐ์ด๋๋ค.
โญ์ ๊ทํํ์
- ์ ๊ท ํํ์์ ์ ๋ ฅ ๋ฐ์ดํฐ ํฌ๋งท์ ํ์ธํ๊ธฐ ์ข์
- if๋ฌธ๊ณผ ๋น์ทํ๊ฒ ์ ๋ ฅ ๋ฐ์ดํฐ๊ฐ ๋ค์ด์ค์๋ง์ ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉ
- ์ฝํ ์์๋ ์ ๋ ฅ ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉ
- ์ ๊ท ํํ์
- Pattern
- ์ ๊ท์์ ํจํด ํด๋์ค๋ก ์ปดํ์ผ
- Matcher
- ํจํด์ ํด์ํ์ฌ ๋ฌธ์์ด์ ๋ง์ถฐ๋ณด๋ matching ์์
- ์ฃผ์ ๋ฉ์๋
- ์์
import java.util.regex.Pattern;
public class PatternClassExample {
static final String EMAIL_REGEX = "^\\w+@\\w+\\.\\w+(\\.\\w+)?$";
static final String PHONE_REGEX = "^\\d{2,3}-\\d{3,4}-\\d{4}$";
public static void main(String[] args) {
String email = "qwerty@gmail.com";
System.out.println("Pattern.matches(EMAIL_REGEX, email) = " + Pattern.matches(EMAIL_REGEX, email));
email = "qweqw@naver.co.kr";
System.out.println("Pattern.matches(EMAIL_REGEX, email) = " + Pattern.matches(EMAIL_REGEX, email));
email = "qwe@naver.";
System.out.println("Pattern.matches(EMAIL_REGEX, email) = " + Pattern.matches(EMAIL_REGEX, email));
System.out.println();
String phone = "010-1234-5678";
System.out.println("Pattern.matches(PHONE_REGEX, phone) = " + Pattern.matches(PHONE_REGEX, phone));
phone = "02-000-0000";
System.out.println("Pattern.matches(PHONE_REGEX, phone) = " + Pattern.matches(PHONE_REGEX, phone));
phone = "010-00000-0000";
System.out.println("Pattern.matches(PHONE_REGEX, phone) = " + Pattern.matches(PHONE_REGEX, phone));
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherClassExample1 {
static final String ALPHA_REGEX = "^[a-zA-Z]*$";
static final String GROUP_REGEX = "([0-9]+)([SDT])([*#]?)";
static final String GROUP_NAME_REGEX = "(?<score>[0-9]+)(?<bonus>[SDT])(?<option>[*#]?)";
public static void main(String[] args) {
// ์ฝ๋ฉ ํ
์คํธ ๋ฌธ์ (https://school.programmers.co.kr/learn/courses/30/lessons/17682)
// 1. ์์ฑํ๊ธฐ, ์ ๊ท์ ์ผ์น์ฌ๋ถ ํ๋ณํ๊ธฐ
String input = "hello world";
System.out.println(Pattern.matches(ALPHA_REGEX, input)); // ๊ณต๋ฐฑ ํฌํจ. false
Pattern pattern = Pattern.compile(ALPHA_REGEX);
Matcher matcher = pattern.matcher(input);
System.out.println(matcher.matches()); // ๊ณต๋ฐฑ ํฌํจ. false
System.out.println();
// 2. Grouping
// find() - ํจํด์ด ์ผ์นํ๋ ๋ค์ ๋ฌธ์์ด์ด ์กด์ฌ๋ค๋ฉด true ๋ฐํ
pattern = Pattern.compile(GROUP_REGEX);
matcher = pattern.matcher("1D2S#10S*"); // 1D/2S#/10S*
while (matcher.find()) {
System.out.println(matcher.group(1) + "/" + matcher.group(2) + "/" + matcher.group(3));
System.out.println(matcher.start(1) + "/" + matcher.start(2) + "/" + matcher.start(3));
System.out.println(matcher.end(1) + "/" + matcher.end(2) + "/" + matcher.end(3));
System.out.println();
}
pattern = Pattern.compile(GROUP_NAME_REGEX);
matcher = pattern.matcher("1D2S#10S*"); // 1D/2S#/10S*
while (matcher.find()) {
System.out.println(matcher.group("score"));
}
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherClassExample2 {
static final String REGEX = "a*b";
public static void main(String[] args) {
String input = "aabzzzaabxxxxabyyyy";
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(input);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
System.out.println("sb = " + sb);
System.out.println("matcher.group().toUpperCase() = " + matcher.group().toUpperCase());
matcher.appendReplacement(sb, matcher.group().toUpperCase());
System.out.println();
}
matcher.appendTail(sb);
System.out.println("sb = " + sb);
}
}
'๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป > ๐๐๐ฏ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๊ฐ์ฒด, ์์ฑ์, ํด๋์ค (0) | 2022.10.07 |
---|---|
[Java] ๋ฐฐ์ด (6) | 2022.09.30 |
[Java] ์กฐ๊ฑด๋ฌธ (0) | 2022.09.30 |
[Java] ์ ์ถ๋ ฅ, ์๋ฃํ, ์ฐ์ฐ์ (4) | 2022.09.23 |
[Java] ๋ณ์, ์์, ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ (2) | 2022.09.23 |