classData{int x;}classEx6_6 {publicstaticvoidmain(String[] args) {Data d =newData();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 불변. }staticvoidchange(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 stackSystem.out.println("change() : x = "+ x); }}
2. Reference parameter : read & write
classData{int x;}classEx6_6 {publicstaticvoidmain(String[] args) {Data d =newData();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 불변. }staticvoidchange(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 stackSystem.out.println("change() : x = "+ x); }}
Reference type return
classData {int x;}classEx6_8 {publicstaticvoidmain(String[] args) {Data d =newData(); d =10;Data d2 =copy(d);System.out.println("d.x = "+d.x);System.out.println("d2.x = "+d2.x); }staticDatacopy(Data d) {//return type and parameter type are reference typeData tmp =newData();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 =newEx6_8();e.copy();MyMath mm =newMyMath();mm.add(x,y);
classEx6_4 {publicstaticvoidmain(String args[]) {//그냥 호출해서 변수에 저장하면 안 되나? 왜 객체를 생성해서 사용하지?//메소드(클래스)에 접근 및 사용하려면 그 클래스는 참조변수 같은 개념이기 때문에 객체가 있어야 .을 통해 접근하고 사용할 수 있다.//2.MyMath 객체 생성 MyMath mm =newMyMath();//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 클래스 작성 classMyMath {voidprintGugudan(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); } }longmax(long a,long b) {long result =0; result = a > b ? a : b;return result;//return result = a > b ? a: b; }longmin(long a,long b) {long result =0; result = a < b ? a : b;return result;//return result = a > b ? a: b; }longadd(long a,long b) {long result = a + b;return result;// return a + b; // ���� �� ���� �̿� ���� �� �ٷ� ������ �� �� �ִ�. }longsubtract(long a,long b) { return a - b; }longmultiply(long a,long b) { return a * b; }doubledivide(double a,double b) {return a / b; } }