728x90
포멧을 이용한 출력
C언어에서 사용하던 printf() 메서드를 지원한다.

class FormatPrintTest
{
 public static void main( String [] args )
 {
  System.out.printf("%s %d","Hello",123);
 }// end main
}

728x90


Typesafe Enumeration : enum 상수를 표현할때 사용
특정 그룹에 대한 상태값을 정수로 표현하고자 할때 이전에는 클래스를 사용해서 표현했다.

이것을

public class YoeIL
{
public static final int SUN = 0;
public static final int MON = 1;
public static final int TUE = 2;
public static final int WED = 3;
public static final int THU = 4;
public static final int FRI = 5;
public static final int SAT = 6;
}

요로케 쓴다..

public enum YoeIL{SUN,MON,TUE,WED,THU,FRI,SAT};
                              0      1       2     3      4      5    6

typesafe Enumeration은 내부적으로 클래스로 처리되며, 요일들은 모두 정수형으로 변환된다.
class TypesafeTest
{
 public enum YoeIL{SUN,MON,TUE,WED,THU,FRI,SAT};
 
 public static void main( String [] args )
 {
  for(YoeIL menu : YoeIL.values()) //values() 는 상수 이름을 얻어온다.
  {
   System.out.println(menu);
  }
  
  for(YoeIL menu : YoeIL.values())
  {
   switch(menu)
   {
   case SUN:
    System.out.print("일요일 ");  break;
   case MON:
    System.out.print("월요일 ");  break;
   case TUE:
    System.out.print("화요일 ");  break;
   case WED:
    System.out.print("수요일 ");  break;
   case THU:
    System.out.print("목요일 ");  break;
   case FRI:
    System.out.print("금요일 ");  break;
   case SAT:
    System.out.println("토요일");  break;
   }
  }
 }// end main
}
728x90
 Java 1.4버전  Java 1.5버전 이후
 int a = 10;
 Integer A  = new Integer(a);
 Integer A = 10; 
 Integer B = new Integer(10);
 int b = B.intValue();
 Integer B = new Integer(20);
 int b = B;

 Integer A = 10;                 -->  Autoboxing
                                      Stack 영역에 있던 값이 heap영역에 저장된다.

 Integer B = new Integer(20);    --> AutoUnboxing
 int b = B;                           heap영역에 있던 존재하는 레퍼런스형 객체의 값이 Stack영역에 저장된다.

class AutoUnboxingTest
{
 int a = 10;
 Integer A = a;  // Autoboxing 
 
 Integer B = new Integer(20);
 int b = B;      // AutoUnboxing
 
 //System.out.println("A:"+ A);
 //System.out.println("b:"+ b);
 
 //이전
 
 Integer C = new Integer(100);
 Integer D = new Integer(200);
 
 int c = C.intValue();
 int d = D.intValue();
 
 System.out.println(c+"2진 :"+Integer.toBinaryString(c));
 System.out.println(c+"8진 :"+Integer.toOctalString(c));
 System.out.println(c+"16진:"+Integer.toHexString(c));
}
728x90

Java SE 5.0 에 추가된 컬렉션을 위한 반복문

< 형식 >
   for(자료형  변수명 : Collection자료)
{
            처리문;
}
기존방법

public  void  oldFor ( Clollection c )
{
     for( Iterator  i = c.iterator() : i.hasNext() ;)
         {
               String str = (String)i.next();
               sb.append(str);
          }
}

새로운 방법

public void newFor( Collection<String>c)
{
    for(String str:c)
       {  
            sb.append(str);
        }
}
class ForTest
{
 public static void main( String [] args )
 {
  String [] arr = new String[]{"소녀시대","카라","원더걸스"};
  for(String a:arr)
  {
   System.out.println(a);
  }
  System.out.println();
  
 }// end main
}

+ Recent posts