JAVA 프로그래밍

문제

함수 호출시 값을 전달받고, 함수 내에서 기본자료형 수정, 객체 값을 직접 수정, 새 객체를 생성하여 수정하는 프로그램입니다 이를 해결하는 다음 프로그램을 해석하세요 
change() 호출전   argument1 = 1     argument2 = 2     argument3 = 3 
change() 변경전  parameter1 = 1    parameter2 = 2    parameter3 = 3 
change() 변경후  parameter1 = 100  parameter2 = 200  parameter3 = 300 
change() 호출후   argument1 = 1     argument2 = 2     argument3 = 300 

알고리즘

 프로그램 시작 
   전달인자 초기화
   전달인자 수정
프로그램 종료

전달인자 수정
   Pass by Value 후 값 수정
   Pass by Reference 후 새 객체를 생성하여 수정
   Pass by Reference 후 객체 값을 직접 수정

프로그램 코드

	// 파일명 : ./Chapter11/PassByValueVsPassByReference.java
	 
	// 숫자 클래스
	class Int
	{
		protected int value;
		 
		// 값 초기화
P1b		public Int( int value ) {
			this.value = value;
P1e		}
	 
		// 값 문자열 표현
		public String toString() {
			return this.value + " ";
		}
	}
	 
	public class PassByValueVsPassByReference
	{
		// 전달인자 수정 
P2b		public static void change( int parameter1, Int parameter2, Int parameter3 ) {
			System.out.println( "change() 변경전  parameter1 = " + parameter1 + "    parameter2 = " + parameter2 + "   parameter3 = " + parameter3 );
 
			// Pass by Value 후 값 수정  
P21			parameter1 = 100;
			// Pass by Reference 후 새 객체를 생성하여 수정   
P22			parameter2 =
P23			             new Int( 200 );
			// Pass by Reference 후 객체 값을 직접 수정  
P24			parameter3.value = 300;
	 
			System.out.println( "change() 변경후  parameter1 = " + parameter1 + "  parameter2 = " + parameter2 + " parameter3 = " + parameter3 );
P2e		}
	 
		// 프로그램 시작 
1		public static void main( String[] args ) {
			// 전달인자 초기화 
2			int argument1 = 1;
3			Int argument2 =
4			                new Int( 2 );
5			Int argument3 =
6			                new Int( 3 );
 
			// 전달인자 수정 
			System.out.println( "change() 호출전   argument1 = " +  argument1 + "     argument2 = " +  argument2 + "    argument3 = " +  argument3 );
7			change( argument1, argument2, argument3 );
			System.out.println( "change() 호출후   argument1 = " +  argument1 + "     argument2 = " +  argument2 + "    argument3 = " +  argument3 );
	 
		// 프로그램 종료 
8		}
	}

실행 순서

 
 					※ 실행순서 및 메모리상태는 A키(이전) 및 D키(다음)를 눌러도 확인할 수 있습니다