글
java5 로 넘어오면서 많이 바꼈는데 가장 맘에드는건 저너릭스와 enum 타입이 생긴것이다.
annotation 은 어떤곳에 써야할지 아직은 감이 안잡힌다.
1. static 멤버 import 하기
- static import 를 사용하면 클래스명을 안쓰고 static 멤버변수와 static 메소드에 접근할수 있다.
import static java.lang.Math.*;
public class StaticImportTest {
public static void main(String args[]) {
double y = sin(1.2);
System.out.println("SIN(1.2) : " + y);
System.out.println("PI : " + PI);
}
}
2. Eunumerated 타입
- c 언어의 enum 타입처럼 자동으로 숫자를 매겨주는 변수를 만들수 있다
- 이것과 관련해서 enumMap 이라는 클래스도 있다
public class EnumTest {
public enum testEnum {ITEM1,ITEM2,ITEM3};
public static void main(String args[]){
System.out.println(testEnum.ITEM1);
}
}
3. 새로운 for 문 형태
- 자바스크립트의 for in 구문 같이 배열을 자동으로 탐색할수있다.
- 단 for 문 안에서 꼭 String tmp 와 같은형태로 선언해야 한다는것은 좀 이상했다.
public class NewForTest {
public static void main(String args[]) {
String strs[] = {"test1","test2","test3"};
for (String tmp : strs) {
System.out.println(tmp);
}
}
}
4. autoBoxing, autoUnBoxing
- wrapper 클래스로 변환히 한층 더 자유로워졌다.
public class AutoBoxingTest {
public static void main(String args[]) {
Integer i = 10;
int i2 = i;
System.out.println("I:" + i2);
}
}
5. c언어의 printf 와 유사한기능 추가
- . 뒤에있는것이 소숫점이하 자릿수가 아니라 전체 자릿수이다.
public class PrintfTest {
public static void main(String args[]) {
double PI = 3.141592;
System.out.printf("%7.3g",PI);
}
}
6. 가변 파라메터(varargs)를 갖는 메소드
- 이건 왜 이제야 지원하는지 의심스럽다.
public class VarargsTest {
public static int sum(int... data) {
int total = 0;
for (int n : data) {
total += n;
}
return total;
}
public static void main(String args[]) {
int total = sum(1,2,3,4,5,6,7,8,9,10);
System.out.println("SUM : " + total);
}
}
7. 저너릭스(generics)
- 이제 클래스 생성할때 리턴 타입도 지정받을수 있게 됬다.
- 아래 예제를 보면 알겠지만 클래스를 만들때 리턴 타입까지 고려해서 만들수가 있게되었다.
class MyMemory<M> {
private int size = 0;
private Object objects[];
public MyMemory(int size) {
this.size = size;
objects = new Object[size];
}
public M get(int i) {
return (M)objects[i];
}
public void put(int i,M obj) {
objects[i] = obj;
}
public int getSize() {
return size;
}
}
public class GenericsTest {
public static void main(String args[]) {
MyMemory<String> mem = new MyMemory<String>(10);
mem.put(1, "test");
mem.put(2, "test2");
mem.put(3, "test3");
System.out.println(mem.get(1));
System.out.println(mem.get(2));
System.out.println(mem.get(3));
}
}
8. 주석(annotation)
- 이놈은 좀 길기때문에 다음회에 연재하겠다.
'알짜정보 > Java' 카테고리의 다른 글
JNLP 테스트 (36) | 2010.02.14 |
---|---|
JAVA 리눅스 IP 주소 알아내기 (38) | 2008.12.02 |
apache 프로젝트의 commons-dbcp 를 이용하여 connection pool 만들기. (42) | 2008.08.15 |
java 에서 xmlrpc 사용하기 (38) | 2008.07.13 |
annotation 주석? (43) | 2006.12.16 |
RECENT COMMENT