메소드 형식
- 메소드는 클래스의 멤버 함수이다.
인자 전달 방식
기본 타입의 값이 전달되는 경우 ( call by value )
- 인자가 건네는 값이 매개변수에 복사되어서 전달된다.
객체가 전달되는 경우
- 객체의 레퍼런스 값이 전달된다.
배열이 전달되는 경우
- 배열에 대한 레퍼런스 값이 전달된다.
예시
char[] 배열을 전달받아 출력하는 printCharArray() 메소드와 배열 속의 공백(' ') 문자를 '.'로 대치하는 replaceSpace() 메소드를 작성하라.
public class AraayPassingEx {
static void replaceSpace(char a[]) {
for(int i=0; i<a.length; i++)
if(a[i] == ' ')
a[i] = ',';
}
static void printCharArray(char a[]) {
for(int i=0; i<a.length; i++)
System.out.print(a[i]);
System.out.println();
}
public static void main(String[] args) {
char c[] = {'T', 'h', 'i', 's', ' ','i','s',' ','a',' ','p','e','n','c','i','l','.'};
printCharArray(c);
replaceSpace(c);
printCharArray(c);
}
}
실행결과
This is a pencil.
This,is,a,pencil.
'Java > 기본 프로그래밍' 카테고리의 다른 글
[JAVA] static 멤버 ( static 멤버와 static 멤버 활용 ) (0) | 2024.02.09 |
---|---|
[JAVA] 객체 배열 ( 객체 배열 개념 ) (0) | 2024.02.06 |
[JAVA] 생성자 ( 생성자, this 레퍼런스 ) (1) | 2024.02.05 |
[JAVA] 클래스와 객체 ( 객체지향, 클래스 객체 생성 및 활용 ) (0) | 2024.02.02 |
[JAVA] 예외 처리 ( try catch finally 문 ) (0) | 2024.01.31 |