Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

붓, 그리다

자바의 객체 생성 방법(API 문서 방법 참조) 본문

JAVA/Basic

자바의 객체 생성 방법(API 문서 방법 참조)

붓그린 2017. 6. 16. 17:11

자바의 객체 생성 방법(API 문서 방법 참조)

1. new 연산자를 이용

2. 메서드의 매개변수를 통해서 객체를 얻어오는 방법

1) CallByValue.

package j0609;

// caller method -> work method
// 매개변수를 전달(기본자료형(8byte) -> call By Value(값에 의한 전달방법)
// 그 값은 복사해서 전달
public class CallByValue {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                int r=-1, g=-1, b=-1; // rgb(0~255) 숫자로 표현
                System.out.println("Before:red="+r+", green="+g+", blue="+b);
                // 메서드 호출(change_color)
                change_color(r,g,b);
                System.out.println("After:red="+r+", green="+g+", blue="+b);
                // 복사본(chage_color) 값을 변경해도 메인(원본)의 값은 변경되지 않는다.
                // 원본값은 불변 -> CallByValue


        }

        // 색상을 변경시켜주는 메서드 => 매개변수 O, 반환값 X
        static void change_color(int r, int g, int b) {
                r+=10;
                g+=50;
                b+=100;
                System.out.println("메서드의 내부 r="+r+", g="+g+", b="+b);

        }
}



2) call By Reference

package j0609;

// caller method -> work method
// 매개변수를 전달(객체(주소)를 전달) -> call By Reference(참조에 의한 전달방법)
// main(원본) -> chane_color(원본) -> 원본값을 변경

class RGBcolor{
        int r, g, b;

        RGBcolor(int r, int g, int b){
                this.r=r;
                this.g=g;
                this.b=b;
        }
}

public class CallByRef {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                RGBcolor color=new RGBcolor(-1,-1,-1);
                System.out.println("color값은 "+color);

                System.out.println("Before:red="+color.r+", green="+color.g+", blue="+color.b);
                // 메서드 호출(change_color)
                change_color(color); // 객체를 전달
                System.out.println("After:red="+color.r+", green="+color.g+", blue="+color.b);
                // 원본값은 변경 -> CallByRef


        }

        // 색상을 변경시켜주는 메서드 => 매개변수 O, 반환값 X
        static void change_color(RGBcolor color1) { //자료형이 같아야함
                color1.r+=10;
                color1.g+=50;
                color1.b+=100;
                System.out.println("메서드의 내부 r="+color1.r+", g="+color1.g+", b="+color1.b);

        }
}


- 기본형 자료형 : 8가지
- 참조형 자료형
1) 클래스형(참조형) : 모든 클래스
2) 배열형 : 객체로 취급
3) 인터페이스 형



API 문저 기준
- 매개변수 첫번째 String 객체 하나를 넣어달라
- 매개변수명은 아무거나 쓰면 된다.
fomat(String format, Object... args)

링크걸린 자료형 : callByReference -> 객체 전달 표시
링크 X : 기본자료형(callByValue)
(예외 배열형태의 기본자료형) -> callByReference 객체전달 표시




3. 메서드의 반환형을 통해서 객체를 얻어오는 방법



- totalMemory() : 자바의 메모리 총 사용량
- reeMemory() : 사용하고 남은 용량
- gc() : Runs the garbage collector
- garbage collector : 현재 사용중이지 않은 메모리를 체크 -> 쓸데 없는 메모리를 발견 -> 자동적으로 제거시켜주는 기능























'JAVA > Basic' 카테고리의 다른 글

자바에서의 화면처리(AWT, Swing)  (0) 2017.06.16
재귀호출  (0) 2017.06.16
String클래스 메서드  (0) 2017.06.16
접근제어자  (0) 2017.06.16
2차원 배열  (0) 2017.06.16
Comments