
기초 변수(Primitive Variable)
값이 저장
크기가 정해져 있음 → jvm이 미리 공간 설계 가능
예시) 사람의 이름 : 2자리, 3자리 등 가변적
주민등록번호 : 13자리 (int→52Byte, →13Byte)
성별 : 1자리
예시) 카톡 채팅 메세지 : 자료형 설계
분석 : 가장 길었던 메세지 10000자
기초변수로 자료형 설계 가능 → char 10000개 = 20000Byte
최대로 설계해서 메모리가 낭비됨
⇒ 기초 변수로 설계 못하고 참조 변수를 사용
참조 변수(Reference Variable)
크기를 알 수 없을 때
값이 아닌 주소 값(포인터)이 들어있음
예시) n=*1000
1000번지에 가보니 값이 있음

this
현재 객체 자신
컴파일러에서 자동 생성
매개 변수의 이름과 필드의 이름이 동일한 경우 사용해서 구분
this()
다른 생성자
생성자 안에서만 호출 가능
반드시 첫 번째 문장이여야 함
다른 생성자를 호출할 때만 사용
연습문제
package ex04; public class DeskLamp { private boolean isOn; public void turnOn(){ isOn = true; } public void turnOff(){ isOn = false; } @Override public String toString() { return "현재 상태는 " + (isOn == true ? "켜짐" : "꺼짐"); } }
package ex04; public class DeskLampTest { public static void main(String[] args) { DeskLamp myLamp = new DeskLamp(); myLamp.turnOn(); System.out.println(myLamp); myLamp.turnOff(); System.out.println(myLamp); } }

package ex04; class Box { int width; int length; int height; double getVoume(){ return (double) width * height * length; } } public class BoxTest{ public static void main(String[] args) { Box b; b = new Box(); b.width = 20; b.length = 20; b.height = 30; System.out.println("상자의 가로, 세로, 높이는 " + b.width + ", " + b.length + ", " + b.height + "입니다."); System.out.println("상자의 부피는 " + b.getVoume() + "입니다."); } }

package ex04; public class Television { int channel; int volume; boolean onOff; public static void main(String[] args) { Television myTv = new Television(); myTv.channel = 7; myTv.volume = 10; myTv.onOff = true; Television yourTv = new Television(); yourTv.channel = 9; yourTv.volume = 12; yourTv.onOff = true; System.out.println("나의 텔레비전의 채널은 " + myTv.channel + "이고 볼륨은 " + myTv.volume + "입니다."); System.out.println("너의 텔레비전의 채널은 " + yourTv.channel + "이고 볼륨은 " + yourTv.volume + "입니다."); } }

UML(Unified Modeling Language)
소프트웨어 시스템을 시각적으로 표현
클래스 다이어그램 (Class Diagram)
시스템의 클래스, 클래스의 속성, 동작 방식, 객체 간 관계를 표시하여 시스템의 구조를 보여주는 정적 구조 다이어그램


toString : 상태확인
public boolean getIsOn()없이 참조변수만 있어도 자동실행
연습문제
package ex03; class GugudanUtil{ static void gugudan(int x){ //파라미터도 stack에서 만들어짐 for (int i = 1; i <= 9; i++) { System.out.println(x + "*" + i + "=" + (x * i)); } System.out.println(); } } public class GugudanEx04 { public static void main(String[] args) { GugudanUtil.gugudan(10); } }

1차 개발자 : 라이브러리를 만든 개발자
2차 개발자 : 만들어진 라이브러리로 만든 개발자
2차 개발자에게 사용방법 설명하는 방법
public class GugudanEx04 { public static void main(String[] args) { //구구단을 출력해주는 메서드 //ex) GugudanUtil.gugudan(5); //GugudanUtil 클래스에 gugudan 정적 메서드를 호출하시오. //파라미터는 int 1개가 필요합니다. } }
검증 : 최소 몇 가지 실행 > 실무에 적용
메소드 오버로딩
같은 이름의 메소드가 여러개 존재 가능 ⇒ 중복정의
생성자
객체가 생성될 때 초기화하는 특수한 메소드
기본 생성자 : 매개변수가 없음
연습문제
package ex04; public class MyMath { int add(int x, int y){ return x+y; } int add(int x, int y, int z){ return x + y + z; } int add(int x, int y, int z, int w){ return x + y + z + w; } public static void main(String[] args) { MyMath obj; obj = new MyMath(); System.out.print(obj.add(10, 20) + " "); System.out.print(obj.add(10, 20, 30) + " "); System.out.print(obj.add(10, 20, 30, 40) + " "); } }

package ex04; class Pizza { int size; String type; public Pizza(){ size = 12; type = "슈퍼슈프림"; } public Pizza(int s, String t){ size = s; type = t; } } public class PizzaTest{ public static void main(String[] args) { Pizza obj1 = new Pizza(); System.out.println("(" + obj1.type + "," + obj1.size + ",)"); Pizza obj2 = new Pizza(24, "포테이토"); System.out.println("(" + obj2.type + "," + obj2.size + ",)"); } }

package ex04; class Television01 { private int channel; private int volume; private boolean onOff; Television01(int c, int v, boolean o) { channel = c; volume = v; onOff = o; } void print(){ System.out.println("채널은 " + channel + "볼륨은 " + volume + "입니다."); } } public class TelevisionTest{ public static void main(String[] args) { Television01 myTv = new Television01(7, 10, true); myTv.print(); Television01 yourTv = new Television01(11, 20, true); yourTv.print(); } }

package ex04; class Box01 { int width, height, depth; } public class BoxTeat{ public static void main(String[] args) { Box01 b = new Box01(); System.out.println("상자의 크기: (" + b.width + "," + b.height + "," + b.depth + ")"); } }

package ex04; class A{ private int a; int b; public int c; } public class Test { public static void main(String[] args) { A obj = new A(); obj.b = 20; obj.c = 30; } }
package ex04; class Account { private int regNumber; private String name; private int balance; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getBalance() { return balance; } public void setBalance(int balance) { this.balance = balance; } } public class AccountTest { public static void main(String[] args) { Account obj = new Account(); obj.setName("Tom"); obj.setBalance(10000); System.out.println("이름은 " + obj.getName() + " 통장 잔고는 " + obj.getBalance() + "입니다."); } }

package ex04; class SafeArray { private int a[]; public int lenght; public SafeArray(int size) { a = new int[size]; lenght = size; } public int get(int index){ if(index >= 0 && index < lenght){ return a[index]; } return -1; } public void put(int index, int value){ if(index >= 0 && index < lenght){ a[index] = value; } else { System.out.println("잘못된 인덱스 " + index); } } } public class SafeArrayTest{ public static void main(String[] args) { SafeArray array = new SafeArray(3); for (int i = 0; i < array.lenght+1; i++) { array.put(i, i * 10); } } }

Share article