ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA μžλ°” ν•΅μ‹¬ν΄λž˜μŠ€
    JAVA 2020. 8. 16. 21:42

    🎯 μžλ°”μ˜ String 클래슀, Wrapper 클래슀 λ“± λ‹€μ–‘ν•œ 핡심 클래슀λ₯Ό μ•Œμ•„λ³Έλ‹€.

     

     

     

    String 클래슀

    public class StringTest {
    	public static void main(String[] args) {
    		String a = "hello";
    		String b = "hello";
    		System.out.println("a==b "+(a==b));
    		System.out.println("a.equals(b)======= "+(a.equals(b)));
    		String c = new String("hello");
    		String d = new String("hello");
    		System.out.println("c==d "+(c==d));
    		System.out.println("c.equals(d)======= "+(c.equals(d)));
    	}
    }
    
    -------------------------
    a==b true
    a.equals(b)======= true
    c==d false
    c.equals(d)======= true

    String 클래슀둜 helloλΌλŠ” λ¬Έμžμ—΄μ„ μƒμ„±ν•˜λ €λ©΄ λ¦¬ν„°λŸ΄μ„ μ΄μš©ν•˜λŠ” 방법과 newλ₯Ό μ΄μš©ν•˜λŠ” 방법이 μžˆλ‹€. λ³€μˆ˜ a, b 처럼 λ¦¬ν„°λŸ΄μ„ μ΄μš©ν•˜λ©΄ λ™μΌν•œ λ¬Έμžμ—΄μ΄ 이미 μ‘΄μž¬ν•  경우 μƒˆλ‘œ μƒμ„±λ˜μ§€ μ•Šκ³  μž¬μ‚¬μš©ν•˜κ²Œ λœλ‹€. λ°˜λŒ€λ‘œ λ³€μˆ˜ c, d 처럼 newλ₯Ό μ΄μš©ν•˜λ©΄ λ™μΌν•œ λ¬Έμžμ—΄μ΄ 이미 μ‘΄μž¬ν•˜λ”λΌλ„ 맀번 μƒˆλ‘­κ²Œ μƒμ„±λœλ‹€. λ¬Έμžμ—΄μ€ μ°Έμ‘° λ°μ΄ν„°ν˜•μ΄κΈ° λ•Œλ¬Έμ— equals() λ©”μ†Œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ”© ν•΄μ„œ λ‚΄μš©μ„ 동등비ꡐ할 수 μžˆλ‹€. 

     

    valueOf()

    		String z = String.valueOf(true);
    		String z2 = String.valueOf(10);
    		String z3 = String.valueOf(3.24F);
    		String z4 = String.valueOf(new char[] {'A','B'});
    		String z5 = String.valueOf(4.5D);
    		String z6 = String.valueOf(125L);

    valueOf()λŠ” λ¬Έμžμ—΄μ΄ μ•„λ‹Œ 데이터λ₯Ό λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•΄μ£ΌλŠ” λ©”μ†Œλ“œμ΄λ‹€.

     

    length(), trim()

    		String t = "  hEllo   ";
    		System.out.println(t.length());
    		System.out.println(t.trim().length());

    length()λ©”μ†Œλ“œλŠ” λ¬Έμžμ—΄μ˜ 길이λ₯Ό λ°˜ν™˜ν•˜λ©° trim() λ©”μ†Œλ“œλŠ” 곡백문자λ₯Ό μ œκ±°ν•œλ‹€.

     

    toCharArray()

    		// String클래슀의 λ©”μ„œλ“œ μ‚¬μš©λ²•
    		String xyz = "helloworld";
    		char[] xyz2 = xyz.toCharArray();
    		for(int i = 0; i < xyz2.length; i++) {
    			System.out.println("인덱슀 " + i + " = " + xyz2[i]);
    		}

    toCharArray()λŠ” λ¬Έμžμ—΄μ„ 문자 λ°°μ—΄λ‘œ λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œμ΄λ‹€. λ°˜λ³΅λ¬Έμ„ λŒλ €μ„œ 좜λ ₯해보면 ν•œ κΈ€μžμ”© 배열에 λ“€μ–΄μžˆλŠ” 것을 λ³Ό 수 μžˆλ‹€.

    #μ‹€ν–‰κ²°κ³Ό
    
    인덱슀 0 = h
    인덱슀 1 = e
    인덱슀 2 = l
    인덱슀 3 = l
    인덱슀 4 = o
    인덱슀 5 = w
    인덱슀 6 = o
    인덱슀 7 = r
    인덱슀 8 = l
    인덱슀 9 = d

     

    substring()

    		String xyz = "helloworld";
    		String q = xyz.substring(3);
    		String q2 = xyz.substring(3, 7);
    		System.out.println(q);  //loworld
    		System.out.println(q2);  //lowo

    substring() λ©”μ†Œλ“œλŠ” νŠΉμ • 인덱슀 λΆ€λΆ„μ˜ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•œλ‹€. q2λŠ” 인덱슀 3λΆ€ν„° 인덱슀 7 μ „κΉŒμ§€μ˜ λ¬Έμžμ—΄μ„ κ°–κ²Œλœλ‹€.

     

    split()

    		String mesg = "홍길동,μ΄μˆœμ‹ ,μœ κ΄€μˆœ";
    		String[] n = mesg.split(",");
            
    		System.out.println(n[0]);
    		System.out.println(n[1]);
    		System.out.println(n[2]);
    		System.out.println(n.length);
            
    		for (int x = 0; x < n.length; x++) {
    			System.out.println(n[x]);
    		}

    split() λ©”μ†Œλ“œλŠ” νŠΉμ • 문자λ₯Ό κΈ°μ€€μœΌλ‘œ λ¬Έμžμ—΄μ„ κ΅¬λΆ„ν•˜μ—¬ λ°°μ—΄λ‘œ λ°˜ν™˜ν•œλ‹€. 

     

    charAt()

    		String s ="hello";
    		char x = s.charAt(0);
    		System.out.println(x);
    		System.out.println(s.charAt(1));

    charAt()은 νŠΉμ • μΈλ±μŠ€κ°’μ˜ 문자λ₯Ό λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œμ΄λ‹€.

     

    concat()

    String s ="hello";
    String x = s.concat("world");
    System.out.println(x);

    concat()λ©”μ†Œλ“œλŠ” λ¬Έμžμ—΄μ„ μ—°κ²°ν•œλ‹€.

     

    equalsIgnoreCase()

    		String aaa = "Hello";
    		String aaa2 = "hello";
            
    		boolean result = aaa.equals(aaa2);
    		System.out.println(result);  //false
            
    		boolean result2 = aaa.equalsIgnoreCase(aaa2);
    		System.out.println(result2);  //true

    equalsIgnoreCase() λ©”μ†Œλ“œλŠ” λŒ€μ†Œλ¬Έμž ꡬ별없이 λ¬Έμžμ—΄μ΄ μΌμΉ˜ν•˜λŠ”μ§€ λΉ„κ΅ν•΄μ„œ λΆˆλ¦¬μ–Έ νƒ€μž…μœΌλ‘œ λ°˜ν™˜ν•œλ‹€.

     

    contains()

    String s = "hello";
    
    boolean kkk = s.contains("h");
    System.out.println(kkk);
    
    boolean kkk2 = s.contains("hx");
    System.out.println(kkk2);

    contains() λ©”μ†Œλ“œλŠ” νŠΉμ • 문자λ₯Ό κ°–κ³  μžˆλŠ”μ§€ κ²€μ‚¬ν•΄μ„œ λΆˆλ¦¬μ–Έ νƒ€μž…μœΌλ‘œ λ¦¬ν„΄ν•œλ‹€.

     

    endsWith(), startsWith()

    boolean kkk3 = s.endsWith("o");
    System.out.println(kkk3);
    
    boolean kkk4 = "world".endsWith("o");
    System.out.println(kkk4);
    
    boolean kkk5 = "world".startsWith("w");
    System.out.println(kkk5);

    endsWith()λŠ” λ§ˆμ§€λ§‰ λ¬Έμžμ—΄μ„ λΉ„κ΅ν•˜κ³  startsWith()λŠ” μ‹œμž‘ν•˜λŠ” λ¬Έμžμ—΄μ„ λΉ„κ΅ν•΄μ„œ λΆˆλ¦¬μ–Ένƒ€μž…μœΌλ‘œ λ¦¬ν„΄ν•œλ‹€.

     

    format()

    String ppp = String.format("%s %d", "홍길동", 20);
    //홍길동 20

    νŠΉμ • 포맷에 λ§žλŠ” λ¬Έμžμ—΄μ„ λ¦¬ν„΄ν•œλ‹€.

     

    indexOf()

    int index = "hello".indexOf("a");
    System.out.println(index);  //-1

    νŠΉμ • 문자의 인덱슀 값을 λ°˜ν™˜ν•œλ‹€. μΌμΉ˜ν•˜μ§€ μ•Šμ„ 경우 -1을 λ¦¬ν„΄ν•œλ‹€.

     

    isEmpty()

    System.out.println("".isEmpty());  //true
    System.out.println("".length());  //0

    νŠΉμ • λ¬Έμžμ—΄μ΄ λΉ„μ–΄μžˆλŠ”μ§€ κ²€μ‚¬ν•΄μ„œ λΆˆλ¦¬μ–Έ νƒ€μž…μœΌλ‘œ λ¦¬ν„΄ν•œλ‹€.

     

    replace('a', 'b')

    System.out.println("abc".replace('a', 'A'));
    System.out.println("abc".replace("ab", "xxx"));

    λ¬Έμžμ—΄ aλ₯Ό b둜 μΉ˜ν™˜ν•œλ‹€.

     

    toUpperCase(), toLowerCase()

    System.out.println("abc".toUpperCase());
    System.out.println("aBdE".toLowerCase());

    toUpperCase()λŠ” λ¬Έμžμ—΄μ„ λŒ€λ¬Έμžλ‘œ λ°”κΏ”μ£Όλ©°, toLowerCase()λŠ” λ¬Έμžμ—΄μ„ μ†Œλ¬Έμžλ‘œ λ°”κΏ”μ€€λ‹€.

     

     

    Wrapper클래슀

    κΈ°λ³Έν˜• 데이터 νƒ€μž…κ³Ό λ™μΌν•œ 값을 ν‘œν˜„ν•  수 μžˆλŠ” 8개의 클래슀λ₯Ό λ§ν•œλ‹€. κΈ°λ³Έ 데이터λ₯Ό 클래슀둜 λ‘˜λŸ¬μ‹Έκ³  래퍼클래슀의 λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•΄ 데이터λ₯Ό 효율적으둜 μ²˜λ¦¬ν•  수 μžˆλ‹€.

    //Wrapper 클래슀 : κΈ°λ³Έν˜• 8κ°œμ— ν•΄λ‹Ήν•˜λŠ” ν΄λž˜μŠ€λ“€.
    				
    *   Byte
    *   Short
    *   Integer
    *   Long
    
    *   Float
    *   Double
    
    *   Character
    
    *   Boolean
    //Wrapper 클래슀의 μ£Όμš” μƒμˆ˜
    
    System.out.println(Integer.MIN_VALUE);
    System.out.println(Integer.MAX_VALUE);
    // μ£Όμš” λ©”μ„œλ“œ
    
    //λ¬Έμžμ—΄ "123"을 μ •μˆ˜ 123으둜 λ³€κ²½
    String str = "123";
    int a = Integer.parseInt(str);
    		
    //λ¬Έμžμ—΄ "3.14F"λ₯Ό μ‹€μˆ˜ 3.14F둜 λ³€κ²½
    String str2 = "3.14F";
    float b = Float.parseFloat(str2);
    		
    //μ •μˆ˜ 123을 λ¬Έμžμ—΄ "123"으둜 λ³€κ²½
    String intStr = Integer.toString(123);
    		
    //μ‹€μˆ˜ 3.14Fλ₯Ό λ¬Έμžμ—΄ "3.14F"둜 λ³€κ²½
    String floatString = Float.toString(3.14F);

    parseInt()λŠ” λ¬Έμžμ—΄λ‘œ 된 μˆ˜μΉ˜λ°μ΄ν„°λ₯Ό μ—°μ‚°κ°€λŠ₯ν•œ Intν˜•μœΌλ‘œ λ³€κ²½ν•˜λŠ” λ©”μ†Œλ“œμ΄λ‹€. λ‹€μ‹œ μˆ˜μΉ˜λ°μ΄ν„°λ₯Ό λ¬Έμžμ—΄λ‘œ λ³€κ²½ν•  땐 String.valueOf() λ˜λŠ” Integer.toString() λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€.

     

    μ˜€ν† λ°•μ‹±κ³Ό μ–Έλ°•μ‹±

    int n =10;
    	  
    Integer n2 = new Integer(10); //μ •μˆ˜λ₯Ό 객체둜
    int n3 = n2.intValue(); // 객체λ₯Ό μ •μˆ˜λ‘œ
    
    // Integer --> int : μ˜€ν†  μ–Έλ°•μ‹±
    int n4 = n2;  
    
    //  int--> Integer : μ˜€ν† λ°•μ‹±
    Integer n5 = n4;

    κΈ°λ³Έν˜• 데이터λ₯Ό 래퍼 클래슀둜 λ§Œλ“œλ €λ©΄ μƒμ„±μžμ— κΈ°λ³Έν˜•μ„ 인자둜 μ „λ‹¬ν•˜λ©΄ λœλ‹€. 이 μƒνƒœμ—μ„œ λ‹€μ‹œ κΈ°λ³Έν˜•μ„ λ§Œλ“œλ €λ©΄ value λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ λœλ‹€. κΈ°λ³Έν˜•κ³Ό 래퍼클래슀(μ°Έμ‘°ν˜•)λŠ” μ„œλ‘œ λ‹€λ₯Έ λ°μ΄ν„°νƒ€μž…μ„ κ°€μ‘ŒκΈ° λ•Œλ¬Έμ— μ„œλ‘œ λ³€ν™˜ν•˜λ €λ©΄ μ΄λ ‡κ²Œ μΆ”κ°€ μž‘μ—…μ΄ ν•„μš”ν•˜λ‹€. ν•˜μ§€λ§Œ JDK 1.5 버전뢀터 μ„œλ‘œκ°„ μžλ™ λ³€ν™˜ κΈ°λŠ₯이 μ œκ³΅λ˜μ–΄μ„œ μΆ”κ°€μž‘μ—…μ΄ ν•„μš”μ—†μ–΄μ‘Œλ‹€.

    μ˜€ν† λ°•μ‹±μ€ κΈ°λ³Έλ°μ΄ν„°ν˜•μ—μ„œ 래퍼클래슀둜 μžλ™λ³€ν™˜λ˜λŠ” κΈ°λŠ₯이고 언박싱은 λž˜νΌν΄λž˜μŠ€μ—μ„œ κΈ°λ³Έλ°μ΄ν„°ν˜•μœΌλ‘œ μžλ™λ³€ν™˜λ˜λŠ” κΈ°λŠ₯이닀.

    Object [] arr = {10, "홍길동", true, 3.14};

    였브젝트 νƒ€μž… λ°°μ—΄ μ•ˆμ— μ°Έμ‘°ν˜•λ°μ΄ν„° "홍길동" 뿐만 μ•„λ‹ˆλΌ κΈ°λ³Έν˜• λ°μ΄ν„°κΉŒμ§€ λ‹΄κ³ μžˆλ‹€. κΈ°λ³Έν˜• 데이터가 μ˜€ν† λ°•μ‹±λ˜μ–΄ μžλ™μœΌλ‘œ 래퍼둜 λ³€ν™˜λœ ν›„ μ €μž₯된 μƒνƒœμ΄λ‹€.

     

     

    Random 클래슀

    import java.util.Random;
    
    public class RandomTest {
    	public static void main(String[] args) {
    	       Random  ran  = new Random();  
    	       int n = ran.nextInt();
    	       int m = ran.nextInt();
    	       int z= ran.nextInt(100);
    	       System.out.println(n);
    	       System.out.println(m);
    	       System.out.println(z);
    	}
    }

    λžœλ€ν΄λž˜μŠ€λŠ” μ •μˆ˜, μ‹€μˆ˜, λΆˆλ¦¬μ–Έ νƒ€μž…μ˜ 랜덀 값을 λ§Œλ“€ 수 μžˆλŠ” ν΄λž˜μŠ€μ΄λ‹€. nextInt()λŠ” μž„μ˜μ˜ μ •μˆ˜κ°’μ„ κ΅¬ν•˜λŠ” λ©”μ†Œλ“œμ΄λ‹€. 

     

    		Random random = new Random();
    		int num = random.nextInt(100)+1;
    		System.out.println("1λΆ€ν„° 100 μ‚¬μ΄μ˜ μž„μ˜μ˜ κ°’:"+num);
    		
    		float f = random.nextFloat();
    		System.out.println("0.0λΆ€ν„° 1.0 μ‚¬μ΄μ˜ μž„μ˜μ˜ κ°’:"+f);
    		
    		boolean b = random.nextBoolean();
    		System.out.println("μž„μ˜μ˜ 논리값 : "+b);

     random.nextInt(100)은 0λΆ€ν„° 99κΉŒμ§€μ˜ μž„μ˜μ˜ μ •μˆ˜κ°’μ„ λ¦¬ν„΄ν•˜κΈ° λ•Œλ¬Έμ— 1을 더해쀀닀. 

     

     

    StringTokenizer

    		String data = "aa/bb/cc,dd/ee/xx,kk/yy/zz";	
    		StringTokenizer st = new StringTokenizer(data, "/ ,");
    		while(st.hasMoreTokens()) {
    			System.out.println(st.nextToken());
    		}
    		
            	// String의 split()
    		String [] d = data.split("/");
    		for (String s : d) {
    			System.out.println(s);
    		}

    λ¬Έμžμ—΄μ„ νŠΉμ • κ΅¬λΆ„μžλ‘œ 뢄리할 수 μžˆλŠ” ν΄λž˜μŠ€μ΄λ‹€. μœ„μ—μ„œ λ³Έ String 클래슀의 split() λ©”μ†Œλ“œμ™€ λΉ„μŠ·ν•˜λ‹€. μŠ€νŠΈλ§ν† ν¬λ‚˜μ΄μ € 객체λ₯Ό μƒμ„±ν•˜κ³  μƒμ„±μžμ˜ μΈμžκ°’μœΌλ‘œ λ¬Έμžμ—΄κ³Ό κ΅¬λΆ„μžλ₯Ό 지정할 수 μžˆλ‹€. κ΅¬λΆ„μžλ₯Ό μƒλž΅ν•˜λ©΄ 곡백이 κΈ°λ³Έκ΅¬λΆ„μžλ‘œ μ μš©λœλ‹€.

    hasMoreTokens()λŠ” λ‚¨μ•„μžˆλŠ” 토큰이 μžˆλŠ”μ§€ λΆˆλ¦¬μ–Ένƒ€μž…μœΌλ‘œ λ¦¬ν„΄ν•˜λŠ” λ©”μ†Œλ“œμ΄λ‹€. nextToken()은 λ‹€μŒν† ν°μ„ λ¬Έμžμ—΄λ‘œ λ°˜ν™˜ν•œλ‹€. 

     

     

    Date와 Calendar 클래슀

    λ‚ μ§œλ₯Ό μ²˜λ¦¬ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•˜λŠ” ν΄λž˜μŠ€μ΄λ‹€. Date클래슀 μ•ˆμ˜ λ§Žμ€ λ©”μ†Œλ“œκ°€ μ˜›λ‚ λ°©μ‹μ΄λΌ 일반적으둜 Calendar 클래슀λ₯Ό μ‚¬μš©ν•œλ‹€.

    		// java.util.Calendar
    		Calendar c = Calendar.getInstance();
    		System.out.println(c);
    		System.out.println(c.toString());
    		System.out.println("년도:" + c.get(Calendar.YEAR));
    		int month = c.get(Calendar.MONTH)+1;
    		System.out.println("μ›” :" + month);
    		int day = c.get(Calendar.DAY_OF_MONTH);
    		System.out.println("일 :" + day);
    		
    		int hour = c.get(Calendar.HOUR_OF_DAY);
    		int minute= c.get(Calendar.MINUTE);
    		int seconds= c.get(Calendar.SECOND);
    		System.out.println(hour+"\t"+minute+"\t"+seconds);
    		//λͺ…μ‹œμ μœΌλ‘œ μƒˆλ‘œμš΄ λ‚ μ§œμ„€μ •
    		//c.set(2019,10,18);
    		System.out.println(c.get(Calendar.YEAR));
    		System.out.println(c.get(Calendar.MONTH)+1);
    		System.out.println(c.get(Calendar.DAY_OF_MONTH));

    월을 λ°˜ν™˜ν•˜λŠ” monthλ₯Ό μ‚¬μš©ν•  땐 0λΆ€ν„° λ°˜ν™˜ν•˜κΈ° λ•Œλ¬Έμ— +1을 ν•΄μ€˜μ•Όν•œλ‹€.

    		//Calendar ==> Date 
            
    		//Date xxxx =c.getTime();
    		//System.out.println(xxxx);
    		
    		long yyyy = c.getTimeInMillis();
    		System.out.println(yyyy);
    		
    		long yyyy2= System.currentTimeMillis();
    		System.out.println(yyyy2);

     

     

    SimpleDateFormat

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyλ…„ MMμ›” dd일 HH:mm:ss (a)");
    String date = sdf.format(new Date());
    System.out.println(date);

    Dateν΄λž˜μŠ€μ—μ„œ 얻은 λ‚ μ§œλ°μ΄ν„°λ₯Ό νŠΉμ • ν˜•μ‹μ— λ§žμΆ°μ„œ 좜λ ₯ν•  수 μžˆλ‹€.

     

     

    DecimalFormat

    //1.숫자--> String : format
    		
    DecimalFormat df = new DecimalFormat("$###,###,###");
    String mesg = df.format(987654321);
    System.out.println(mesg);
    
    //$987,654,321

    숫자의 포맷을 λ³€κ²½ν•  수 μžˆλŠ” ν΄λž˜μŠ€μ΄λ‹€. μƒμ„±μžμ— μ›ν•˜λŠ” 좜λ ₯ν˜•μ‹μ„ μ§€μ •ν•˜κ³  formatλ©”μ†Œλ“œ μΈμžμ— 숫자λ₯Ό μ „λ‹¬ν•˜λ©΄ μ§€μ •ν•œ 포맷으둜 μˆ«μžκ°€ 좜λ ₯λœλ‹€.

    //2. String --> 숫자 : parse
    
    DecimalFormat df2 = new DecimalFormat("###,###");
    Number xxx = df2.parse("987,654");
    System.out.println(xxx);
    
    //987654

    포맷으둜 μž‘μ„±λœ 문자λ₯Όμ—΄μ„ 숫자둜 λ³€ν™˜ν•  땐 parse λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.

     

     

    NumberFormat

    NumberFormat nf = NumberFormat.getCurrencyInstance(); //ν˜„μž¬μ§€μ—­ν™”νμ–‘μ‹
    String mesg = nf.format(987654321);
    System.out.println(mesg);
    
    //β‚©987,654,321

    숫자λ₯Ό νŠΉμ • ν˜•μ‹μœΌλ‘œ λ°”κΏ€ 수 μžˆλŠ” ν΄λž˜μŠ€μ΄λ‹€. getCurrencyInstance()λ©”μ†Œλ“œλŠ” ν˜„μž¬ μ§€μ—­μ˜ 화폐 양식을 μ μš©ν•΄ format λ©”μ†Œλ“œ 인자둜 보낸 숫자λ₯Ό 좜λ ₯ν•œλ‹€.

     

     

    Enum (μ—΄κ±°ν˜• μƒμˆ˜)

    public class EnumTest1 {
    	public static void check(int num) {
    		//byte,short,int,char,String ,enum
    		switch(num) {
    		case 1: System.out.println("1");break;
    		case 2: System.out.println("2");break;
    		case 3: System.out.println("3");break;
    		default: System.out.println("default");break;
    		}
    	}
    	public static void main(String[] args) {
    		check(1);  //1.SELECT 2:INSERT 3;DELETE
    		check(2);
    		check(3);
    		check(99); //λ””ν΄νŠΈ 좜λ ₯됨
    	}//end main0
    }//end class
    public class EnumTest2 {
    
    	//μƒμˆ˜
    	public static final int SELECT= 1;
    	public static final int INSERT= 2;
    	public static final int DELETE= 3;
    	
    	public static void check(int num) {
    		//byte,short,int,char,String ,enum
    		switch(num) {
    		case SELECT: System.out.println("1");break;
    		case INSERT: System.out.println("2");break;
    		case DELETE: System.out.println("3");break;
    		default: System.out.println("default");break;
    		}
    	}
    	
    	public static void main(String[] args) {
    		// enumλŠ” μƒμˆ˜ λŒ€μ²΄ κ°€λŠ₯
    		check(SELECT);  //1.SELECT 2:INsERT 3;DELETE
    		check(INSERT);
    		check(DELETE);
    		check(99); //μ˜λ―Έμ—†λŠ” μˆ«μžμ§€μ •κ°€λŠ₯
    	}//end main0
    }//end class

    Enum은 데이터가 λͺ‡κ°œμ˜ ν•œμ •λœ μƒμˆ˜κ°’μœΌλ‘œ ꡬ성될 λ•Œ μ‚¬μš©ν•œλ‹€. public static final 둜 μ§€μ •ν•œ 일반 μƒμˆ˜λ₯Ό λŒ€μ²΄ν•  수 있으며 static이기 λ•Œλ¬Έμ— 객체생성 없이 μ‚¬μš©ν•  수 μžˆλ‹€.

    public class EnumTest3 {
    
    	//public enum νƒ€μž… {μƒμˆ˜κ°’,μƒμˆ˜κ°’2,μƒμˆ˜κ°’3}
    	public enum Color {BLUE,RED,YELLOW}
    	
    	public static void main(String[] args) {
    		 // νƒ€μž….μƒμˆ˜κ°’
    		Color c = Color.BLUE;
    		Color c2 = Color.RED;
    		Color c3 = Color.YELLOW;
    		//Color c4 = Color.GREEN;
    		System.out.println(c); //BLUE
    		
    		Color[] xx = Color.values();
    		for (Color p : xx) {
    			System.out.println(p);
    			System.out.println(p.name()+"\t"+p.ordinal());
    		}
    	}//end main0
    }//end class
    public class EnumTest4 {
    
    	//μƒμˆ˜
    	public enum DML { SELECT, INSERT, DELETE}
    	
    	public static void check(DML num) {
    		//byte,short,int,char,String ,enum
    		switch(num) {
    		case SELECT: System.out.println("SELECT");break;
    		case INSERT: System.out.println("INSERT");break;
    		case DELETE: System.out.println("DELETE");break;
    		default: System.out.println("default");break;
    		}
    	}
    	public static void main(String[] args) {
    		// enumλŠ” μƒμˆ˜ λŒ€μ²΄ κ°€λŠ₯
    		check(DML.SELECT);  //1.SELECT 2:INsERT 3;DELETE
    		check(DML.INSERT);
    		check(DML.DELETE);
    		//check(99); //μ˜λ―Έμ—†λŠ” μˆ«μžμ§€μ • λΆˆκ°€λŠ₯
    	}//end main0
    }//end class

     

    μ—΄κ±°ν˜• μƒμˆ˜ μ‚¬μš©ν•˜κΈ°

    public class Ex08_13 {
    	public enum Colors { RED, BLACK, YELLOW, BLUE };
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Colors color = Colors.BLACK;
    		System.out.println(color);
    		Colors[] c = Colors.values();
    		for(Colors x : c) {
    			System.out.println(x.name() + "\t"+x.ordinal());
    		}
    		//switchμ‚¬μš©
    		switch(color) {
    		case RED : System.out.println("RED"); break;
    		case BLACK : System.out.println("BLACK"); break;
    		case YELLOW : System.out.println("YELLOW"); break;
    		case BLUE : System.out.println("BLUE"); break;
    		default : break;
    		}
    	}
    }

     

     

    μ–΄λ…Έν…Œμ΄μ…˜

    @Override
    
    @Deprecated
    
    @SuppressWarning

    μ»΄νŒŒμΌλŸ¬κ°€ 검증할 λ•Œ ν•„μš”ν•œ 메타데이터λ₯Ό μ‰½κ²Œ ν™œμš©ν•  수 μžˆλ„λ‘ μ„€μ •ν•˜λŠ” 방법이닀. 

    @Override : ν•΄λ‹Ή λ©”μ†Œλ“œκ°€ λΆ€λͺ¨ 클래슀의 λ©”μ†Œλ“œ μ˜€λ²„λΌμ΄λ”© ν•œλ‹€λŠ” 정보λ₯Ό μ•Œλ €μ€€λ‹€.

    @Deprecated : μ˜€λž˜λ˜μ–΄μ„œ ν˜„μž¬ μ‚¬μš©ν•˜μ§€ μ•ŠμŒμ„ μ•Œλ €μ€€λ‹€.

    @SuppressWarning : μ»΄νŒŒμΌλŸ¬μ—κ²Œ κ²½κ³  정보λ₯Ό ν‘œμ‹œν•˜μ§€ μ•Šλ„λ‘ μ•Œλ €μ€€λ‹€.

     

     

    μŠ€μΊλ„ˆapi

    		Scanner scan = new Scanner(System.in);
    		while (true) {
    			System.out.println("이름을 μž…λ ₯ν•˜μ‹œμ˜€");
    			String name = scan.nextLine();
    			if(name.equals("q"))break;
    			System.out.println(name);
    		}
    		System.out.println("ν”„λ‘œκ·Έλž¨ μ’…λ£Œ");
    		scan.close();

    ν‚€λ³΄λ“œλ‘œλΆ€ν„° μž…λ ₯받은 값을 좜λ ₯ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•œλ‹€. Scanner κ°μ²΄μƒμ„±μ‹œ μƒμ„±μžμ— System.in을 인자둜 λ„£μ–΄μ„œ μž…λ ₯ν•œ 값을 λ°”μ΄νŠΈ λ‹¨μœ„λ‘œ μ½λŠ”λ‹€. nextInt()λ©”μ†Œλ“œλ‘œ μ •μˆ˜λ₯Ό μž…λ ₯λ°›λŠ”λ‹€. μ‚¬μš© ν›„ scan.close()둜 μŠ€μΊλ„ˆλ₯Ό λ‹«μ•„μ€€λ‹€.


     

    😜 NEXT : μžλ°” μž…μΆœλ ₯ κ³΅λΆ€ν•˜κΈ°

    λ°˜μ‘ν˜•

    λŒ“κΈ€

κ°œλ°œκ³΅λΆ€