반응형
API (Application Programming Interface)
- 프로그램 개발에 자주 사용되는 class 및 Interface의 모음.
- 유용한 기능
- 요 3가지가 java.lang / util / IO 는 기본 중의 기본이다.
StringBulider
- String은 메모리를 과소비한다는 단점이 있다. 메모리 사용의 단점을 보완한 것이 StringBulider이다.
➰ StringBulider의 사용
package day19.api.lang.sb;
public class StrBufferEx {
public static void main(String[] args) {
String str = new String("Java");
StringBuffer sb = new StringBuffer("Java");
System.out.println(str);
System.out.println(sb);
str = str + "Program";
sb.append("Program");
System.out.println(str);
System.out.println(sb);
//문자열 마지막에 추가
sb.append("기존 문자열 마지막에 추가");
System.out.println(sb);
//중간에 추가
sb.insert(11, "-");
System.out.println(sb);
//문자열 변경
sb.replace(0, 6, "자바");
System.out.println(sb);
//문자열 삭제
sb.delete(0, 2);
System.out.println(sb);
//문자열로 형변환
//System.out.println(str.equals(sb));
String result = sb.toString();
//거꾸로
sb.reverse();
System.out.println(sb);
}
}
➰ String과 StringBulider의 실행 속도 차이
package day19.api.lang.sb;
public class StringText {
public static void main(String[] args) {
long start = System.currentTimeMillis();
String str = "";
for(int i = 1; i < 300000; i++) {
str += "A";
}
long end = System.currentTimeMillis();
long start_sb = System.currentTimeMillis();
StringBuffer sb = new StringBuffer("");
for(int i = 1; i < 300000; i++) {
sb.append("A");
}
long end_sb = System.currentTimeMillis();
System.out.println("String 걸린 시간: " + (end - start) * 0.001);
System.out.println("StringBuffer 걸린 시간: " + (end_sb - start_sb) * 0.001);
}
}
<결과값>
String 걸린 시간: 10.061
StringBuffer 걸린 시간: 0.011
Math
- 수학계산에 사용할 수 있는 method들 제공
- static으로 선언되어있다. Math.~~로 사용.
➰ Math API 사용
package day19.api.lang.math;
public class MathEx {
public static void main(String[] args) {
Math.random();
//올림
double d = Math.ceil(1.3);
System.out.println(d);
//내림
double d2 = Math.floor(1.2);
System.out.println(d2);
//반올림
double d3 = Math.round(3.14);
System.out.println(d3);
//16의 루트값
double d4 = Math.sqrt(16);
System.out.println(d4);
//절대값
double d5 = Math.abs(-4);
System.out.println(d5);
//큰값
int i1 = Math.max(5, 3);
System.out.println(i1);
}
}
Wrapper class
- 기본타입을 포장객체로 만드는 과정을 boxing이라고 한다.
- ex) int -> Integer
- 반대의 경우는 unboxing이라고 한다.
- ex) Integer -> int
- 1.8 버전 부터 AutoBoxing을 지원한다.
➰ Boxing 예시 (AutoBoxing 미지원시 이런식으로 사용해왔다)
package day19.api.lang.wrapper;
public class Boxing {
public static void main(String[] args) {
int a = 100;
double b = 3.14;
long c = 200L;
// 기본형 -> 객체형 포장하는 작업을 boxing
Integer val = new Integer(a);
Double val2 = new Double(b);
Long val3 = new Long(c);
Object[] arr = {val, val2, val3}; //객체형 변수를 object배열에 저장
//객체형 -> 기본형 변경하는 작업 unboxing
int a1 = val.intValue();
double b1 = val2.doubleValue();
long c1 = val3.longValue();
}
}
➰ AutoBoxing 사용
package day19.api.lang.wrapper;
public class Autoboxing {
public static void main(String[] args) {
//AutoBoxing 자동형변환
int a = 100;
Integer val = a;
a = val;//상호형변환 가능.
Object[] arr = {1, 2, 3};
int v = Integer.parseInt("3");
int v2 = Integer.parseInt("34");
System.out.println(v + v2);
double v3 = Double.parseDouble("3.14");
long v4 = Long.parseLong("234234234242");
System.out.println(v + v2 + v3 + v4);
}
}
반응형