반응형
- C11클래스
- C1101클래스
- C1101개념01_클래스
package C1101클래스;
//클래스를 정의
//class Member => 주석이다. 어떠한 기능이 없다.
//내가 만든 자료형 모음
//주로 테이블과 똑같이 맞춘다.
class Member {
int number;
String id;
String pw;
String name;
int score;
}
// 다음 예제에서 진행 예정
// 주석과 같다. 기능이 없음
class Test01 {
// 변수
int x;
int y;
// 메서드
}
public class C1101개념01_클래스 {
public static void main(String[] args) {
//[사용법]
//1. 사용할때는 new
//2. .(점)을 찍어서. 내부 변수에 값을 넣는다.
// 클래스 사용 후
Member member = new Member();
member.number = 1;
member.id = "qwer";
member.pw = "1234";
member.name = "김철수";
member.score = 30;
}
}
- 자바는 특이하게. 프로젝트가. 하나의 프로그램이다.
- C1101개념02_클래스
package C1101클래스;
/*
class Test01 {
}
*/
/*
같은 패키지 안의 클래스는
다른 클래스에서 사용할 수 있다.
같은 패키지 내에서는
클래스를 중복해서 사용할 수 없다.
*/
class Test02 {
String name;
int score;
}
public class C1101개념02_클래스 {
public static void main(String[] args) {
Test01 t1 = new Test01();
t1.x = 10;
t1.y = 20;
System.out.println(t1.x);
System.out.println(t1.y);
Test02 t2 = new Test02();
t2.name = "홍길동";
t2.score = 87;
System.out.println(t2.name);
System.out.println(t2.score);
}
}
- C1101개념03_클래스
package C1101클래스;
class Test03 {
// int[] arr = new int[5];
// arr[0] = 87;
// ...
int[] arr = {87, 100, 11, 72, 92};
//클래스 안에 배열도 가능하다.
//변수가 6개. 이게 핵심
}
public class C1101개념03_클래스 {
public static void main(String[] args) {
Test03 t3 = new Test03();
// [문제1] 전체 합 출력
// [정답1] 362
int total = 0;
for(int i=0; i<t3.arr.length; i++) {
total += t3.arr[i];
}
System.out.println(total);
// [문제2] 4의 배수의 합 출력
// [정답2] 264
total = 0;
for(int i=0; i<t3.arr.length; i++) {
if(t3.arr[i] % 4 == 0) {
total += t3.arr[i];
}
}
System.out.println(total);
// [문제3] 4의 배수의 개수 출력
// [정답3] 3
int count = 0;
for(int i=0; i<t3.arr.length; i++) {
if(t3.arr[i] % 4 == 0) {
count += 1;
}
}
System.out.println(count);
// [문제4] 짝수의 개수 출력
// [정답4] 3
count = 0;
for(int i=0; i<t3.arr.length; i++) {
if(t3.arr[i] % 2 == 0) {
count += 1;
}
}
System.out.println(count);
}
}
- 메모리
- int[] arr = {87, 100, 11, 72, 92};
- C1101개념04_클래스
package C1101클래스;
/*
# 클래스의 용도
1. 배열만 사용해서 프로그램을 만들때의 불편함 해소
2. 즉, 배열은 같은 종류의 자료형만 저장이 가능하다.
3. 때문에 정수의 경우 계속해서 형변환을 사용해야 한다.
4. 데이터 검색 시 데이터의 양이 많아지면 인덱스로 찾기가 불편하다.
*/
//클래스의 용도는 테이블을 저장하기 위함이다.
class Employee {
//Employee를 배열에 넣기 위해서 클래스를 만든 것이다.
//결론. Employee을 배열에 넣기 위함.
//2차원 배열은 다 문자열로 만들어야 한다.
//2차원 배열을 발전시킨것이 클래스이다.
int number; // 번호
String name; // 이름
String position; // 직책
int superiorNumber; // 상사번호
String regDate; // 입사일
int salary; // 급여
int commission; // 상여금
int departmentNumber; // 부서번호
}
public class C1101개념04_클래스 {
public static void main(String[] args) {
Employee emp = new Employee();
emp.number = 7369;
emp.name = "SMITH";
emp.position = "CLERK"; // 사무원
emp.superiorNumber = 7902;
emp.regDate = "17-12-1980";
emp.salary = 800;
emp.commission = 0;
emp.departmentNumber = 20;
}
}
- C1101개념05_메모리구조
- 클래스는 메모리가 중요하다.
package C1101클래스;
import java.util.Scanner;
class Sample01 {
int a;
int b;
}
//클래스는 메모리가 가장 중요하다.
public class C1101개념05_메모리구조 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Sample01 s1 = new Sample01();
s1 = new Sample01();
s1.a = scan.nextInt();
// s1 = null;
// s1을 null로 저장하지 않아도
// GC(Garbage Collector)가 알아서 메모리를 해제해준다.
s1 = new Sample01();
scan.close();
}
}
- 메모리
- Scanner scan = new Scanner(System.in);
- 이 줄이 생기면. 1번째 메모리에 scan이 생기고.
- new를 했기 때문에.
- 2번째 메모리에 메모리가 생기고. 그 주소에 1번째 메모리에 주소를 넘겨준다.
- new 하면 heap에 생긴다.
- Sample01 s1 = new Sample01();
- 1번째에 s1이 생기고.
- new를 했기 때문에.
- 2번째 heap에서 메모리가 생기며. 메모리 주소를 1번에 넘겨준다.
- a b
- 만약 s1을 또 new를 하면 어떻게 되냐!
- heap 영역에 또 a와 b가 생긴다.
- 주소 x3이 1번째 s1의 주소를 덮어 씌운다.
- 그럼 첫번째. ab는 어떻게 되느냐.
- GC. 가비지 컬렉터
- heap에서 주소를 잃은 메모리를 삭제하는 역할
- 주인이 없네. 하고 삭제를 한다.
- 면접 질문에 가끔 나온다.
- 그 누구도 x2를 모른다.
- new를 2번해서 그렇다.
- GC. 가비지 컬렉터
- s1 = new Sample01();
- new를 또 했다.
- heap에 또 생긴다.
- a b 가 생기고
- x4가 s1에 들어간다. 주소가.
- 가비지 컬렉터가 삭제를 한다.
- 자바는 메모리를 효율적으로 관리를 한다.
- 편하다.
- 개발자가 신경 쓸 것이 없다.
- c언어는 이것을 직접 해야한다.
- 그래서 어렵다.
- Scanner scan = new Scanner(System.in);
- C1101개념06_메모리구조
- new를 안하면 어떻게 되나.
- new를 안 하면 null이 들어있다.
- 주소가 null
- null == 0 과 같다.
package C1101클래스;
class Sample02 {
int[] arr = {10, 20, 30, 40, 50};
}
public class C1101개념06_메모리구조 {
public static void main(String[] args) {
//new를 안하면 어떻게 되는가.
//null은 0과 같다.
//new를 안하면 null이 된다.
// (1) 기본형 변수
int num = 10;
// (2) 주소 변수
//주소 변수는 null로 초기화를 한다.
int[] arr = null;
// 클래스도 배열과 마찬가지로 null로 초기화한다.
Sample02 s2 = null;
}
}
- C1101개념07_메모리구조
package C1101클래스;
class Sample03 {
int[] arr = {10, 20, 30, 40, 50};
}
public class C1101개념07_메모리구조 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
Sample03 s3 = new Sample03();
s3.arr = arr;
System.out.println(s3.arr[0]);
System.out.println(s3.arr[1]);
System.out.println(s3.arr[2]);
System.out.println(s3.arr[3]);
System.out.println(s3.arr[4]);
}
}
- 메모리
- int[] arr = {1, 2, 3, 4, 5};
- 1번
- arr이 생긴다. new를 했기 때문에
- 메모리 주소 . x1
- 2번
- new를 해서. 메모리 자리가 생김
- 메모리 주소 x1을 1번에 준다.
- 1번
- Sample03 s3 = new Sample03();
- 1번
- s3를 만들고
- new는 Sample03();를 했다.
- 주소 x2를 받음
- 2번
- arr이 생기는 것이다.
- 주소 x2
- heap에서 arr이 new를 한다.
- new => int[] arr = {10, 20, 30, 40, 50};
- 메모리 5칸이 생긴다.
- 주소 x3
- new => int[] arr = {10, 20, 30, 40, 50};
- 1번
- s3.arr = arr;
- 주소가 바뀐다.
- heap에 있는 arr의 주소가 바뀐다.
- x3 => x1으로 바뀐다.
- 이러면 또 가비지컬렉터가 나온다. GC
- X3는 아무도 안 갖고 있어서. 메모리가 삭제된다.
- int[] arr = {1, 2, 3, 4, 5};
- C1101개념08_메모리구조
package C1101클래스;
class Sample04 {
int[] arr = {10, 20, 30, 40, 50};
}
public class C1101개념08_메모리구조 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
Sample04 s4 = new Sample04();
Sample04 temp = s4;
s4 = null;
// 에러가 발생한다.
// 15번째 줄에서 null로 삭제했기 때문에
// 새로 new를 하지 않으면 사용할 수 없다.
// NullPointerException
s4.arr = arr;
}
}
- 구분을 할 수 있어야 한다.
- 코드 짜다 보면 . 어디 갔나 싶다.
- EX) 클래스를 만들었는데. 데이터가 안 들어온다.
- 메모리 어딘가에 삭제 당한거다.
- 필시 이런일이 벌어진것이다.
- 메모리
- .NullPointerException
- 에러남
- null에다가 포인트를 찍었다. 라는 뜻
- .NullPointerException
- C1101개념09_메모리구조
package C1101클래스;
import java.util.Arrays;
class Sample05 {
int[][] arr = new int[4][];
}
public class C1101개념09_메모리구조 {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {6, 7, 8, 9, 10};
Sample05 s5 = new Sample05();
System.out.println(s5.arr[0]);
s5.arr[0] = arr1;
s5.arr[1] = arr2;
s5.arr[2] = arr1;
s5.arr[3] = arr2;
s5.arr[1][1] = 100;
for(int i=0; i<s5.arr.length; i++) {
System.out.println(Arrays.toString(s5.arr[i]));
}
}
}
- 메모리
- Sample05 s5 = new Sample05();
- s5가. int[][] arr = new int[4][]; 이것을 만든 것이다.
- 이것은 Sample05만 만든다.
- 1번
- s5가 만들어짐
- 주소 x3
- 2번
- arr이 만들어짐
- 주소 x3
- 주소 x4가 들어간다.
- int[][] arr = new int[4][];
- heap에서 arr이 머리만 4개를 만든것이다.
- 머리 4개 메모리 만들어지고.
- 주소 x4
- s5가. int[][] arr = new int[4][]; 이것을 만든 것이다.
- 제일 중요한것. 변수가 몇개냐.
- Sample05 s5 = new Sample05();
- C1101개념10_메모리구조
package C1101클래스;
import java.util.Arrays;
class Sample06 {
int[] arr = {1, 2, 3, 4, 5};
}
class Sample07 {
int[] arr = {5, 4, 3, 2, 1};
}
public class C1101개념10_메모리구조 {
public static void main(String[] args) {
Sample06 s6 = new Sample06();
Sample07 s7 = new Sample07();
s6.arr = s7.arr;
s7.arr = s6.arr;
System.out.println(Arrays.toString(s6.arr));
System.out.println(Arrays.toString(s7.arr));
}
}
- 메모리
- C1101개념11_메모리구조
package C1101클래스;
import java.util.Arrays;
class Academy {
int[] hakbun = {1001, 1002, 1003, 1004, 1005};
int[] score = { 92, 38, 87, 100, 11};
}
public class C1101개념11_메모리구조 {
public static void main(String[] args) {
// 메모리 그림그리기
int arr[] = new int[3];
Academy ac = new Academy();
arr = ac.score;
arr[1] = 100;
System.out.println(Arrays.toString(ac.score));
Academy ac2 = ac;
ac2.score[2] = 100;
System.out.println(Arrays.toString(ac.score));
}
}
- 메모리
반응형
'코딩 > 2-JAVA' 카테고리의 다른 글
C1103클래스배열_문자열 , C1104클래스배열_심화 (1) | 2025.07.03 |
---|---|
C1102클래스배열 , C1102클래스배열_개념연습(1~4) (0) | 2025.07.01 |
C1005파싱 , C1006입력 (1) | 2025.06.27 |
C1001문자열, C1002문자열함수, C1003형변환, C1004문자열과데이터 (0) | 2025.06.27 |
C0911어레이리스트 (0) | 2025.06.27 |