n id user Name username user Name user

  • Slides: 71
Download presentation

자바 식별자 n 올바른 식별자의 예 id user. Name user_name _user. Name $user. Name

자바 식별자 n 올바른 식별자의 예 id user. Name user_name _user. Name $user. Name n 틀린 식별자의 예 user name 3 d_studio this #arg

키워드 n 자바의 키워드 abstract boolean break byte case catch char class const continue

키워드 n 자바의 키워드 abstract boolean break byte case catch char class const continue n default do double else extends finally float for goto if implements import instanceof interface long native new package private protected public return short static super switch synchronized this throws transient try void volatile while 주의 1) const와 goto는 예약어로 지정됐지만 현재 사용되지 않는다. 2) "true", "false", "null" 은 boolean/null 상수로 예약어처럼 사용가능 3) 문자가 자바 문자나 숫자인가를 알아보기 위해서는 Character. is. Java. Letter. Or. Digit() 메소드를 이용할 수 있다. 4) 유니코드에 관한 정보는 http: //www. unicode. org/에서 얻을 수 있다.

예제 : id. java 1 class id { 2 public static void main(String args[])

예제 : id. java 1 class id { 2 public static void main(String args[]) { 3 String 이름 = "홍길동"; 4 System. out. println("안녕하세요. " + 이름 +" 님!"); 5 } 6 } n 결과 % javac id. java % java id 안녕하세요. 홍길동 님!

예제 : Name. Test. java n 클래스, 메소드, 멤버필드의 이름 공간이 분리 n 클래스,

예제 : Name. Test. java n 클래스, 메소드, 멤버필드의 이름 공간이 분리 n 클래스, 메소드, 멤버필드의 이름이 동일해도 됨 1 class Same. Name. Point x, y; 2 3 class Name. Test { 4 5 static Same. Name. Point(int x, int y) { 6 Same. Name. Point p = new Same. Name. Point(); . . . 11 public static void main(String[] args) { 12 int Same. Name. Point; 13 Same. Name. Point[] pa = new Same. Name. Point[2]; 14 for (Same. Name. Point = 0; Same. Name. Point++) { 15 pa[Same. Name. Point] = new Same. Name. Point(); . . . < 2;

예제 : Break. Label. java n break/continue에서 사용되는 레이블 n n 변수와 다른 이름

예제 : Break. Label. java n break/continue에서 사용되는 레이블 n n 변수와 다른 이름 공간을 갖는다. 라벨과 변수의 이름이 동일해도 무방 1 class Break. Label { 2 public static void main(String args[ ]) { 3 i: 4 for (int i = 0; i <= 100; i++) { 5 int j = 0; 6 while (j < 10) { 7 j++; …………. . .

int 타입 n n 32비트 정수 타입으로 -231 ~ 231 -1 사이의 값을 가진다.

int 타입 n n 32비트 정수 타입으로 -231 ~ 231 -1 사이의 값을 가진다. 예제 : Int. java 1 class Int { 2 3 public static void main(String ard[]) { 4 int 1 = 5, int 2 = 28; 5 int 3, int 4, int 5 ; 6 7 int 3 = int 2 * int 1; 8 int 4 = int 2 / int 1; 9 int 5 = 25 / int 1; 10 11 System. out. println("28 * 5 = "+ int 3); 12 System. out. println("28 / 5 = "+ int 4); 13 System. out. println("25 / 5 = "+ int 5); 14 } 15 }

byte, short 타입 n byte와 short도 int와 마찬가지로 정수 타입이다. byte는 -27 ~ 27

byte, short 타입 n byte와 short도 int와 마찬가지로 정수 타입이다. byte는 -27 ~ 27 -1 사이의 값 short은 -215 ~ 215 -1 사이의 값 n 예제 : Byte. java n n 1 class Byte { 2 public static void main(String args[]) { 3 byte 1; 4 5 byte 1 = 256; 6 System. out. println("byte 1 = "+byte 1); 7 8} }

예제 : Floating. One. java 1 class Floating. One { 2 public static void

예제 : Floating. One. java 1 class Floating. One { 2 public static void main(String args[]) { 3 float 1; 4 5 float 1 = 3. 14; 6 } 7 } n 결과 % javac Floating. One. java: 5: Incompatible type for =. Explicit cast needed to convert double to float 1 = 3. 14; ^ 1 error

예제 : Floating. Four. java 1 class Floating. Four { 2 public static void

예제 : Floating. Four. java 1 class Floating. Four { 2 public static void main(String args[]) { 3 int 1; 4 float 1 = 28. 0 f, float 2 = 5; 5 6 int 1 = (int)(float 1 / float 2); 7 System. out. println("(int)(28. 0 / 5. 0) = "+int 1); …………. . n 결과 % java Floating. Four (int)(28. 0 / 5. 0) = 5

char 타입 n char 타입은 16 비트의 부호가 없는 유니코드로 정의 n 예제: Char.

char 타입 n char 타입은 16 비트의 부호가 없는 유니코드로 정의 n 예제: Char. One. java 1 class Char. One { 2 public static void main(String args[]) { 3 char c = 'a'; 4 int 1 ; 5 6 int 1 = c; 7 System. out. println("int 1 = "+ int 1); 8 9 int 1 = int 1 + 1; 10 System. out. println("int 1 = "+ int 1); 11 System. out. println("(char)"+ int 1 +" = "+ (char)int 1); ……… n 예제: Char. Two. java 1 class Char. Two { 2 public static void main(String args[]) { 3 char han = 'ud 55 c'; 4 char gul = 'uae 00’; ……. .

참조 타입 n C++과 자바에서 객체를 생성하는 방법 1 2 3 4 int a

참조 타입 n C++과 자바에서 객체를 생성하는 방법 1 2 3 4 int a = 100 ; Car mycar, yourcar; mycar = new Car(); yourcar = mycar;

예제: Equal. java 1 public class Equal { 2 public static void main(String args[])

예제: Equal. java 1 public class Equal { 2 public static void main(String args[]) { 3 String a = new String("abc"); 4 String b = "abc"; 5 String c = b; 6 String d = a; 7 8 String. Buffer e = new String. Buffer("abc"); 9 String. Buffer g = e; 10 11 if(a = = b) { 12 System. out. println("a= =b"); 13 } 14 // if(a = = e) { // 컴파일 에러 15 // System. out. println("a= =e"); 16 // } …………. . .

예제: Equal. java 20 if(b. equals(a)) { 21 System. out. println("b. equals(a)"); 22 }

예제: Equal. java 20 if(b. equals(a)) { 21 System. out. println("b. equals(a)"); 22 } 23 if(b. equals(e)) { 24 System. out. println("b. equals(e)"); ……………. . . n 결과 % java Equal c==b b. equals(a)

예제: Gauss. java 1 class Gauss { 2 public static void main(String[] args) {

예제: Gauss. java 1 class Gauss { 2 public static void main(String[] args) { 3 int[] ia = new int[101]; 4 for (int i = 0; i < ia. length; i++) 5 ia[i] = i; 6 7 int sum = 0; 8 for (int i = 0; i < ia. length; i++) 9 sum += ia[i]; 10 ………. . .

예제: Multi. Darray. java n 1 class Multi. Darray { 2 public static void

예제: Multi. Darray. java n 1 class Multi. Darray { 2 public static void main(String args[]) { 3 char stars[][]; 4 5 stars = new char[6][]; 6 for(int i=0; i < stars. length; i++) { 7 stars[i] = new char[i+1]; 8 for(int j=0; j < stars[i]. length; j++) { 9 stars[i][j] = '*'; 10 } ………….

if 문 n 자바의 제어 구조는 C나 C++와 거의 동일 n 형태 1: if

if 문 n 자바의 제어 구조는 C나 C++와 거의 동일 n 형태 1: if (boolean) { statements; } else { statements; } n 형태 2: if (boolean) { statements; } else if(boolean) { statements; } else { statements; }

예제: Hello. Somebody. java 1 class Hello. Somebody { 2 3 public static void

예제: Hello. Somebody. java 1 class Hello. Somebody { 2 3 public static void main (String args[]) { 4 5 System. out. print("Hello "); 6 if (args. length > 0) { 7 System. out. println(args[0]); 8 } 9 else { 10 System. out. println("? ? ? "); 11 } …………. n 결과 % javac Hello. Somebody. java % java Hello. Somebody CHOI Hello CHOI % java Hello. Somebody Hello ? ? ? --> 실행 결과

예제 args[3] java Hello. Somebody CHOI KIM T args[0] args[1]

예제 args[3] java Hello. Somebody CHOI KIM T args[0] args[1]

for 문 n 형태: for(init_expr; test_expr 2; increment_expr 3) { statements; } n 예제:

for 문 n 형태: for(init_expr; test_expr 2; increment_expr 3) { statements; } n 예제: Hello. Friends. java 1 class Hello. Friends { 3 public static void main (String args[]) { 5 int i; 7 System. out. print("Hello "); 8 for (i=0; i < args. length; i++) { 9 System. out. print(args[i]); 10 System. out. print(" "); 11 } 12 System. out. println(); ………. n 결과 % javac Hello. Friends. java % java Hello. Friends CHOI LEE KIM SONG PARK YANG Hello CHOI LEE KIM SONG PARK YANG --> 실행 결과

while 문 n 형태: while(boolean) { statements; } n 예제: Hello. Friends. While. java

while 문 n 형태: while(boolean) { statements; } n 예제: Hello. Friends. While. java n 결과 1 class Hello. Friends. While { 3 public static void main (String args[]) { 5 int i = 0; 7 System. out. print("Hello "); 8 while(i < args. length) { 9 System. out. print(args[i]); 10 System. out. print(" "); 11 i++; 12 } …………. . % javac Hello. Friends. While. java % java Hello. Friends. While CHOI LEE KIM SONG PARK YANG Hello CHOI LEE KIM SONG PARK YANG --> 실행 결과

do-while 문 n do-while 문은 while문과 비슷하지만 최소 한번은 수 행됨 n 형태: do

do-while 문 n do-while 문은 while문과 비슷하지만 최소 한번은 수 행됨 n 형태: do { statements; } while(boolean);

예제: Hello. Friends. Do. While. java n 1 class Hello. Friends. Do. While {

예제: Hello. Friends. Do. While. java n 1 class Hello. Friends. Do. While { 3 public static void main (String args[]) { 5 int i = 0; 7 System. out. print("Hello "); 8 do { 9 System. out. print(args[i]); 10 System. out. print(" "); 11 i++; 12 } while(i < args. length); …………. n 결과 % javac Hello. Friends. Do. While. java % java Hello. Friends. Do. While CHOI LEE KIM SONG PARK YANG Hello CHOI LEE KIM SONG PARK YANG --> 실행 결과

switch 문 n 스위치 문의 expr 부분에는 long을 제외한 정수형 타입 n n n

switch 문 n 스위치 문의 expr 부분에는 long을 제외한 정수형 타입 n n n byte, short, char, int 문자열이나 실수형 및 long 등은 expr 부분에 들어갈 수 없다. 형태: switch(expr) { case expr 1: statements; break; case expr 2: statements; break; default: statements; break; }

예제: Yes. Or. No. java n 1 import java. io. *; 3 class Yes.

예제: Yes. Or. No. java n 1 import java. io. *; 3 class Yes. Or. No { 4 public static void main(String args[]) { 6 System. out. println("Yes/No ? "); 7 try { 8 char c = (char)System. in. read(); 9 10 switch(c) { 11 case 'y': 12 case 'Y': 13 System. out. println("Yes"); 14 break; 15 case 'n': 16 case 'N': 17 System. out. println("No"); 18 break; 19 default: 20 System. out. println("Wrong answer"); 21 break; 22 } 23 }catch(IOException e) { } ………. .

break 문 n n for, while, do, switch 문에서 빠져나갈 때 사용 예제: Break.

break 문 n n for, while, do, switch 문에서 빠져나갈 때 사용 예제: Break. java 1 import java. io. IOException; 3 class Break { 4 public static void main(String args[]) { 6 System. out. println("Enter 'q' to exit. "); 7 quit: 8 for(int i=0; i < 10; i++) { 9 for(int j=0; j < 10; j++) { 10 if(j%7 == 4) { 11 try { 12 int b = System. in. read(); 13 if(b == 'q') 14 break quit; 15 }catch(IOException ioe) {} 16 } else if(j%7 == 6) 17 break; 18 else 19 System. out. print("(" + i +", "+j+")"); ……. . .

continue 문 n while, do, for 문에서 continue문 n n 다음은 수행하지 않고 반복문의

continue 문 n while, do, for 문에서 continue문 n n 다음은 수행하지 않고 반복문의 조건 부분을 실행한다. 예제: Continue. java 1 class Continue { 2 public static void main(String args[]) { 3 int sum =0; 4 System. out. println("continue 테스트. "); 5 6 quit: 7 for(int i=0; i < 10; i++) { 8 for(int j=i; j < 2*i; j++) { 9 System. out. print("("+i+", "+j+", "+sum+")"); 10 if(j%3 == 0) 11 continue; 12 if(sum > i*10) 13 continue quit; 14 else 15 sum += j; ……. . .

2. 5 예외 처리 (Exception Handling)

2. 5 예외 처리 (Exception Handling)

예외 클래스 계층 구조 Object Throwable Exception Class. Not. Found. Exception Error Runtime. Exception

예외 클래스 계층 구조 Object Throwable Exception Class. Not. Found. Exception Error Runtime. Exception Illegal. Acess. Exception Instantiation. Exception Arithmetic. Exception Interrupted. Exception Nagative. Array. Size. Exception No. Such. Method. Exception Array. Index. Out. Of. Bounds. Exception … Security. Exception. . .

예외 클래스: 예제 Divide. Zero. Exception. java 1 public class Divide. Zero. Exception extends

예외 클래스: 예제 Divide. Zero. Exception. java 1 public class Divide. Zero. Exception extends Exception { 2 private String reason; 3 4 Divide. Zero. Exception() { 5 reason = "Divide by zero"; 6 } 7 8 public String to. String() { 9 return reason; 10 } 11 }

예외 발생: 예제 Divider. java 1 public class Divider { 2 3 public Divider()

예외 발생: 예제 Divider. java 1 public class Divider { 2 3 public Divider() { } 6 7 public int divide(int x) throws Divide. Zero. Exception { 8 switch(x) { 9 case 0: 10 throw new Divide. Zero. Exception(); 11 case 1: 12 throw new Divide. Zero. Exception(); 13 defaults: 14 ; 15 } 16 return (int)(100/(x*(x-1))); 17 } 18 }

예외 처리: 예제 Test. Exception. java 1 public class Test. Exception { 2 3

예외 처리: 예제 Test. Exception. java 1 public class Test. Exception { 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public Test. Exception() { } public static void main(String[] args) { int a; Divider t = new Divider(); try { a = Integer. parse. Int(args[0]); System. out. println("successful!: "+ t. divide(a)); } catch(Divide. Zero. Exception e) { System. out. println("error!!: " + e); } catch(Exception ex) { System. out. println("error!!: " + ex); }. . .

예외 전파: 예제 public class Propagation_Demo { static public void main (String[] args) {

예외 전파: 예제 public class Propagation_Demo { static public void main (String[] args) { Exception_Scope demo = new Exception_Scope(); System. out. println("program beginning"); demo. level 1(); System. out. println("program ending"); } // method main } // class Propagation_Demo

class Exception_Scope { public void level 3 (int adjustment) { int current = 1;

class Exception_Scope { public void level 3 (int adjustment) { int current = 1; System. out. println("level 3 beginning"); current = current / adjustment; System. out. println("level 3 ending"); } // method level 3 public void level 2() { System. out. println("level 2 beginning"); level 3 (0); System. out. println("level 2 ending"); } // method level 2 public void level 1() { System. out. println("level 1 beginning"); try { level 2(); } catch (Arithmetic. Exception problem) { System. out. println (problem. get. Message()); problem. print. Stack. Trace(); } System. out. println("level 1 ending"); } // method level 1 } // class Exception_Scope