Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
주석(Comments)주석(Comments)
▶이해를 돕기 위한 설명문
▶종류
// //
/* … */
/** … */
– 활용 예
javadoc HelloApplication.java
2
주석(Comments)주석(Comments)
/*/*File name: HelloApplication.java Created by: JungCreated by Jung Created on: March 2005
*/ /** documentation */public class HelloApplication { // to the end of line
public static void main(String args[]) {public static void main(String args[]) { System.out.println("Hello, Java Application!");
} }}
3
식별자(Identifier)식별자(Identifier)
▶클래스, 변수, 메소드 등의 이름
▶프로그래머가 정하는 이름▶프로그래머가 정하는 이름
규칙▶대소문자 구분
▶길이에 제한 없음▶길이에 제한 없음
▶영 대소문자, 한글, 숫자, ’_’, ’$’ 사용숫자로 시작할 수 없음
▶키워드, true, false, null은 불가▶키워 , true, false, null은 불가
4
식별자식별자
public class HelloApplication {String szMsg = "Hello, JavaString szMsg Hello, Java
Application!";public static void main(String args[])public static void main(String args[])
{int iTest = 0;int iTest 0;System.out.println(szMsg);
}} }
5
리터럴(Literal)리터럴(Literal)
a = 2; // int형 리터럴x = 0xA; // 16진수로 표현된 int형 리터럴o = 055; // 8진수로 표현된 int형 리터럴b = 5L; // long형 리터럴f 2 0f // fl t형 리터럴f = 2.0f; // float형 리터럴d = 2.0; // double형 리터럴e = 2 0E4; // double형 리터럴 2 0 * 104e 2.0E4; // double형 리터럴, 2.0 * 10t = true; // boolean형 리터럴c = 'd'; // char형 리터럴s = "Hello"; // Hello는 String 리터럴
7
메모리 영역메모리 영역
스택 영역▶메소드가 호출될 때 매개 변수 지역▶메소드가 호출될 때 매개 변수, 지역
변수, 반환 값을 저장하기 위한 메모리 공간리 공간
▶메소드의 실행이 끝나면 자동으로 사라짐
힙 영역힙 영역▶객체가 저장되는 메모리 공간
8
자료형(Data Type)자료형(Data Type)
▶변수의 저장 값, 메소드의 반환 값, 적용 가능한 연산을 결정적용 가능한 연산을 결정
기본(Primitive)형기본( )형▶기본형 변수에는 자료형의 값이 저장
됨됨
참조(Referenece)형참 형▶참조형 변수에는 주소가 저장됨
10
자료형(Data Type)자료형(Data Type)
기본(Primitive)형기본(Primitive)형
값변수변수
참조(Reference)형
참조값 객체나객체나 배열배열참조형참조형 변수변수
11
기본형기본형
분 입 의 위분류 타입 크기
(byte)
값의 범위
문자 h 2 ‘\ 0000’ ‘\ FFFF’문자 char 2 ‘\u0000’~‘\uFFFF’
논리 boolean 1 true 또는 false
b 1 128 127
정수
byte 1 -128~127
short 2 -32768~32767
4 231 231 1정수
int 4 -231~231-1
long 8 -263~263-1
실수float 4 (+/-)약1.4E-45~3.4E38
double 8 (+/-)약4.9E-324~1.8E308
12
자료형의 변환자료형의 변환
시적 형변환묵시적 형변환▶작은 타입에서 큰 타입으로▶작은 타입에서 큰 타입
int i = 5; double d = i;
int j = 10;System.out.println("j = " + j);
14
자료형의 변환자료형의 변환
오류가 나는 경우▶큰 타입에서 작은 타입으로
float f = 5 5; // error! (기본이 double)float f = 5.5; // error! (기본이 double)
명시적 형변환▶‘(자료형)’ 형태의 형변환 연산자 사용▶ (자료형) 형태의 형변환 연산자 사용
float f = (float )5.5; // ok!
연산자의 종류연산자의 종류
구분 연산자 예
산술연산자 + - * / % 단항+ 단항- ++ --
비교연산자 > >= < <= == != instanceof
논리연산자 && || ! 삼항?: & | ^논리연산자 && || 삼항 & |
비트연산자 & | ^ ~ << >> >>>
대입연산자 = += -= *= /= %= &= ^= |= >>=대입연산자 / | >> <<= >>>=
형변환연산자 (자료형)
기타 [ ] () .
21
블럭블럭
블럭블럭▶명령문들을 중괄호로 묶어 놓은 것public class BlockTest { // 블럭
public static void main(String args[]) { // 블럭int a b c;int a, b, c; { // 명령문 블럭;
int d, e, f; + b;a = a + b;
{ // 중첩된 블럭d++;
}} }
} }
24
public class IfElseTest {bli t ti id i (St i []) {public static void main(String args[]) {int n = Integer.parseInt(args[0]); if (n > 10) {if (n 10) {
System.out.println("10보다 크다"); } else if ( n < 3) {
System.out.println("3보다 작다"); } else {
S stem o t p intln("3보다 크거나 같고System.out.println("3보다 크거나 같고10보다 작거나 같다.");
} }}
}
26
public class SwitchTest {bli t ti id i (St i []) {public static void main(String args[]) {int n = Integer.parseInt(args[0]); switch(n) { case 10:
System.out.println("10입니다."); break;break;
case 20: case 30:
S t t i tl ("20이거나 30입니다 ")System.out.println("20이거나 30입니다."); break;
default: System.out.println("모르겠습니다."); break;
} }}
}
27
제어문(반복)제어문(반복)
▶조건이 true일 때 명령어를 반복실행실행
for 문for 문for (for (초기화식초기화식 ; boolean; boolean--수식수식 ; ; 반복식반복식) {) {
문장문장문장문장
}}
28
제어문(반복)제어문(반복)
hil 문while 문while (booleanwhile (boolean--수식수식) {) {
문장문장
}}
do-while 문▶조건 검사 전에 일단 명령문을 실행
do {do {do {do {
문장문장
} while (boolean} while (boolean--수식수식););} while (boolean} while (boolean 수식수식););
29
break 문break 문
▶반복문을 빠져나갈 때 사용
문을 포함하는 가장 가까운break 문을 포함하는 가장 가까운 switch, for, while 문의 실행을 끝냄
break;
30
break 문break 문
bli l B kT t2 {bli l B kT t2 {public class BreakTest2 {public class BreakTest2 {
public static void main(String public static void main(String argsargs[]) {[]) {
intint n;n;intint n; n;
{ {
intint n = n = Integer.parseIntInteger.parseInt((argsargs[0]); [0]);
if (n > 10) if (n > 10) break; ;
n = n * 10; n = n * 10;
}}} }
n = n * 20; n = n * 20;
System.out.printlnSystem.out.println(n);(n);System.out.printlnSystem.out.println(n); (n);
} }
}}
31
continue 문continue 문
▶f 문 ▶ hil 문▶for 문 ▶while 문
FF
초기화식초기화식초기화식초기화식
FF
TT
FF조건조건조건조건 조건조건
TT
FF
......continue;continue;
............
continue;continue;
TT
반복식반복식
continue;continue;......
33
배열배열
▶같은 자료형의 데이터들을 모아 놓은 것▶같은 자료형의 데이터들을 모아 놓은 것
▶자바에서 배열은 참조형
▶배열의 선언
선언만으로는 크기를 지정할 수 없다선언만으로는 크기를 지정할 수 없다
int[ ] a; int a [ ];
선언한 뒤 생성 과정을 거쳐 사용함
37
배열의 초기화배열의 초기화
배열의 초기화▶선언 후 초기 값을 지정하는 것▶선언 후 초기 값을 지정하는 것
▶선언과 동시에 초기값을 지정하면 자동으로 메모리 공간이 확보됨됨
int a [ ] = {1, 2, 3};
38
배열의 선언과 초기화배열의 선언과 초기화
public class ArrayTest2 {public class ArrayTest2 {
public static void main(String args[]) {public static void main(String args[]) {
/ 1/ 1차원차원 배열의배열의 초기화초기화 ///* 1/* 1차원차원 배열의배열의 초기화초기화 **/ /
int anArray1[ ] = {1, 2, 3, 4, 5}; int anArray1[ ] = {1, 2, 3, 4, 5};
int[ ] anArray2 = {1 2 3 4 5};int[ ] anArray2 = {1 2 3 4 5};int[ ] anArray2 {1, 2, 3, 4, 5}; int[ ] anArray2 {1, 2, 3, 4, 5};
/* 2/* 2차원차원 배열의배열의 초기화초기화 **/ /
int anArray3[ ][ ] = {{1, 2, 3}, {4, 5, 6}}; int anArray3[ ][ ] = {{1, 2, 3}, {4, 5, 6}};
int[ ][ ] anArray4 = {{1, 2, 3}, {4, 5, 6}}; int[ ][ ] anArray4 = {{1, 2, 3}, {4, 5, 6}};
int anArray5[ ];
A 5 {1 2 3 4 5}; //에러;anArray5 = {1, 2, 3, 4, 5}; //에러;
} }
}}}}}}
39
배열의 생성배열의 생성
배열의 생성▶사용할 메모리 공간을 확보
▶숫자의 경우 0으로 나머지는 null▶숫자의 경우 0으로, 나머지는 null로 초기화됨
int a [ ];
a = new int[3];
40
배열의 생성배열의 생성
{{public class ArrayTest3 {public class ArrayTest3 {
public static void main(String args[]) {public static void main(String args[]) {
/* 1/* 1차원차원 배열의배열의 생성생성 **///* 1/* 1차원차원 배열의배열의 생성생성 **/ /
int anArray1[ ] = new int[5]; int anArray1[ ] = new int[5];
int[] anArray2 = new int[5]; int[] anArray2 = new int[5];
int anArray3[ ]; int anArray3[ ];
anArray3 = new int[5]; anArray3 = new int[5];
/* 2/* 2차원차원 배열의배열의 생성생성 **/ /
int anArray4[ ][ ] = new int[3][2]; int anArray4[ ][ ] = new int[3][2];
int[ ][ ] anArray5 = new int[3][2];int[ ][ ] anArray5 = new int[3][2];int[ ][ ] anArray5 = new int[3][2]; int[ ][ ] anArray5 = new int[3][2];
} }
} }
41
배열의 크기배열의 크기anArray[0].lengthanArray[0].length
anArray.lengthanArray.length배열이름.length
public class ArrayTest5 { public class ArrayTest5 {
public static void main(String args[]) {public static void main(String args[]) {
i t[ ][ ] Ai t[ ][ ] AanArray[1].lengthanArray[1].length
int[ ][ ] anArray;int[ ][ ] anArray;
……
for(int i = 0;i <for(int i = 0;i < anArray length;i++);i++)for(int i 0;i < for(int i 0;i < anArray.length;i++) ;i++)
for(int j = 0;j < for(int j = 0;j < anArray[i].length;j++) ++)
anArray[i][j] = i; anArray[i][j] = i;
} }
}}
42
객체 배열객체 배열
▶배열의 원소가 객체인 배열
▶배열 생성 외에 원소도 생성해야 함▶배열 생성 외에 원소도 생성해야 함
Cat[ ] myCats = new Cat[5];Cat[ ] myCats = new Cat[5];
myCats[0] = new Cat("myCats[0] = new Cat("야옹이야옹이", 2);", 2);
……
Cat[ ] myCats = { new Cat ("Cat[ ] myCats = { new Cat ("야옹이야옹이", 2), ", 2), ……};};
43
문자열문자열
String 클래스String 클래스
▶참조형이지만 기본형처럼 다룰 수참 형이지만 기본형처럼 다룰 수있도록 함
String szStr1 = "Java Application";
String szStr2, szStr3;
szStr2 = "Java Applet";
szStr3 = szStr2;
String szStr1 = new String(“Java”);String szStr1 new String( Java );
44
문자열문자열
St i 클래스String 클래스
▶+ 연산자▶ 연산자
문자열의 연결
다른 기본형이나 참 형과의 연산 가능다른 기본형이나 참조형과의 연산 가능
▶System.out.print▶System.out.print
▶System.out.println
문자열로 변환하여 출력함
System out println( “i =” + i );System.out.println( i = + i );
45