Parameter

  1. Primitive parameter : read only

class Data{int x;}

class Ex6_6 {
    public static void main(String[] args) {
        Data d = new Data();
        d.x = 10;
        System.out.prinln("main() : x = " +d.x);
        
        change(d.x);
        System.out.println("After changed(d.x)");
        System.out.println("main() : x =" + d.x);//x값 10 불변.
        
    }

    static void change(int x) {//primitive type : read only
        x = 1000;//only exist when change method is being executed
        //after change execution, change method and local variable in change method disappear from call stack
        System.out.println("change() : x = " + x);
    }
}

2. Reference parameter : read & write

class Data{int x;}

class Ex6_6 {
    public static void main(String[] args) {
        Data d = new Data();
        d.x = 10;
        System.out.prinln("main() : x = " +d.x);
        
        change(d.x);
        System.out.println("After changed(d.x)");
        System.out.println("main() : x =" + d.x);//x값 10 불변.
        
    }

    static void change(Data d) {//REFERENCE type : read and write
        x = 1000;//only exist when change method is being executed
        //after change execution, change method and local variable in change method disappear from call stack
        System.out.println("change() : x = " + x);
    }
}
  • Reference type return

class Data {int x;}

class Ex6_8 {
    public static void main(String[] args) {
        Data d = new Data();
        d = 10;
        
        Data d2 = copy(d);
        
        System.out.println("d.x = " + d.x);
        System.out.println("d2.x = " + d2.x);
    }
    
    static Data copy(Data d) {//return type and parameter type are reference type
        Data tmp = new Data();
        tmp.x = d.x;
        
        return tmp;
    }
}

reference return type means return the address of object so that after copy method is ended, the d2 from copy method can be used in main method.

The two static methods, void main() {..} and Data copy(){..} are in same class. There is no reference type variable to get something from copy method. The reasons are 1.copy method is static method. 2. main and copy methods are in same class.

Static method can be used without creating object(reference type variable). Other normal method is used like below.

//원래는 이렇게 해주어야하지만 생략 가능
Ex6_8 e = new Ex6_8();
e.copy();

MyMath mm = new MyMath();
mm.add(x,y);

class Ex6_4 {
	public static void main(String args[]) {
		//그냥 호출해서 변수에 저장하면 안 되나? 왜 객체를 생성해서 사용하지?
		//메소드(클래스)에 접근 및 사용하려면 그 클래스는 참조변수 같은 개념이기 때문에 객체가 있어야 .을 통해 접근하고 사용할 수 있다.
		
		//2.MyMath 객체 생성 
		MyMath mm = new MyMath();
		//3.MyMath 객체 사용(객체의 메소드 호출)
		long result1 = mm.add(5L, 3L);
		long result2 = mm.subtract(5L, 3L);
		long result3 = mm.multiply(5L, 3L);
		double result4 = mm.divide(5L, 3L);

		long resultmax = mm.max(5L, 3L);
		long resultmin = mm.min(5L, 3L);
		
		System.out.println("add(5L, 3L) = " + result1);
		System.out.println("subtract(5L, 3L) = " + result2);
		System.out.println("multiply(5L, 3L) = " + result3);
		System.out.println("divide(5L, 3L) = " + result4);
		System.out.println("max(5L, 3L) = " + resultmax);
		System.out.println("min(5L, 3L) = " + resultmin);
		
		mm.printGugudan(9);
	}
 }
//메소드는 클래스다.
//메소드는 클래스 영역에만 정의할 수 있다!!!!!!!
//1. 메소드 작성 - MyMath 클래스 작성 
 class MyMath {
	 void printGugudan(int dan) {
		 if(!(2<=dan && dan <=9)) {
			 System.out.println("2와 9사이의 정수를 입력해주세요.");
			 return;
		 }
		 
		 for(int i=1;i <=9; i++) {
			 System.out.printf("%2d X %2d = %2d%n",dan,i,dan*i);
		 }
	 }
	 long max(long a, long b) {
		 long result = 0;
		 result = a > b ? a : b;
		 
		 return result;
		 //return result = a > b ? a: b;
	 }
	 long min(long a, long b) {
		 long result = 0;
		 result = a < b ? a : b;
		 
		 return result;
		 //return result = a > b ? a: b;
	 }
	long add(long a, long b) {
		long result = a + b;
		return result;
	//	return a + b;	// ���� �� ���� �̿� ���� �� �ٷ� ������ �� �� �ִ�.
	}
	
	long subtract(long a, long b) { return a - b; }
	long multiply(long a, long b) { return a * b; }
	double divide(double a, double b) {
		return a / b;
	}
 }

Last updated