πŸ‘¨πŸ»β€πŸ’»

[JAVA] API

κ³Όλͺ©
JAVA
νƒœκ·Έ
API
toString
hashcode
StringBuilder
StringBuffer
Calendar
StudyDate
2020/12/25

APIλž€ ?

β€’
API(Application Programming Interface)의 μ•½μžμ΄λ‹€
β€’
μ‘μš©ν”„λ‘œκ·Έλž¨μ—μ„œ μ‚¬μš©ν• μˆ˜ μžˆλ„λ‘ μš΄μ˜μ²΄μ œλ‚˜ ν”„λ‘œκ·Έλž˜λ°μ–Έμ–΄κ°€ μ œκ³΅ν•˜λŠ” κΈ°λŠ₯을
μ œμ–΄ ν•  수있게 λ§Œλ“  μΈν„°νŽ˜μ΄μŠ€

μžλ°” API

β€’
μžλ°”ν”Œλž«νΌμœ„μ—μ„œ λ™μž‘ν•˜λŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜
β€’
κ°œλ°œμ‹œ ν™œμš© (μœ μš©ν•œ 클래슀 및 μΈν„°νŽ˜μ΄μŠ€ 제곡)
β€’
JDKλ₯Ό μ„€μΉ˜ν•˜λ©΄ API제곡

Object

β€’
λͺ¨λ“  ν΄λž˜μŠ€λŠ” Object클래슀의 후손
β€’
java.lang νŒ¨ν‚€μ§€μ— 쑴재
β€’
Object ν΄λž˜μŠ€κ°€ 가진 λ©”μ†Œλ“œμ€‘ 관둀상 많이 μ˜€λ²„λΌμ΄λ”©ν•΄μ„œ μ‚¬μš©ν•˜λŠ” λ©”μ†Œλ“œλ“€μ΄ 쑴재
1. toString(): μΈμŠ€ν„΄μŠ€ μƒμ„±μ‹œ μ‚¬μš©ν•œ full class name κ³Ό @ 그리고 16μ§„μˆ˜ ν•΄μ‰¬μ½”λ“œκ°€
λ¬Έμžμ—΄λ‘œ λ°˜ν™˜
@Override public String toString() { return "BookVO [number=" + number + ", title=" + title + ", author=" + author + ", price=" + price + "]"; } 1. Object에 toStringλΌλŠ” λ©”μ†Œλ“œκ°€ μ‘΄μž¬ν•˜λ―€λ‘œ μ‚¬μš©ν• λ €λ©΄ Ovrrideν•΄μ•Όν•˜λ©° returnκ°’μœΌλ‘œ μ΄λ ‡κ²Œ ν•΄λ‹Ή 내역에 정보λ₯Ό λ°›μ•„μ˜¬μˆ˜ μžˆλ‹€ (μ½˜μ†”2-1) 2. toString을 μ˜€λ²„λΌμ΄λ”©μ•ˆν•˜κ³  ν˜ΈμΆœν•  경우 @ + 16μ§„μˆ˜κ°€ λ°˜ν™˜λœλ‹€.(μ½˜μ†”2-2) -------------------------------------------------------------------------------- BookVO book1 = new BookVO(1, "홍길동전", "ν—ˆκ· ", 50000); BookVO book2 = new BookVO(2, "λͺ©λ―Όμ‹¬μ„œ", "μ •μ•½μš©", 30000); BookVO book3 = new BookVO(3, "λͺ©λ―Όμ‹¬μ„œ", "μ •μ•½μš©", 30000); System.out.println("book1의 toString() : " + book1.toString() ); System.out.println("book2의 toString() : " + book2.toString() ); System.out.println("book3의 toString() : " + book3.toString() ); System.out.println("book1 : " + book1); System.out.println("book2 : " + book2); System.out.println("book3 : " + book3);
Java
볡사
(μ½˜μ†”2-1)
(μ½˜μ†”2-2)
2. equals() : 맀개 λ³€μˆ˜λ‘œ 전달 받은 μΈμŠ€ν„΄μŠ€μ™€ == μ—°μ‚°ν•˜μ—¬ true λ˜λŠ” falseλ₯Ό λ°˜ν™˜
동일 μΈμŠ€ν„΄μŠ€ 비ꡐ
(동일객체 : μ£Όμ†Œκ°€ λ™μΌν•œ μΈμŠ€ν„΄μŠ€)
(동등객체 : μ£Όμ†Œκ°€ λ‹€λ₯΄λ”라도 ν•„λ“œ 값이 λ™μΌν•œ μΈμŠ€ν„΄μŠ€)
package com.greedy.section01.object.run; import com.greedy.section01.object.vo.BookVO; public class Application2 { public static void main(String[] args) { * equals()λ©”μ†Œλ“œ κΈ°λ³Έ κΈ°λŠ₯은 동일객체 νŒλ‹¨μ„ ν•œλ‹€κ³ λ³Ό 수 μž‡λ‹€.(동일성 확인) * * ν•˜μ§€λ§Œ κ²½μš°μ— λ”°λΌμ„œλŠ” 동등객체λ₯Ό λ™μΌκ°μ²΄λ‘œ μ·¨κΈ‰ν•΄μ„œ λΉ„κ΅ν•˜λŠ” 싢은 κ²½μš°κ°€ λ°œμƒν•œλ‹€. * 즉, λ™μΌν•œ ν•„λ“œκ°’μ„ κ°€μ§€λŠ” 객체λ₯Ό 같은 객체둜 νŒλ‹¨ν•  수 μžˆλ„λ‘ ν•˜λŠ” κ²½μš°μ΄λ‹€. * κ·ΈλŸ¬ν•œ 경우 equals()λ©”μ†Œλ“œλ₯Ό μ˜€λ²„λΌλ”©ν•˜μ—¬, κ°κ°μ˜ν•„λ“œκ°€ λ™μΌν•œ 값을 κ°€μ§€λŠ”μ§€λ₯Ό ν™•μΈν•˜κ³  * λͺ¨λ“  ν•„λ“œ 값이 κ°™μ€κ²½μš° true, μ•„λ‹Œκ²½μš° falseλ₯Ό λ°˜ν™˜ν•˜λ„λ‘ μ œμž‘ν•œλ‹€. * */ BookVO book1 = new BookVO(1, "홍길동전", "ν—ˆκ· " ,50000); BookVO book2 = new BookVO(2, "홍길동전", "ν—ˆκ· ", 50000); System.out.println("두 μΈμŠ€ν„΄μŠ€μ˜ == μ—°μ‚° 비ꡐ : " + (book1==book2)); System.out.println("두 μΈμŠ€ν„΄μŠ€μ˜ equals() 비ꡐ : " + (book1.equals(book2))); } } ------------------------------- @Override public boolean equals(Object obj) { /*두 μΈμŠ€ν„΄μŠ€μ˜ μ£Όμ†Œκ°€ κ°™μœΌλ©΄ 이후 λ‹€λ₯Έλ‚΄μš©μ„ 비ꡐ ν•  것 없이 trueλ₯Ό λ³€ν™˜ν•œλ‹€.*/ if(this == obj) { return true; } /* thisλŠ” μΈμŠ€ν„΄μŠ€κ°€ μƒμ„±λ˜λ©΄ μ£Όμ†Œκ°’μ΄ μ €μž₯λœλ‹€. * null이면 λ©”μ†Œλ“œλ₯Ό 호좜 μžμ²΄κ°€ λΆˆκ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ— thisλŠ” null일 수 μ—†λ‹€. * λ”°λΌμ„œ 전달 받은 래퍼런슀 λ³€μˆ˜κ°€ null값을 μ €μž₯ν•˜κ³  μžˆλ‹€λ©΄ * λΉ„κ΅ν•˜λ €λŠ” 두 개의 μΈμŠ€ν„°λŠ” μ„œλ‘œ λ‹€λ₯Έ μΈμŠ€ν„΄μŠ€μ΄λ‹€.*/ if(obj == null) { return false; } /*전달받은 레퍼런슀 λ³€μˆ˜λ₯Ό BookVO νƒ€μž…μœΌλ‘œ ν˜•λ³€ν™˜ν•˜μ—¬ 각 ν•„λ“œλ³„λ‘œ λΉ„κ΅μ‹œλ₯Ό μ‹œν‚¨λ‹€.*/ BookVO other = (BookVO) obj; /*number ν•„λ“œ 값이 μ„œλ‘œ 같지 μ•ŠμœΌλ©΄ μ„œλ‘œ λ‹€λ₯Έ μΈμŠ€ν„΄μŠ€μ΄λ‹€*/ if(this.number != other.number) { return false; } /*title ν•„λ“œμ˜ 값이 null인경우 * λ§€κ°œλ³€μˆ˜λ‘œ 전달 받은 μΈμŠ€ν„΄μŠ€μ˜ titleν•„λ“œμ—λ„ null이 μ €μž₯λ˜μ–΄μžˆμ–΄μ•Ό μ„œλ‘œ 같은 μΈμŠ€ν„΄μŠ€μ΄λ‹€. * 그렇지 μ•ŠμœΌλ©΄ falseλ₯Ό λ°˜ν™˜ν•˜κ³  끝낸닀.*/ if(this.title == null) { /*title ν•„λ“œκ°€ null 인경우 λ‹€λ₯Έ μΈμŠ€ν„΄μŠ€μ˜ title이 null이 μ•„λ‹ˆλ©΄ fasleλ₯Ό λ°˜ν™˜*/ if(other.title != null) { return false; } }else if(!this.title.equals(other.title)) { /*String 클래슀의 equals()λŠ” 이미 ν•΄λ‹Ή κΈ°λŠ₯을 μ˜€λ²„λΌμ΄λ”©ν•΄ λ‘μ—ˆκΈ° λ•Œλ¬Έμ— * 같은 값을 κ°€μ§€λŠ”μ§€λ₯Ό λΉ„κ΅ν•œλ‹€. * * titleν•„λ“œκ°€ null이 μ•„λ‹Œ 경우 두 μΈμŠ€ν„΄μŠ€μ˜ ν•„λ“œ 값이 κ°™μ•„μ•Όν•˜λŠ”λ° * 그렇지 μ•ŠμœΌκ²½μš° falseλ₯Ό λ°˜ν™˜ν•˜κ³  μ’…λ£Œ*/ return false; } /*μž‘κ°€μ˜ λΉ„κ΅λŠ” 제λͺ© 비ꡐ와 같은 방식이닀.*/ if(this.author == null) { if(other.author != null) { return false; } }else if(!this.author.equals(other.author)) { return false; } return true; }
Java
볡사
3. hashCode() : μΈμŠ€ν„΄μŠ€μ˜ μ£Όμ†Œκ°’μ„ λ³€ν™˜ν•˜μ—¬ μƒμ„±ν•œ μΈμŠ€ν„΄μŠ€μ˜ κ³ μœ κ°’μ„ λ°˜ν™˜
β€’
Object 클래슀의 λͺ…μ„Έμ˜ μž‘μ„±λœ 일반적인 κ·œμ•½μ— λ”°λ₯΄μžλ©΄ equals() λ©”μ†Œλ“œλ₯Ό μ œμ •μ˜ ν•˜λŠ” 경우 hashCode() λ©”μ†Œλ“œλ„ μž¬μ •μ˜ν•˜κ²Œ λ˜μ–΄μžˆλ‹€. λ§Œμ•½ hashCode()λ₯Ό μž¬μ •μ˜ν•˜μ§€ μ•ŠμœΌλ©΄ 같은 값을 κ°€μ§€λŠ” λ™λ“±κ°μ²΄λŠ” 같은 ν•΄μ‹œμ½”λ“œ 값을 κ°€μ Έμ•Ό ν•œλ‹€λŠ” κ·œμ•½μ— μœ„λ°˜ν•˜κ²Œλœλ‹€ (κ°•μ œμ„±μ€ μ•„λ‹ˆκ³  ν•„μš”μ—λ”°λ₯Έ κ΄€λ‘€μš©λ²•μ΄λ‹€)
β€’
λ™μΌν•œ ν•„λ“œκ°’μ„ 가지면 λ™μΌν•œ ν•΄μ‰¬μ½”λ“œλ₯Ό 가진닀
β€’
동등객체λ₯Ό λ™μΌκ°μ²΄λ‘œ λ§Œλ“€κΈ°μœ„ν•΄μ„œ μ‚¬μš©ν•œλ‹€.
β€’
μ•„λž˜ μ½”λ“œλ‚΄μš©μ€ λ¨Όμ € hashcodeλ₯Ό μ˜€λ²„λΌμ΄λ”©ν•œλ‹€μŒμ— λ©”μΈλ©”μ„œλ“œμ—μ„œ ν˜ΈμΆœν•˜λ©΄ 같은 hashcode 값을 κ°€μ§„λ‹€λŠ” λ‚΄μš©μ„ μ½”λ“œλ‘œ λ‚˜νƒ€λ‚Έκ±°λ‹€
@Override public int hashCode() { /*κ³±μ…ˆ 연산을 λˆ„μ μ‹œμΌœμ•Ό ν•˜κΈ° λ•Œλ¬Έμ— 0이 μ•„λ‹Œ κ°’μœΌλ‘œ μ΄ˆκΈ°ν™”*/ int result = 1; /*31은 μ†Œμˆ˜μ΄κΈ° 떄문에 μ—°μ‚° μ‹œ λ™μΌν•œ hashCode 값이 λ‚˜μ˜€μ§€ μ•Šκ²Œ ν•˜κΈ°μœ„ν•΄ μ‚¬μš©ν•œλ‹€. *31이 톡상적인 관둀이며 String ν΄λž˜μŠ€μ—μ„œλ„ μ‚¬μš©ν•œ 값이닀. * */ final int PRIME = 31; /*String 클래슀이 hashCode λ©”μ†ŒλŠ” 이미 μž¬μ •μ˜κ°€ λ˜μ–΄μžˆλ‹€. * 같은 값을 κ°€μ§€λŠ” λ¬Έμžμ—΄μ€ λ™μΌν•œ hashCode값을 가지고 μžˆλ‹€. * */ result = PRIME * result + this.number; result = PRIME * result + this.title.hashCode(); result = PRIME * result + this.author.hashCode(); result = PRIME * result + this.price; return result; }
Java
볡사
*동등객체 생성*/ BookVO book1 = new BookVO(1,"홍길동전", "ν—ˆκ· ", 50000); BookVO book2 = new BookVO(1,"홍길동전", "ν—ˆκ· ", 50000); System.out.println("book1의 hashCode : " + book1.hashCode()); System.out.println("book2의 hashCode : " + book2.hashCode());
Java
볡사

String 클래슀

package com.greedy.section02.string; public class Application1 { public static void main(String[] args) { /*String ν΄λž˜μŠ€μ—μ„œ μžμ£Όμ‚¬μš©ν•˜λŠ” λ©”μ†Œλ“œ * 1.charAt() : ν•΄λ‹Ή λ¬Έμžμ—΄μ˜ νŠΉμ • μΈλ±μŠ€μ— ν•΄λ‹Ήν•˜λŠ” 문자λ₯Ό λ°˜ν™˜ν•œλ‹€. * μΈλ±μŠ€λŠ” 0λΆ€ν„° μ‹œμž‘ν•˜λŠ” 숫자 체계λ₯Ό μ˜λ―Έν•˜λ©° * 인덱슀λ₯Ό λ²—μ–΄λ‚œ μ •μˆ˜λ₯Ό 인자둜 μ „λ‹¬ν•˜λŠ” κ²½μš°μ—λŠ” IndexOutOfBoundException이 λ°œμƒν•œλ‹€. * */ String str1 = "apple"; for(int i = 0 ; i < str1.length(); i++) { System.out.println("charAt(" + i + ") : " + str1.charAt(i)); } 2. compartTo() : 인자둜 μ „λ‹¬ν•œ λ¬Έμžμ—΄κ³Ό 사전 순으둜 비ꡐλ₯Ό ν•˜μ—¬ * 두 λ¬Έμžμ—΄μ΄ κ°™μœΌλ©΄ 0을 λ°˜ν™˜, 인자둜 μ „λ‹¬ν•œ λ¬Έμžμ—΄λ³΄λ‹€ μž‘μœΌλ©΄ 음수λ₯Ό * 크면 μ–‘μˆ˜λ₯Ό λ°˜ν™˜ν•œλ‹€. * 단, 이 λ©”μ†Œλ“œλŠ” λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λΆ„ν•˜λ©° λΉ„κ΅ν•œλ‹€. * */ String str2 = "java"; String str3 = "java"; String str4 = "JAVA"; String str5 = "oracle"; /*κ°™μœΌλ©΄ 0을 λ°˜ν™˜*/ System.out.println("compareTo() : " + (str2.compareTo(str3)) ); /*λŒ€λ¬Έμžμ™€ μ†Œλ¬ΈμžλŠ” 32만큼 차이가 λ‚œλ‹€.*/ System.out.println("compareTo() : " + (str2.compareTo(str4)) ); System.out.println("compareTo() : " + (str4.compareTo(str2)) ); /*jklmno jλΆ€ν„° oκΉŒμ§€ 5μΉΈ λ–¨μ–΄μ Έμž‡λ‹€.*/ System.out.println("compareTo() : " + (str2.compareTo(str5))); 3. compareToIgnoreCase() : λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λΆ„ν•˜μ§€ μ•Šκ³  λΉ„κ΅ν•œλ‹€.*/ System.out.println("compareToIgnoreCase() : " + (str3.compareToIgnoreCase(str4))); 4.concat() : λ¬Έμžμ—΄μ— 인자둜 전달 된 λ¬Έμžμ—΄μ„ ν•©μΉ˜κΈ°ν•΄μ„œ μƒˆλ‘œμš΄ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•œλ‹€. * 단, 원본 λ¬Έμžμ—΄μ—λŠ” 영ν–₯을 μ£Όμ§€μ•ŠλŠ”λ‹€.*/ System.out.println("concat() : " + (str2.concat(str5))); System.out.println("str2:" + str2); 5.indexOf() : λ¬Έμžμ—΄μ—μ„œ νŠΉμ • 문자λ₯Ό νƒμƒ‰ν•˜μ—¬ 처음 μΌμΉ˜ν•˜λŠ” μΈλ±μŠ€μœ„μΉ˜λ₯Ό μ •μˆ˜ν˜•μœΌλ‘œ λ°˜ν™˜ν•œλ‹€. * 단 μΌμΉ˜ν•˜λŠ” λ¬Έμžκ°€ μ—†λŠ”κ²½μš° -1λ₯Ό λ°˜ν™˜ * */ String indexOf = "java oracle"; String example = "apple iphone"; System.out.println("indexOf('a'): " + indexOf.indexOf('a')); System.out.println("indexOf('z'): " + indexOf.indexOf('z')); 6.lastIndexOf() : λ¬Έμžμ—΄ 탐색을 λ’€μ—μ„œλΆ€ν„° ν•˜κ³  처음 μΌμΉ˜ν•˜λŠ” μœ„μΉ˜μ˜ 인덱슀λ₯Ό μ •μˆ˜ν˜•μœΌλ‘œ λ°˜ν™˜ν•œλ‹€. * 단 μΌμΉ˜ν•˜λŠ” λ¬Έμžμ—΄μ΄ μ—†λŠ”κ²½μš° -1을 λ°˜ν™˜ν•œλ‹€. * */ System.out.println("lastIndexOf('a') : " + indexOf.lastIndexOf('a')); System.out.println("lastIndexOf('z') : " + indexOf.lastIndexOf('z')); 7.trim() : λ¬Έμžμ—΄ μ•ž 뒀에 곡백을 μ œκ±°ν•œ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•œλ‹€. * 원본에 영ν–₯을 주지 μ•ŠλŠ”λ‹€. * */ String trimStr = " java "; System.out.println("trimStr : #" + trimStr + "#"); System.out.println("trim() : #" + trimStr.trim() + "#"); System.out.println("trimStr : #" + trimStr + "#"); 8. toLowerCase() : λͺ¨λ“  문자λ₯Ό μ†Œλ¬Έμžλ‘œ λ³€ν™˜μ‹œν‚¨λ‹€. 9. toUpperCase() : λͺ¨λ“  문자λ₯Ό λŒ€λ¬Έμžλ‘œ λ³€ν™˜μ‹œν‚¨λ‹€. * μ›λ³Έμ—λŠ” 영ν–₯을 주지 μ•ŠλŠ”λ‹€. * */ String caseStr = "JavaOracle"; System.out.println("toLowerCase() :" + caseStr.toLowerCase()); System.out.println("toUpperCase() :" + caseStr.toUpperCase()); System.out.println("caseStr :" + caseStr); 10. substring() : λ¬Έμžμ—΄μ˜ 일뢀뢄을 μž˜λΌλ‚΄μ–΄ μƒˆλ‘œμš΄ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•œλ‹€. * 원본에 영ν–₯을 주지 μ•ŠλŠ”λ‹€ * */ String javaoracle = "javaoracle"; System.out.println("substring(3,6) : " + javaoracle.substring(3,6)); System.out.println("substring(3) : " + javaoracle.substring(3)); System.out.println("javaoracle : " + javaoracle); String coronavirus = "coronavirus"; // System.out.println("subscring(2,5)" + coronavirus.substring(2,5)); 11. replace() : λ¬Έμžμ—΄μ—μ„œ λŒ€μ²΄ν•  λ¬Έμžμ—΄λ‘œ κΈ°μ€€ λ¬Έμžμ—΄μ„ λ³€κ²½ν•΄μ„œ λ³€ν™˜ν•œλ‹€. * μ›λ³Έμ—λŠ” 영ν–₯을 주지 μ•ŠλŠ”λ‹€. * */ System.out.println("replace : " + coronavirus.replace("corona", "COVID-19")); System.out.println("replaceA() : " + javaoracle.replace("java","phyton")); System.out.println("javaoracle : " + javaoracle); 12. length() : λ¬Έμžμ—΄μ˜ 길이λ₯Ό μ •μˆ˜ν˜•μœΌλ‘œ λ°˜ν™˜ν•œλ‹€.*/ System.out.println("length : " + javaoracle.length()); System.out.println("빈 λ¬Έμžμ—΄μ˜ 길이 : " + "".length()); 13.*isEmpty() : λ¬Έμžμ—΄μ˜ 길이가 0이면 true, μ•„λ‹ˆλ©΄ false λ°˜ν™˜ * 길이가 0인 λ¬Έμžμ—΄μ€ nullκ³ΌλŠ” λ‹€λ₯΄λ‹€ * */ System.out.println("isEmpty() : " + "".isEmpty()); System.out.println("isEmpty() : " + "abc".isEmpty()); } }
Java
볡사

λ¬Έμžμ—΄ 뢄리!

Spilit

split() : μ •κ·œ ν‘œν˜„μ‹μ„ μ΄μš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ λΆ„λ¦¬ν•œλ‹€. λΉ„μ •ν˜•ν™”λœ λ¬Έμžμ—΄μ„ 뢄리할 λ•Œ μ’‹μ§€λ§Œ ex)"123/234#3290$211" μ •κ·œν‘œν˜„μ‹μ„ μ΄μš©ν•˜κΈ° 떄문에 속도가 λŠλ¦¬λ‹€λŠ” 단점을 κ°€μ§€κ³ μžˆλ‹€. (μ •κ·œ ν‘œν˜„μ‹ 뢀뢄은 μžλ°”μ—μ„œ λ‹€λ£¨μ§€μ•Šκ³  μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ λ‹€λ£° μ˜ˆμ •μ΄λ‹€.)
String emp1 = "100/홍길동/μ„œμšΈ/μ˜μ—…λΆ€"; String emp2 = "200/μœ κ΄€μˆœ//총무뢀"; String emp3 = "300/μ΄μˆœμ‹ /경기도/"; /*split()을 μ΄μš©ν•œ 뢄리*/ String[] empArr1 = emp1.split("/"); String[] empArr2 = emp2.split("/"); String[] empArr3 = emp3.split("/"); for(int i = 0 ; i < empArr1.length; i++) { System.out.println("empArr1[" + i + "] : " + empArr1[i]); } System.out.println(); for(int i = 0; i < empArr2.length; i++) { System.out.println("empArr2[" + i + "] : " + empArr2[i]); } System.out.println(); for(int i = 0; i < empArr3.length; i++) { System.out.println("empArr3[" + i + "] : " + empArr3[i]); } System.out.println(); ----------------- /*split()은 μ •κ·œ ν‘œν˜„μ‹ 이용(λ¬Έμžμ—΄ νŒ¨ν„΄), StringTokenizerλŠ” κ΅¬λΆ„μž λ¬Έμžμ—΄ 이용*/ String colorStr = "red*orange#blue/yellow green"; /*[]λŠ” ν•œλ¬Έμžλ₯Όμ˜λ―Έν•˜κ³  μ•ˆμ— μž‘μ„±ν•œκΈ°ν˜Έλ“€μ€ 그쀑 ν•˜λ‚˜λ₯Ό κ΅¬λΆ„μžλ‘œ μ΄μš©ν•˜κ² λ‹€λŠ” μ˜λ―Έμ΄λ‹€.*/ String[] colors = colorStr.split("[*#/]"); for(int i = 0; i < colors.length; i++) { System.out.println("colors[" + i + "] : " + colors[i]);
Java
볡사

StringToknenizer

StringTokenizer : λ¬Έμžμ—΄μ˜ λͺ¨λ“  λ¬Έμžλ“€μ„ κ΅¬λΆ„μžλ‘œ ν•˜μ—¬ λ¬Έμžμ—΄μ„ λΆ„λ¦¬ν•œλ‹€. μ •ν˜•ν™”λœ λ¬Έμžμ—΄ νŒ¨ν„΄μ„ 뢄리할 λ•Œ μ‚¬μš©ν•˜κΈ° μ’‹λ‹€. split()보닀 μ†λ„λ©΄μ—μ„œ λΉ λ₯΄λ‹€.
κ΅¬λΆ„μžλ₯Ό μƒλž΅ν•˜λŠ” 경우 곡백이 κΈ°λ³Έ κ΅¬λΆ„μžλ‘œ μ‚¬μš©λœλ‹€.
/*StringTokenizer 이용*/ StringTokenizer st1 = new StringTokenizer(emp1,"/"); StringTokenizer st2 = new StringTokenizer(emp2,"/"); StringTokenizer st3 = new StringTokenizer(emp3,"/"); while(st1.hasMoreTokens()) { System.out.println("st1 : " + st1.nextToken()); } System.out.println(); /*nextToken()으둜 토큰을 κΊΌλ‚΄λ©΄ StringTokenizer의 토큰을 μž¬μ‚¬μš©ν•˜λŠ” 것이 λΆˆκ°€λŠ₯ν•˜λ‹€. while(st1.hasMoreTokens()) { System.out.println("st1 : μž¬μ‚¬μš©" + st1.nextToken()); } System.out.println("--------"); /*쀑간에 λΉ„μ–΄μžˆλŠ” 값에 ν•΄λ‹Ήν•˜λŠ” 토큰은 λ¬΄μ‹œν•œλ‹€.*/ while(st2.hasMoreTokens()) { System.out.println("st2 : " +st2.nextToken()); } while(st3.hasMoreTokens()) { System.out.println("st3 : " + st3.nextToken()); }
Java
볡사

μ΄μŠ€μΌ€μ΄ν”„λ¬Έμž

StringBuilder λž‘ String Buffer

두 ν΄λž˜μŠ€λŠ” μŠ€λ ˆλ“œ 동기화 κΈ°λŠ₯제곡 여뢀에 따라 차이점이 μžˆλ‹€.
StringBulider : μŠ€λ ˆλ“œ 동기화 κΈ°λŠ₯ μ œκ³΅ν•˜μ§€μ•ŠμŒ μŠ€λ ˆλ“œ λ™κΈ°ν™”μ²˜λ¦¬λ₯Ό κ³ λ €ν•˜μ§€μ•ŠλŠ” μƒν™©μ—μ„œ SrtringBuffer보닀 μ„±λŠ₯이 μ’‹μŒ StringBuffer : μŠ€λ ˆλ“œ 동기화 κΈ°λŠ₯ μ œκ³΅ν•¨ StringBuilder보닀 μ„±λŠ₯λ©΄μ—μ„œ 느림

λ™κΈ°ν™”λž€????

ν•˜λ‚˜μ˜ μŠ€λ ˆλ“œκ°€ μ‚¬μš©ν•˜κ³ μž‡μ„λ–„ λ‹€λ₯ΈμŠ€λ ˆλ“œκ°€ 접근을λͺ»ν•˜κ²Œν•˜λŠ”것을 λ™κΈ°ν™”λΌκ³ ν•œλ‹€

String과 StringBuilder의 차이

Stringκ³Ό StringBulider * String : λΆˆλ³€μ΄λΌλŠ” νŠΉμ„±μ„ 가지고 μžˆλ‹€. * λ¬Έμžμ—΄ + μ—°μ‚°μœΌλ‘œ ν•©μΉ˜κΈ° ν•˜λŠ” 경우, κΈ°μ‘΄ μΈμŠ€ν„΄μŠ€λ₯Ό μˆ˜μ •ν•˜λŠ” 것이 μ•„λ‹Œ * μƒˆλ‘œμš΄ μΈμŠ€ν„΄μŠ€λ₯Ό λ°˜ν™˜ν•œλ‹€. * λ”°λΌμ„œ λ¬Έμžμ—΄ 변경이 자주 μΌμ–΄λ‚˜λŠ” 경우 μ„±λŠ₯ λ©΄μ—μ„œ 쒋지 μ•Šλ‹€. * ν•˜μ§€λ§Œ λ³€ν•˜μ§€ μ•ŠλŠ” λ¬Έμžμ—΄μ„ 자주 μ½μ–΄λ“€μ΄λŠ” κ²½μš°μ—λŠ” 였히렀 쒋은 μ„±λŠ₯을 κΈ°λŒ€ν•  수 μžˆλ‹€. * * StringBuilder : κ°€λ³€μ΄λΌλŠ” νŠΉμ„±μ„κ°€μ§€κ³  μžˆλ‹€. * λ¬Έμžμ—΄ append() λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•˜μ—¬ ν•©μΉ˜κΈ° ν•˜λŠ” 경우 * κΈ°μ‘΄ μΈμŠ€ν„΄μŠ€λ₯Ό μˆ˜μ •ν•˜κΈ° λ•Œλ¬Έμ— μƒˆλ‘œμš΄ μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ§€ μ•ŠλŠ”λ‹€. * λ”°λΌμ„œ μž¦μ€λ¬Έμžμ—΄ 변경이 μΌμ–΄λ‚˜λŠ” 경우 String보닀 μ„±λŠ₯이 μ’‹λ‹€. * * 단 ,jdk 1.5 λ²„μ „μ—μ„œλŠ” λ¬Έμžμ—΄μ˜ + 연산이 StirngBuilder의 append()둜 컴파일이 λœλ‹€. * λ”°λΌμ„œ μ„±λŠ₯에 큰차이λ₯Ό 보이지 μ•ŠλŠ”λ‹€. * ν•˜μ§€λ§Œ λ°˜λ³΅λ¬Έμ—μ„œ λ¬Έμžμ—΄μ˜ + 연산을 μˆ˜ν–‰ν•˜λŠ” 경우 StringBuilder μΈμŠ€ν„΄μŠ€λ₯Ό * 반볡 λ£¨ν”„μ‹œλ§ˆλ‹€ μƒμ„±ν•˜κΈ° 떄문에 μ„±λŠ₯μ—λŠ” 쒋은 영ν–₯을 주지 μ•ŠλŠ”λ‹€. * * μ—­μ»΄νŒŒμΌ κ²°κ³Ό μ˜ˆμ‹œ * * μ›λ³Έμ½”λ“œ * ------------------------------------------- * String str1 = "java"; * String str2 = "oracle" * * String str3 = str1 + str2; * String str4 = ""; * * for(int i = 0 ; i < 10; 1++){ * str4 += str1; * } * * * ---------------------------------------------- * * * * jdk1.4μ΄ν•˜ * -------------------------------------- * String str1 = "java" * String str2 = "oracle" * * String str3 = str1 + str2; * String str4 = ""; * * for(int i = 0; i < 10; i++){ * str4 = str4 + str1; * } * ------------------------------------------------ * * * jdk 1.5 이상 * ------------------------------------------------- * String str1 = "java"; * String str2 = "oracle"; * * String str3 = new StringBuilder(str1).append(str2).toString(); * String4 = ""; * * StringBuilder sb = new StringBuilder(str4); *μ€‘μš”* λ°˜λ³΅λ¬Έμ— new StringBuilder(str4)κ°€ μ•„λ‹Œ sb둜 μ„ μ–Έν•˜μ—¬ μ‚¬μš©ν•΄μ£ΌλŠ”κ²Œ μ„±λŠ₯λ©΄μ—μ„œμ’‹λ‹€ * for(int i = 0 ; i < 10; i++) { * str4 = sb.append(str1).toString(); * } * ---------------------------------------------------- * */
Java
볡사

StringBuilder의 자주 μ‚¬μš©λ˜λŠ” λ©”μ†Œλ“œ

1.
capacity() : μš©λŸ‰(ν˜„μž¬ λ²„νΌμ˜ 크기)을 μ •μˆ˜ν˜•μœΌλ‘œ λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œ(κΈ°λ³Έ 16)
StringBuilder sb1 = new StringBuilder(); System.out.println(sb1.capacity());
Java
볡사
2. append() : 인자둜 전달 된 값을 λ¬Έμžμ—΄λ‘œ λ³€ν™˜ ν›„ κΈ°μ‘΄ λ¬Έμžμ—΄μ˜ λ§ˆμ§€λ§‰μ— μΆ”κ°€ν•œλ‹€. κΈ°λ³Έ μš©λŸ‰μ„ μ΄ˆκ³Όν•˜λŠ” 경우 ( κΈ°μ‘΄ λ¬Έμžμ—΄ + 1 ) * 2 λ₯Ό ν•˜μ—¬ μš©λŸ‰μ„ 혹μž₯μ‹œν‚¨λ‹€.
sb1.append("java"); System.out.println("sb1 : " + sb1);
Java
볡사
3 .delete()
: μ‹œμž‘ μΈλ±μŠ€μ™€ μ’…λ£Œ 인덱슀λ₯Ό μ΄μš©ν•΄μ„œ λ¬Έμžμ—΄μ—μ„œ μ›ν•˜λŠ” λΆ€λΆ„μ˜ λ¬Έμžμ—΄μ„ μ œκ±°ν•œλ‹€. deletaCharAt() : λ¬Έμžμ—΄ 인덱슀λ₯Ό μ΄μš©ν•΄μ„œ 문자 ν•˜λ‚˜λ₯Ό μ œκ±°ν•œλ‹€.
System.out.println("delete() : " + sb2.delete(2, 5) ); System.out.println("deleteCharAt() : " + sb2.deleteCharAt(0)); System.out.println("sb2 : " + sb2);
Java
볡사
4. insert() : 인자둜 μ „λ‹¬λœ 값을 λ¬Έμžμ—΄λ‘œ λ³€ν™˜ ν›„ μ§€μ •ν•œ 인덱슀 μœ„μΉ˜μ— μΆ”κ°€ν•œλ‹€. 원본에 영ν–₯을 λΌμΉœλ‹€.
/*insert() : 인자둜 μ „λ‹¬λœ 값을 λ¬Έμžμ—΄λ‘œ λ³€ν™˜ ν›„ μ§€μ •ν•œ 인덱슀 μœ„μΉ˜μ— μΆ”κ°€ν•œλ‹€. * 원본에 영ν–₯을 λΌμΉœλ‹€.*/ System.out.println("insert() : " + sb2.insert(1, "vao")); System.out.println("insert() : " + sb2.insert(0,"j")); System.out.println("sb2 : " + sb2); System.out.println("insert() : " + sb3.insert(0, "coro")); System.out.println("insert() : " + sb3.insert(7, "i")); /*인덱슀 λ²ˆν˜Έκ°€ λ¬Έμžμ—΄ 기리이와 κ°™μ€κ²½μš° append()와 λ™μΌν•œμ—­ν• μ„ ν•œλ‹€.*/ System.out.println("insert() : " +sb2.insert(sb2.length(),"jabc"));
Java
볡사
5. reverse() : λ¬Έμžμ—΄ 인덱슀 μˆœλ²ˆμ„ μ—­μˆœμœΌλ‘œ μž¬λ°°μ—΄ν•œλ‹€. 원본에 영ν–₯을 λ―ΈμΉ¨
/*reverse() : λ¬Έμžμ—΄ 인덱슀 μˆœλ²ˆμ„ μ—­μˆœμœΌλ‘œ μž¬λ°°μ—΄ν•œλ‹€. * 원본에 영ν–₯을 λ―ΈμΉ¨ * */ System.out.println("reverse() : " + sb2.reverse()); System.out.println("sb2 :" + sb2);
Java
볡사

WRAPPER 클래슀

β€’
상황에 따라 κΈ°λ³Έ νƒ€μž…μ˜ 데이터λ₯Ό μΈμŠ€ν„΄μŠ€ν™” ν•΄μ•Ό ν•˜λŠ” κ²½μš°λ“€μ΄ λ°œμƒν•œλ‹€.
β€’
κΈ°λ³Έ νƒ€μž…μ˜ 데이터λ₯Ό λ¨Όμ € μΈμŠ€ν„΄μŠ€λ‘œ λ³€ν™˜ ν›„ μ‚¬μš©ν•΄μ•Ό ν•˜λŠ”λ°
β€’
8κ°€μ§€μ˜ ν•΄λ‹Ήν•˜λŠ” κΈ°λ³Έ νƒ€μž…μ˜ 데이터λ₯Ό μΈμŠ€ν„΄μŠ€ν™” ν•  수 μžˆλ„λ‘ ν•˜λŠ”
클래슀λ₯Ό 래퍼클래슀 라고 ν•œλ‹€

BOXINGκ³Ό UNBOXING

*λ°•μ‹±(Boxing)κ³Ό μ–Έλ°•μ‹±(UnBoxing) * κΈ°λ³Ένƒ€μž…μ„ 래퍼클래슀의 μΈμŠ€ν„΄μŠ€λ‘œ μΈμŠ€ν„΄μŠ€ν™” ν•˜λŠ” 것을 λ°•μ‹±(Boxing)이라고 ν•˜λ©°, * 래퍼클래슀 νƒ€μž…μ˜ μΈμŠ€ν„΄μŠ€λ₯Ό κΈ°λ³Ένƒ€μž…μœΌλ‘œ λ³€κ²½ν•˜λŠ” 것을 μ–Έλ°•μ‹±(Unboxing)이라고 ν•œλ‹€. * */ *μ˜€ν†  λ°•μ‹±(AutoBoxing)κ³Ό μ˜€ν†  μ–Έλ°•μ‹±(AutoUnBoxing)은 * JDK 1.5 λΆ€ν„° λ°•μ‹±κ³Ό 언박싱이 ν•„μš”ν•œ μƒν™©μ—μ„œ μžλ°” μ»΄νŒŒμΌλŸ¬κ°€ 이λ₯Ό μžλ™μœΌλ‘œ μ²˜λ¦¬ν•΄μ€€λ‹€. * 이런 μžλ™ν™”λœ λ°•μ‹±κ³Ό 언박싱을 μ˜€ν† λ°•μ‹±, μ˜€ν†  언박싱이라고 λΆ€λ₯Έλ‹€. * */ int intValue = 20; Integer boxingNumber1 = new Integer(intValue); //μΈμŠ€ν„΄μŠ€ν™” - λ°•μ‹±(Boxing) //μƒμ„±μž Integer boixngNumber2 = Integer.valueOf(intValue); //staticλ©”μ†Œλ“œμ΄μš© Integer boxingNumber3 = intValue; //μ˜€ν† λ°•μ‹± int unBoxingNumber1 = boxingNumber1.intValue(); //μ–Έλ°•μ‹±(UNBOXIGN) int unBoxingNumber2 = boxingNumber3; //μ˜€ν†  μ–Έλ°•μ‹±
Java
볡사

Calender 클래슀

*Calendar ν΄λž˜μŠ€λŠ” abstract 클래슀둜 μž‘μ„±λ˜μ–΄ 있기 λ•Œλ¬Έμ— *λ”°λΌμ„œ Calendar 클래슀λ₯Ό μ΄μš©ν•΄μ„œ μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜λŠ”κ²ƒμ΄ λΆˆκ°€λŠ₯ν•˜λ‹€. **/ * Calendar 클래슀λ₯Ό μ΄μš©ν•œ μΈμŠ€ν„΄μŠ€ μƒμ„±λ°©λ²•μ—λŠ” 두가지 방법이 μžˆλ‹€. * 1.getInstance() static λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•΄μ„œ μΈμŠ€ν„΄μŠ€λ₯Ό λ°˜ν™˜λ°›λŠ” 방법 * 2.ν›„μ†ν΄λž˜μŠ€μΈ GreogorianCalendar 클래슀λ₯Ό μ΄μš©ν•˜μ—¬ μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜λŠ” 방법 * */ 1. getInstance static λ©”μ†Œλ“œ 이용*/ // Calendar calendar = new Calendar(); // μΆ”μƒν΄λž˜μŠ€λ‘œ μΈμŠ€ν„΄μŠ€μƒμ„±λΆˆκ°€. Calendar calendar = Calendar.getInstance(); System.out.println(calendar); -------------------------------------------------------------------- 2. GrogorianCalendar μ΄μš©ν•˜λŠ” 방법*/ 2-1 κΈ°λ³Έμƒμ„±μž μ‚¬μš©*/ Calendar gregorianCalendar = new GregorianCalendar(); System.out.println(gregorianCalendar); *2-2. λ…„, μ›”, 일, μ‹œ, λΆ„, 초 정보λ₯Ό μ΄μš©ν•΄μ„œ μΈμŠ€ν„΄μŠ€λ₯Ό 생성 * 2014λ…„ 9μ›” 18일 16μ‹œ 42λΆ„ 00초 (월은 0이 1월이닀 κ·Έλž˜μ„œ 0~11κΉŒμ§€λ§Œ μ·¨κΈ‰)*/ int year = 2014; int month = 8; //(월은 0이 1월이닀 κ·Έλž˜μ„œ 0~11κΉŒμ§€λ§Œ μ·¨κΈ‰, 즉 8이 9월을 의미) int dayOfMonth = 18; int hour = 16; int min = 42; int second = 0; Calendar birthDay = new GregorianCalendar(year,month, dayOfMonth, hour, min, second); System.out.println(12); System.out.println(birthDay); * 상황에 따라 νŠΉμ •μΌμžλ₯Ό κΈ°μ€€μœΌλ‘œ ν•œ Date νƒ€μž…μ˜ μΈμŠ€ν„΄μŠ€κ°€ ν•„μš”ν•œ κ²½μš°λ„ μžˆλ‹€. * λ¨Όμ € GregorianCalendarλ₯Ό μ΄μš©ν•΄μ„œ νŠΉμ • λ‚ μ§œ/μ‹œκ°„ μ •λ³΄λ‘œ μΈμŠ€ν„΄μŠ€λ₯Ό 생성 ν•œ ν›„ * 1970λ…„ 1μ›” 1일 μžμ •μ„ κΈ°μ€€μœΌλ‘œ μ§€λ‚œ μ‹œκ°„μ„ millisecond둜 κ³„μ‚°ν•΄μ„œ longν˜•μœΌλ‘œ λ°˜ν˜Έλ‚˜ν•˜λŠ” λ©”μ†Œλ“œκ°€ μžˆλ‹€.*/ System.out.println(birthDay.getTimeInMillis()); Date date = new Date(birthDay.getTimeInMillis()); System.out.println(date); Date date2 = new Date(new GregorianCalendar(year, month, dayOfMonth, hour, min ,second) .getTimeInMillis()); System.out.println(date2);
Java
볡사

μƒμ„±ν•œ μΈμŠ€ν„΄μŠ€μ˜ ν•„λ“œ 정보λ₯Ό Calendar 클래슀의 get()λ©”μ†Œλ“œλ‘œ λ°˜ν™˜ 받을 수 μžˆλ‹€.

int birthYear = birthDay.get(1); int birthYear2 = birthDay.get(Calendar.YEAR); int birthMonth2 = birthDay.get(2); int birthMonth = birthDay.get(Calendar.MONTH); int birthDayofMonth = birthDay.get(Calendar.DAY_OF_MONTH); int birthDayofMonth2 = birthDay.get(3);
Java
볡사

μš”μΌ - 일(1), μ›”(2), ν™”(3), 수 (4), λͺ©(5), 금(6), ν† (7)

System.out.println("dayOfWeek : " + birthDay.get(Calendar.DAY_OF_WEEK)); String day = ""; switch(birthDay.get(Calendar.DAY_OF_WEEK)) { case Calendar.SUNDAY : day = "일"; break; case Calendar.MONDAY : day = "μ›”"; break; case Calendar.TUESDAY : day = "ν™”"; break; case Calendar.WEDNESDAY: day = "수 "; break; case Calendar.THURSDAY : day = "λͺ©"; break; case Calendar.FRIDAY : day = "금"; break; case Calendar.SATURDAY : day = "ν† "; break; } System.out.println("μš”μΌ : " + day); System.out.println("amPm : " + birthDay.get(Calendar.AM_PM)); System.out.println(birthDay.get(Calendar.AM_PM) == Calendar.AM ? "μ˜€μ „" : "μ˜€ν›„"); System.out.println("hourOfDay : " + birthDay.get(Calendar.HOUR_OF_DAY)); System.out.println("hour : " + birthDay.get(Calendar.HOUR)); System.out.println("min : " + birthDay.get(Calendar.MINUTE) ); System.out.println("second : " + birthDay.get(Calendar.SECOND)); }
Java
볡사