* 함수 호출 방식

 

1) Call By Value
- 메소드를 호출하는 것뿐만 아니라 호출하는 메소드로 처리할 기초 데이터를
   전달하는 경우 Call By Value에의한 호출이라 한다

- 메소드 호출시 메소드로 한 문자, 상수 문자열, 숫자를 전달하면 전부
   값에의한 호출이라고하고 Call By Value 라고 한다.


2) Call By Reference: 참조값에 의한 호출

- 메소드로 클래스의 객체를 전달할 수 있다.   
- 메소드안에서 객체의 멤버변수를 변형하면, 원본 객체의 데이터가 변경된다.
   따라서 변경을 목적으로 하는 경우에도 이용한다.

- 메소드로 클래스의 객체를 전달하면 메모리 주소가 전달(C, C++)되는
   것이 아니라 객체를 가르키고있는 Hash Code(객체가 가지고 있는 지문)가 전달됨으로 
   Call By Reference 라고 한다.

- Call By Value: 숫자계열 전부 해당, String은 클래스이나 Call By Value처럼 사용.
- Call By Reference: 모든 클래스의 객체 전달의 경우

 

 

파일명 : CBRTest.java

 

package oop;

class Animal
{
    String name;
    int age;
    void print()
    {
        System.out.print(name+" ");
        System.out.print(age+" ");
        System.out.println();
    }
}//end

class Fruit
{
    String name;
    int price;
    void print()
    {
        System.out.print(name+" ");
        System.out.print(price+" ");
        System.out.println();
    }
}//end

class Output
{
    void prn(Animal ref)
    {
        System.out.print(ref.name+" ");
        System.out.print(ref.age+" ");
        System.out.println();
       
        ref.print();
    }
   
    void prn_fruit(Fruit ref)
    {
        ref.print();
    }
   
    void prn3(Animal d, Fruit a)
    {
        d.print();
        a.print();
    }
}

public class CBRTest {

    public static void main(String[] args) {
       Animal dog=new Animal();
       dog.name="퍼피";
       dog.age=5;
       dog.print(); //Call By Value(값)
      
       Output out=new Output();
       out.prn(dog); //Call By Reference(객체)

       Fruit apple=new Fruit();
       apple.name="대구사과";
       apple.price=1500;
       out.prn_fruit(apple);
      
       out.prn3(dog,apple);

    }

}

 

파일명 : CBRTest2.java

 

package oop;

class Tree
{
    String name;
    int age;
    void print()
    {
        System.out.print(name+" ");
        System.out.print(age+" ");
        System.out.println();
    }
   
}//end

public class CBRTest2 {

    public static void main(String[] args) {

        Tree one=new Tree();
        one.name="소나무";
        one.age=10;
        one.print();

        Tree two=new Tree();
        two.name="박달나무";
        two.age=8;
        two.print();
       
        //one객체와 two객체를 서로 교환하기       
        Tree temp=one;
        one=two;
        two=temp;

        one.print();
        two.print();
       
    }

}

'..열심히 공부하세.. > JAVA 문법' 카테고리의 다른 글

[18] 생성자 함수  (0) 2012.04.26
[17] 메소드 오버로딩  (0) 2012.04.25
[15] Math 클래스 및 Wrapper 클래스  (0) 2012.04.24
[메소드 연습]  (0) 2012.04.24
[14] 메소드(Method) 정의  (0) 2012.04.24

+ Recent posts