728x90
수업내용
1교시 (09:30-10:20)
- 어제 배운 함수와 변수 복습
데이터타입 선언 변수명 선언 ; __________ _______ _ 데이터타입 선언 변수명 선언 = 변수의 값 ; __________ _______ _ ______ _
접근제한자 : public, protected, default, private함수 ——— 함수 선언하기 [접근제한자] [수정자] 리턴형 함수이름(parameter) { // 지역변수 데이터타입 변수명 = 값; [return] 변수명; }
수정자 : static, final
리턴형 : void, 기초자료형, 참조자료형, 사용자정의클래스, etc.
함수이름 : 영문자 소문자로 시작, 한글 가능(자바는 유니코드를 지원한다. 단, 사용금지)
() 소괄호 : parameter(매개변수) tjsdjsgksms rhdrks parameter : 기초자료형, 참조자료형, 사용자정의클래스 등이 올 수 있다.
{} 블록 : 함수 블록 안에서 인터프리터 방식으로 수행된다.
return : return 값의 데이터타입은 리턴형 데이터타입과 동일해야 한다.
return 값 : 표현식이나 값이 와도 무방하다.함수 호출하기
리턴형 void
void 함수이름(파라미터);
함수이름(아규먼트);
리턴형 데이터타입
데이터타입 함수이름(파라미터);
데이터타입 변수명 = 함수이름(아규먼트); - 파라미터의 데이터타입과 아규먼트의 데이터타입은 동일해야 한다. 파라미터의 변수명과 데이터타입의 변수명은 동일하지 않아도 된다.
함수 리턴형 함수 파라미터(매개변수), 아규먼트 - 변수 : 데이터를 담는 상자
변수 : 데이터를 담는 상자
변수
멤버변수 : Fields
클래스 블록 안과 함수 블록 외부 사이에 선언하는 변수
상수 : Constant variable
public static final 데이터타입 변수명 = 값;
클래스 변수 : Class variable, Static variable
[public] static 데이터타입 변수명 = 값;
[public] static 데이터타입 변수명;
전역변수 : Global variable
public 데이터타입 변수명;
멤버변수 : Member variable
데이터타입 변수명;
지역변수 : Local variable
함수 블록 안에서 선언하는 변수
1. 함수 블록에서 태어나서 함수 블록에서 죽는다.
2. 함수 블록 밖으로는 나갈 수 없다.
3. 지역 변수를 사용할 때는 항시 초기화되어 있어야 한다.
데이터타입 변수명 = 값;
데이터타입 변수명;
변수명 = 값;
2교시 (10:30-11:20)
- [교재 126p] 클래스와 객체
- 객체란
- 현실 세계의 유무형의 사물
package a.b.c.ch1;
public class 나 {
public static void main(String args[]) {
System.out.println("나.main() 함수 시작 >>> : ");
// 운동하는 나 : 등산, 헬스장
// 스터디하는 나 : 머신러닝, 메타버스
// 국비지원교육 훈련생 나 : 빅데이터 개발자 과정을 수강
// 친구 만나는 나 : 전시회 관람
나 운동 = new 나();
System.out.println("운동 >>> : " + 운동);
나 스터디 = new 나();
System.out.println("스터디 >>> : " + 스터디);
나 훈련생 = new 나();
System.out.println("훈련생 >>> : " + 훈련생);
나 친구 = new 나();
System.out.println("친구 >>> : " + 친구);
} // end of main()
} // end of 나 class
3교시 (11:30-12:20)
- 자바에서 자바 프로그램을 메모리에 올리는 방법
- static 키워드 클래스, 변수, 함수의 이름 앞에 static 키워드가 붙어 있으면 자바 버추얼 머신이 그 대상을 메모리에 올려준다. 단, new 연산자와는 전혀 다른 것이다.
- new 키워드 또는 연산자 자바 버추얼 머신이 new 키워드를 보면 new 뒤에 있는 클래스이름을 보고 해당하는 클래스를 메모리에 올린다.
- new 키워드(연산자) 사용방법
- 클래스 이름을 선언한다.
- 참조 변수를 선언한다.
- = 대입 연산자를 선언한다.
- new 키워드(연산자)를 선언한다.
- 생성자를 선언한다. 클래스 이름에 () 소괄호를 붙여서 만든다. 클래스이름 변수(참조변수, reference variable) = new 클래스이름();
package a.b.c.ch1; public class Exam_ClassTest { // 멤버 변수 int iVal; // 스태틱변수, 클래스변수 // static 키워드가 붙은 변수는 실행할 때 JVM이 해당 변수의 값을 초기화해준다. static int iValStatic; // void : 리턴 값이 없다 : return 키워드를 사용할 수 없다. public static void main(String args[]) { System.out.println("main() 함수는 콘솔 어플리케이션의 시작점이다."); System.out.println("함수 블록 안에서는 인터프리터 방식으로 수행된다."); // 함수 블록 내부에서 선언한 지역변수 // 지역변수를 사용하기 전에 무조건 초기화가 되어 있어야 한다. int i = 10; System.out.println("i >>> " + i); // static 변수 호출하기 System.out.println("iValStatic >>> : " + iValStatic); // System.out.println("iVal >>> : " + iVal); /* C:\\00.KOSMO108\\10.JExam\\ch1>javac -d . Exam_ClassTest.java Exam_ClassTest.java:17: error: non-static variable iVal cannot be referenced from a static context System.out.println("iVal >>> : " + iVal); ^ 1 error */ // static 키워드가 붙지 않은 iVal 멤버변수 사용법 // iVal 멤버변수를 메모리에 올려야 한다. Exam_ClassTest ec = new Exam_ClassTest(); System.out.println("Exam_ClassTest 사용자정의클래스의 변수(참조변수) ec >>> : \\n" + ec); System.out.println("ec.iVal >>> : " + ec.iVal); Exam_ClassTest ec_1 = new Exam_ClassTest(); System.out.println("Exam_ClassTest 사용자정의클래스의 변수(참조변수) ec_1 >>> : \\n" + ec_1); System.out.println("ec_1.iVal >>> : " + ec_1.iVal); // 스태틱 변수는 참조변수로 참조해서 사용하면 안 된다. // System.out.println(ec_1.iValStatic); } // end of main() } // end of Exam_ClassTest class
4교시 (12:30-13:20)
- [교재 143p, 147p] 클래스와 인스턴스
package a.b.c.ch1;
public class Exam_ClassTest_1 {
// 멤버변수
byte bVal;
char cVal;
short sVal;
int iVal;
long lVal;
float fVal;
double dVal;
boolean boolVal;
String strVal;
public static void main(String args[]) {
/*
1. Exam_ClassTest_1 클래스를 사용하려면 맨 먼저 Exam_ClassTest_1을 인스턴스 instance 해야한다.
2. instance 인스턴스 : 클래스를 메모리에 올리는 행위 : 가장 대표적인 연산자 new 키워드 연산자
3. new 키워드 (연산자)로 인스턴스 하는 방법 (메모리에 올리는 방법)
3.1 사용하려고 하는 클래스(빌트인클래스 rt.jar에 있는 클래스, 사용자정의클래스)를 클래스 이름으로 선언한다.
3.2 참조변수 reference variable을 선언한다.
이 참조변수 메모리에 올라간 클래스를 참조해서
클래스 블록에 선언한 변수, 함수를 참조변수 .(도트 연산자)를 이용해서 사용할 수 있다.
3.3 = 대입 연산자 선언
3.4 클래스 이름 뒤 () 소괄호를 붙여서 생성자를 만든다.
====================================================
생성자가 메모리에 올라가면(인스턴스 되면)
해당 클래스에 있는 멤버변수를 디폴트 값으로 초기화한다.
====================================================
클래스이름 참조변수 = new 클래스이름();
클래스이름() <---- 생성자라고 한다.
이 생성자가 해당 클래스 내부에 선언한 멤버변수를 디폴트 값으로 초기화한다.
*/
Exam_ClassTest_1 ec = new Exam_ClassTest_1();
System.out.println("ec 참조변수 주소값 >>> : " + ec);
System.out.println("ec.bVal >>> : " + ec.bVal);
System.out.println("ec.cVal >>> : " + ec.cVal);
System.out.println("ec.sVal >>> : " + ec.sVal);
System.out.println("ec.iVal >>> : " + ec.iVal);
System.out.println("ec.lVal >>> : " + ec.lVal);
System.out.println("ec.fVal >>> : " + ec.fVal);
System.out.println("ec.dVal >>> : " + ec.dVal);
System.out.println("ec.boolVal >>> : " + ec.boolVal);
System.out.println("ec.strVal >>> : " + ec.strVal);
} // end of main()
} // end of Exam_ClassTest_1 class
5교시 (14:30-15:20)
- 스태틱변수인지, 함수가 리턴형인지에 따라 호출 방법이 다름
package a.b.c.ch1;
public class Exam_ClassTest_2 {
// 멤버변수
// 상수
public static final int ID_NUM = 1;
// 스태틱 변수, 클래스 변수
static int id_val = 11;
// 전역 변수
public int iVal_G;
// 멤버 변수
int iVal;
// 리턴형이 없는 함수, static 없는 함수
public void aMethod() {
System.out.println("aMethod()");
}
// 리턴형이 없는 함수, static 있는 함수
public static void aaMethod() {
System.out.println("aaMethod()");
}
// 리턴형 int, static 없는 함수
public int bMethod() {
System.out.println("bMethod()");
return 1 + 1;
}
// 리턴형 int, static 있는 함수
public static int bbMethod() {
System.out.println("bbMethod()");
return 10 + 10;
}
public static void main(String args[]) {
// static 있는 자원(변수, 함수) 호출하는 방법
// 패키지명.클래스이름.자원이름
System.out.println("Exam_ClassTest_2.ID_NUM >>> : " + Exam_ClassTest_2.ID_NUM + "\n");
System.out.println("Exam_ClassTest_2.id_val >>> : " + Exam_ClassTest_2.id_val + "\n");
/*
println() 함수에 void 리턴형(리턴값이 없는 함수)는 사용할 수 없다.
System.out.println("Exam_ClassTest_2.aaMethod() >>> : " + Exam_ClassTest_2.aaMethod());
Exam_ClassTest_2.java:44: error: 'void' type not allowed here
System.out.println("Exam_ClassTest_2.aaMethod() >>> : " + Exam_ClassTest_2.aaMethod());
^
1 error
*/
// void 리턴형 함수(리턴값이 없는 함수)는 하기와 같이 함수를 호출해서 사용한다.
Exam_ClassTest_2.aaMethod();
// 리턴형이 있는 함수는 println() 함수에서 사용이 가능하다.
System.out.println("\n Exam_ClassTest_2.bbMethod() >>> : " + Exam_ClassTest_2.bbMethod() + "\n");
int iBB = Exam_ClassTest_2.bbMethod();
System.out.println("\n iBB >>> : " + iBB + "\n");
Exam_ClassTest_2 ec2 = new Exam_ClassTest_2();
System.out.println("ec2 참조변수 주소값 >>> : " + ec2 + "\n");
System.out.println("ec2.iVal_G >>> : " + ec2.iVal_G + "\n");
System.out.println("ec2.iVal >>> : " + ec2.iVal + "\n");
ec2.aMethod();
// 리턴형이 있는 함수는 println() 함수에서 사용이 가능하다.
System.out.println("\n ec2.bMethod() >>> " + ec2.bMethod());
// 리턴형이 있는 함수는 리턴값을 받아서 사용해도 된다.
int iB = ec2.bMethod();
System.out.println("\n iB >>> : " + iB + "\n");
} // end of main()
} // end of Exam_ClassTest_2 class
6교시 (15:30-16:20)
- Exam_ClassTest_2 코드에 주석 달기
- [교재 151p] 하단 표 - 용어가 비슷하면 대충 넘어가기. 너무 파고들면 오히려 헷깔림
7교시 (16:30-17:20)
- 인터프리터 방식
package a.b.c.ch1;
// System.out.println() 문장으로 log 를 찍는다.
// 메인부터 한 줄 한 줄 순서대로 실행됨
public class Exam_ClassTest_3 {
public int aMethod() {
System.out.println(" Exam_ClassTest_3.aMethod() 시작 >>> : \n");
int x = 1;
int y = 2;
System.out.println(" Exam_ClassTest_3.aMethod() >>> : " + x + " : " + y);
int addSum = x + y;
System.out.println(" Exam_ClassTest_3.aMethod() >>> : " + x + " + " + y + " = " + addSum + "\n");
System.out.println(" Exam_ClassTest_3.aMethod() 끝 >>> : \n");
return addSum;
// return 키워드 statement 를 수행하고 ; 종결 문자를 한 뒤에는
// 어떤 구문도 허락하지 않는다.
/*
System.out.println("Exam_ClassTest_3.aMethod() 끝 >>> : ");
Exam_ClassTest_3.java:10: error: unreachable statement
System.out.println("Exam_ClassTest_3.aMethod() 끝 >>> : ");
^
Exam_ClassTest_3.java:11: error: missing return statement
}
^
2 errors
*/
}
public static void main(String args[]) {
System.out.println("Exam_ClassTest_3.main() 시작 >>> : \n");
Exam_ClassTest_3 ec3 = new Exam_ClassTest_3();
System.out.println("ec3 >>> : " + ec3 + "\n");
System.out.println("ec3.aMethod() >>> : " + ec3.aMethod());
System.out.println("Exam_ClassTest_3.main() 끝 >>> : ");
} // end of main()
} // end of Exam_ClassTest
/*
Exam_ClassTest_3.main() 시작 >>> :
ec3 >>> : a.b.c.ch1.Exam_ClassTest_3@15db9742
Exam_ClassTest_3.aMethod() 시작 >>> :
Exam_ClassTest_3.aMethod() >>> : 1 : 2
Exam_ClassTest_3.aMethod() >>> : 1 + 2 = 3
Exam_ClassTest_3.aMethod() 끝 >>> :
ec3.aMethod() >>> : 3
Exam_ClassTest_3.main() 끝 >>> :
*/
8교시 (17:30-18:30)
- [교재 53p] 문자자료형
- char 자료형 공부
- API 문서 확인하면서 코드 만드는 방법
package a.b.c.ch1;
public class Exam_Var_4 {
void charMethod() {
System.out.println("Exam_Var_5.charMethod() 시작 >>> : \n");
char c1 = 'A';
System.out.println("c1 >>> : " + c1);
System.out.println(" (int)c1>>> : " + (int)c1);
int c2 = (int)c1;
System.out.println("c2 >>> : " + c2);
// static String toBinaryString(int i)
// Returns a string representation of the integer argument as an unsigned integer in base 2.
// String s_1 = java.lang.Integer.toBinaryString(int i);
String s_1 = Integer.toBinaryString(c2);
System.out.println("s_1 >>> : " + s_1);
// static String toHexString(int i)
// Returns a string representation of the integer argument as an unsigned integer in base 16.
String s_2 = Integer.toHexString(c2);
System.out.println("s_2 >>> : " + s_2);
// static String toOctalString(int i)
// Returns a string representation of the integer argument as an unsigned integer in base 8.
String s_3 = Integer.toOctalString(c2);
System.out.println("s_3 >>> : " + s_3);
System.out.println("Exam_Var_4.charMethod() 끝 >>> : ");
}
public static void main(String args[]) {
System.out.println("Exam_Var_4.main() 시작 >>> : \n");
System.out.println("main() 함수는 콘솔 어플리케이션을 시작시키고 >>> : \n");
System.out.println("함수를 호출하는 역할만 한다. >>> : \n");
Exam_Var_4 ev4 = new Exam_Var_4();
System.out.println("ev4 참조변수 주소값 >>> : " + ev4);
ev4.charMethod();
System.out.println("Exam_Var_4.main() 끝 >>> : ");
} // end of main()
} // end of Exam_Var_4 class
Notes
728x90
'국비지원교육 (22.01-22.07) > 강의노트' 카테고리의 다른 글
22-02-08(화) 009일차 [Java] 배열, 논리 연산(Boolean), 제어 흐름(If문) (0) | 2022.04.10 |
---|---|
22-02-07(월) 008일차 [Java] char 자료형, 1차원 배열, 형변환, String클래스 (0) | 2022.04.10 |
22-02-03(목) 006일차 [Java] 메소드(함수)의 자료형, 매개변수와 인수, 변수의 유효범위 (0) | 2022.04.10 |
22-01-28(금) 005일차 [Java] 변수와 자료형, static 키워드 (0) | 2022.04.09 |
22-01-27(목) 004일차 [Java] package, class, 변수 (0) | 2022.04.09 |