+, -, *, /, %: 덧셈, 뺄셈, 곱셈, 나눗셈, 나머지 연산
==, !=, <, >, <=, >=: 두 값의 크기나 동등성 비교
&&, ||, !: AND, OR, NOT
=, +=, -=, *=, /=, %=: 값을 변수에 저장하거나 연산 후 대입
++, --: 값 1 증가 또는 감소 (전위, 후위 모두 가능)
condition ? trueValue : falseValue: 조건에 따라 값을 선택
&, |, ^, ~: AND, OR, XOR, NOT
<<, >>, >>>: 왼쪽 시프트, 오른쪽 시프트 (부호 유지), 오른쪽 시프트 (부호 무시)
+: 문자열을 이어 붙일 때 사용
String.format(), printf()텍스트에 서식 지정자를 사용하여 값을 삽입한다.
%s: 문자열%d: 10진수 정수%f: 실수 (소수점 기본 6자리)%c: 문자%b: boolean 값%%: % 자체public class Main {
public static void main(String[] args) {
String name = "Alice";
int age = 20;
double pi = 3.141592;
System.out.println(String.format("Name: %s, Age: %d", name, age)); // Name: Alice, Age: 20
System.out.println(String.format("pi: %.2f", pi)); // pi: 3.14
System.out.printf("|%10s|\n", "Java"); // | Java|
System.out.printf("|%-10s|\n", "Java"); // |Java |
System.out.printf("%05d\n", 1); // 00001
}
}
MessageFormat순번 기반 포맷 ({0}, {1})을 사용한다. {index[, type[, subformat]]} 형태로 지정한다.
number, date, time, choice, etc.import java.text.MessageFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
String pattern = "Name: {0}, Age: {1}";
String result = MessageFormat.format(pattern, "Alice", 20);
System.out.println(result); // Name: Alice, Age: 20
Date today = new Date();
String message_date = MessageFormat.format("Today is {0, date, yyyy-MM-dd}.", today);
System.out.println(message_date); // Today is 2025-09-06.
String message_amount =
MessageFormat.format("Total Amount:{0, number, #,###.00}", 1234567.89);
System.out.println(message_amount); // Total Amount: 1,234,567.89
}
}
DecimalFormat숫자에 천 단위 쉼표, 소수점 자리수 등 포맷을 지정한다.
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
DecimalFormat df1 = new DecimalFormat("#,###.00");
System.out.println(df1.format(1234.567)); // 1,234.57
DecimalFormat df2 = new DecimalFormat("00000");
System.out.println(df2.format(42)); // 00042
DecimalFormat df3 = new DecimalFormat("#.00");
System.out.println(df3.format(3.1)); // 3.10
System.out.println(df3.format(3.141)); // 3.14 (반올림)
}
}
DateTimeFormatter날짜/시간을 포맷팅한다.
| 기호 | 의미 | 예시 (값) |
|---|---|---|
| y | 연도 (year) | 2025 |
| M | 월 (month) | 07, 7, Jul |
| d | 일 (day) | 01~31 |
| E | 요일 | Tue, 수요일 |
| H | 시 (0~23) | 00~23 |
| h | 시 (1~12) | 01~12 |
| m | 분 (minute) | 00~59 |
| s | 초 (second) | 00~59 |
| a | 오전/오후 | AM, PM |
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formatted = today.format(formatter);
System.out.println("Today is " + formatted); // Today is 2025-09-06
String dateStr = "2025-09-06";
LocalDate parsedDate = LocalDate.parse(dateStr, formatter);
System.out.println("Parsed date: " + parsedDate); // Parsed date: 2025-09-06
System.out.println(LocalDate.now().format(DateTimeFormatter.ISO_DATE)); // 2025-09-06
System.out.println(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME)); // 2025-09-06T14:10:39.136909
System.out.println(LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE)); // 20250906
}
}
StringBuilder 포맷: StringBuilder.append()문자열을 빠르고 효율적으로 조립 (append)할 수 있는 클래스이다. 반복적이거나 동적으로 문자열을 생성할 때 유용하며, 포맷 효과를 수동으로 구성한다.
public class Main {
public static void main(String[] args) {
String name = "Alice";
int age = 20;
StringBuilder sb1 = new StringBuilder();
sb1.append("Name: ").append(name).append(", Age: ").append(age);
System.out.println(sb1.toString()); // Name: Alice, Age: 20
StringBuilder sb2 = new StringBuilder();
sb2.append(String.format("%-10s", name)).append(String.format("%5d", age));
System.out.println(sb2.toString()); // Alice 20
}
}
✏️
StringBuildervsStringBuffer
항목 StringBuilder StringBuffer 스레드 안전성 스레드 안전하지 않음 스레드 안전함 (synchronized) 성능 빠름 (단일 스레드 환경에서 권장) 느림 (동기화 오버헤드 있음) 사용 시점 단일 스레드 또는 동기화 불필요한 경우 사용 멀티 스레드 환경에서 동시 접근이 필요한 경우 도입 시기 Java 5 이후 Java 초창기부터 대체 관계 StringBuffer의 비동기화 버전StringBuilder보다 안정적이나 느림기본 메서드 append(),insert(),delete(),toString(), etc.동일