자바는 기본적으로 다양한 패키지를 지원 그중에서 가장 중요한 패키지, java.lang패키지의 클래스는 import를 하지 않고도 사용할 수 있다.
public class WrapperExam {
public static void main(String[] args) {
int i = 5;
Integer i2 = new Integer(5);
Integer i3 = 5; //오토박싱
int i4 = i2; //오토언박싱
long i2_long = i2.longValue();
long i4_long = i4.longValue(); // 오류 발생!
}
}
String str2 = new StringBuffer().append("hello").append(" ").append("world").toString();
System.out.println(str2);
String str3 = str1 + str2;
System.out.println(str3);
String str4 = new StringBuffer().append(str1).append(str2).toString();
// 아래 코드로 직접 확인해보자!!
public class StringBufferPerformanceTest{
public static void main(String[] args){
// (1) String의 +연산을 이용해서 10,000개의 *를 이어붙입니다.
//시작시간을 기록합니다.(millisecond단위)
long startTime1 = System.currentTimeMillis();
String str="";
for(int i=0;i<10000;i++){
str=str+"*";
}
//종료시간을 기록합니다.(millisecond단위)
long endTime1 = System.currentTimeMillis();
// (2) StringBuffer를 이용해서 10,000개의 *를 이어붙입니다.
//시작시간을 기록합니다.(millisecond단위)
long startTime2 = System.currentTimeMillis();
StringBuffer sb = new StringBuffer();
for(int i=0;i<10000;i++){
sb.append("*");
}
//종료시간을 기록합니다.(millisecond단위)
long endTime2 = System.currentTimeMillis();
// 방법(1)과 방법(2)가 걸린 시간을 비교합니다.
long duration1 = endTime1-startTime1;
long duration2 = endTime2-startTime2;
System.out.println("String의 +연산을 이용한 경우 : "+ duration1);
System.out.println("StringBuffer의 append()을 이용한 경우 : "+ duration2);
}
}
int value1 = Math.max(5, 20); // value1 = 5 : 5, 20중 큰 값을 고릅니다.
int value2 = Math.min(5, -5); // value2 = -5 : 5, -5중 작은 값을 고릅니다.
int value3 = Math.abs(-10); // value3 = 10 : -10의 절댓값을 구합니다
double value4 = Math.random(); // 0이상 1.0 미만의 랜덤한 실수값을 구합니다.
double value5 = Math.sqrt(25); // value5 = 5.0 : 25의 제곱근을 구합니다.
// print 실습
System.out.println("2의 10승 = " + Math.pow(2, 10));
System.out.println("16의 1/2승 = : " + Math.pow(16, 0.5));
System.out.println("log200 = " + Math.log10(200));