개발 중 필요할 때 쉽게 찾아볼 수 있도록 자바의 핵심 Method들을 정리해보았다. 목차와 검색(Ctrl + F)을 활용하면 필요한 부분을 빠르게 참조할 수 있을 것이다.
1️⃣ String(문자열) Method
메서드 | 설명 | 예시 코드 (String str = "Hello Java"); |
chatAt(int index) | 지정된 인덱스의 문자를 반환 | char c = str.charAt(2); // 'l' |
concat(String s) | 문자열을 뒤에 붙임 | String t = str.concat("s"); // "Hello Javas" |
contains(CharSequence s) | 문자열에 특정 문자가 포함되어 있는지 확인 | boolean b = str.equals("HELLO"); // false |
equalsIgnoreCase(String s) | 대소문자 구분 없이 문자열 비교 | boolean b = str.equalsIgnoreCase("HELLO"); // true |
indexOf(String s) |
지정된 문자 또는 문자열이 처음으로 나타나는 위치 반환 (없으면 -1 반환) | int index = str.indexOf("a"); // 7 |
lastIndexOf(String s) | 지정된 문자 또는 문자열이 마지막으로 나타나는 위치 반환 | int index = str.lastIndexOf("l"); // 3 |
trim() | 문자열의 앞뒤 공백을 제거 | Strint str2 = " aaa "; String s = str2.trim(); // "aaa" |
length() | 문자열의 길이를 반환 (공백 포함) | int t = str.length(); // 10 |
substring(int beginIndex) | 지정된 인덱스에서 끝까지의 문자열을 반환 | String s = str.substring(3); // "lo Java" |
substring(int beginIndex, int endIndex) | 지정된 시작 인덱스부터 끝 인덱스 전까지의 문자열을 반환 | String s = str.substring(1, 5); // "ello " |
replace(CharSequence target, CharSequence replacement) | 지정된 문자 또는 문자열을 다른 문자로 대체 | String s = str.replace("a", "c"); // "Hello Jcvc" |
replaceAll(String regex, String replacement) | 정규 표현과 일치하는 모든 부분을 대체 | String s = str.replaceAll("[A-Za-z]+", ""); // "" |
toUpperCase() | 모든 문자를 대문자로 변환 | String s = str.toUpperCase(); // "HELLO WORLD" |
toLowerCase() | 모든 문자를 소문자로 변환 | String s = str.toLowerCase(); // "hello world" |
split(String regex) | 정규식을 기준으로 문자열을 분리하여 배열로 반환 | String[] arr = str.split(" "); // ["Hello","World"] (공백을 기준으로 분리) |
join(CharSequence delimiter, CharSequence... elements) | 여러 문자열을 구분자로 연결 | String result = String.join("-", "2024", "05"); // "2024-05" |
replaceFirst(String regex, String replacement) | 첫 번째로 일치하는 문자열 교체 | String s = str.replaceFirst("a", "b"); // "Hello Jbva" |
matches(String regex) | 문자열이 주어진 정규 표현식과 일치하는지 확인 | boolean isMatch = str.matches("[A-Za-z]+"); // true |
repeat(int count) | 문자열을 주어진 횟수만큼 반복 | String repeated = "abc".repeat(3); // "abcabcabc" |
isEmpty() | 문자열이 빈 문자열인지 확인 | boolean isEmpty = str.isEmpty(); // false |
isBlank() | 문자열이 공백만으로 이루어져 있는지 확인 | boolean isBlank = str.isBlank(); // false |
valueOf(Object obj) | 객체를 문자열로 변환 | String s = String.valueOf(123); // "123" |
2️⃣ Math(수학) Method
대부분 static 메서드로 객체를 생성하지 않고 직접 호출할 수 있다.
메서드 | 설명 | 예시 코드 |
abs(int a) | 절댓값을 반환 | int result = Math.abs(-5); // 5 |
abs(double a) | double result = Math.abs(-5.5); // 5.5 |
|
max(int a, int b) | 두 값 중 더 큰 값을 반환 | nt result = Math.max(10, 20); // 20 |
min(int a, int b) | 두 값 중 더 작은 값을 반환 | int result = Math.min(10, 20); // 10 |
pow(double a, double b) | a의 b 제곱 값을 반환 | double result = Math.pow(2, 3); // 8.0 |
sqrt(double a) | 제곱근을 반환 | double result = Math.sqrt(16); // 4.0 |
cbrt(double a) | 세제곱근을 반환 | double result = Math.cbrt(27); // 3.0 |
ceil(double a) | 올림값을 반환 (소수점 첫째 자리에서 올림) | double result = Math.ceil(3.14); // 4.0 |
floor(double a) | 내림값을 반환 (소수점 첫째 자리에서 내림) | double result = Math.floor(3.99); // 3.0 |
round(float a) | 반올림하여 가장 가까운 정수를 반환 | int result = Math.round(3.5f); // 4 |
random() | 0.0 이상 1.0 미만의 난수를 반환 | double result = Math.random(); // 0.0 <= result < 1.0 |
exp(double a) | e의 a 제곱 값을 반환 (e는 자연로그의 밑) | double result = Math.exp(1); // 2.718281828459045 |
log(double a) | 자연로그 (밑이 e인 로그) 값을 반환 | double result = Math.log(2.718); // 0.999896315728952 |
log10(double a) | 밑이 10인 로그 값을 반환 | double result = Math.log10(100); // 2.0 |
log1p(double a) | 1 + a의 자연로그 값을 반환 | double result = Math.log1p(1); // 0.6931471805599453 |
sin(double a) | 사인 값을 반환 (라디안 단위) | double result = Math.sin(Math.PI / 2); // 1.0 |
cos(double a) | 코사인 값을 반환 (라디안 단위) | double result = Math.cos(Math.PI); // -1.0 |
tan(double a) | 탄젠트 값을 반환 (라디안 단위) | double result = Math.tan(Math.PI / 4); // 1.0 |
asin(double a) | 아크사인(역사인) 값을 반환 (결과는 -π/2 ~ π/2 범위) | double result = Math.asin(1); // 1.5707963267948966 (π/2) |
acos(double a) | 아크코사인(역코사인) 값을 반환 (결과는 0 ~ π 범위) | double result = Math.acos(1); // 0.0 |
atan(double a) | 아크탄젠트(역탄젠트) 값을 반환 (결과는 -π/2 ~ π/2 범위) | double result = Math.atan(1); // 0.7853981633974483 (π/4) |
atan2(double y, double x) | y/x의 아크탄젠트 값을 반환 (결과는 -π ~ π 범위) | double result = Math.atan2(1, 1); // 0.7853981633974483 |
hypot(double x, double y) | 직각 삼각형의 빗변 길이를 반환 (피타고라스 정리 이용) | double result = Math.hypot(3, 4); // 5.0 |
toDegrees(double rad) | 라디안을 도(degree) 단위로 변환 | double result = Math.toDegrees(Math.PI); // 180.0 |
toRadians(double deg) | 도(degree) 단위를 라디안으로 변환 | double result = Math.toRadians(180); // 3.141592653589793 |
signum(double d) | 주어진 값의 부호를 반환 (-1.0, 0.0, 1.0 중 하나) | double result = Math.signum(-10); // -1.0 |
expm1(double a) | exp(a) - 1 값을 반환 | double result = Math.expm1(1); // 1.718281828459045 |
absExact(int x) | 절대값을 반환하되, 오버플로우가 발생하면 예외를 던짐 | int result = Math.absExact(-10); // 10 |
'☕ Java' 카테고리의 다른 글
[Java Library] QueryDSL (0) | 2024.09.23 |
---|---|
[Java] 자바 기초 문법 (0) | 2024.09.22 |