○ 메소드 (Method)
- 메소드 : JAVA의 함수를 일컫는 말 (참고 : 자바스크립트 함수)
- 메소드는 클래스 내에 존재함
- 종류
① 리턴값(return)의 유/무
② 전달값(argument value)의 유/무
○ 리턴값이 없는 경우
- void : 리턴값이 없다
- 전달값, 파라미터, 매개변수 (argument value, parameter)
① 전달값(argument value)이 없는 경우
public class Test04_method {
public static void test1() {
System.out.println("JAVA");
}//test1()end
public static void test2() {
System.out.println("Python");
return; // 함수는 호출한 시점으로 되돌아 간다
// 마지막 return명령어는 생략가능하다
}//test2()end
public static void main(String[] args) {
//함수호출
test1();
test1();
test2();
test1();
}//main end
}//class end
② 전달값(argument value)이 있는 경우
public class Test04_method {
public static void test3(int a) {
System.out.println(a);
return;
}//test3()end
public static void test4(int a, int b, int c) { //매개변수는 개별적으로 선언
System.out.println(a+b+c);
return;
}//test4()end
public static void test5(double a, double b) {
System.out.println(a+b);
return;
}//test5()end
public static void test6(char a, byte b) {
for(int i=1; i<=b; i++) {
System.out.print(a);
}//for end
return;
}//test6()end
public static void main(String[] args) {
test3(10);
test4(20, 30, 40);
test5(1.2, 3.4);
byte num=100;
char ch='#';
test6(ch, num);
}//main end
}//class end
○ 리턴값이 있는 경우
- 지난 번 포스팅한 Math클래스의 메소드는 리턴값이 있는 메소드 (참고 : 자바 Math 클래스)
System.out.println(Math.abs(-3)); //-3의 절대값이 리턴됨
System.out.println(Math.max(5, 7)); //5, 7중 최대값이 리턴됨
- 입력 값이 있고 리턴값이 있는 메소드가 일반적인 메소드
- 리턴값이 있는 경우 리턴값의 자료형을 void 자리에 쓴다
- JAVA에서 리턴값은 1개만 가능
public class Test01_method {
public static int test1(int a, int b) {//int가 리턴
int c=a+b;
return c;
}//test1() end
public static String test2(int a) {//String이 리턴
if(a%2==0) {
return "짝수";
}else {
return "홀수";
}//if end
}//test2() end
public static boolean test3(int y) {//boolean이 리턴
if(y%4==0 && y%100!=0 || y%400==0) {
return true;
}else {
return false;
}//if end
}//test3() end
public static long test4(int f) {//long이 리턴
long gop=1;
for(int a=f; a>=1; a--) {
gop=gop*a;
}//for end
return gop;
}//test4() end
public static long fact(int f) {//long이 리턴
if(f==0) {
return 1;
}else {
return f*fact(f-1); //재귀적 함수 호출
}//if end
}//fact() end
public static void main(String[] args) {
int result=test1(2, 4);
System.out.println(result);
// 값 : 상수값, 변수값, 리턴값
System.out.println(test1(7, 9));
// 짝수, 홀수 출력하기
String str=test2(5);
System.out.println(str);
// 윤년 확인하기
if(test3(2022)) {
System.out.println("윤년");
}else {
System.out.println("평년");
}//if end
// 팩토리얼 구하기
long f=test4(5);
System.out.println(f);
// 팩토리얼 구하기(재귀적 함수 호출)
System.out.println(fact(4));
}//main end
}//class end
○ Method Overload
- Overload(함수명 중복 정의) : 함수명을 중복해서 사용할 수 있다
- 메소드 오버로딩의 조건
① 매개변수의 개수가 다르다
② 매개변수의 자료형이 다르다
- 주의 : 함수의 리턴형은 메소드 오버로딩의 조건이 아니다
public class Test02_overload {
// 매개변수의 개수나 종류가 다르면, 같은 함수명을 써도 가능
public static void hap(int a) {System.out.println(a);}
public static void hap(int a, int b) {System.out.println(a+b);}
public static void hap(double a) {System.out.println(a);}
public static void hap(double a, double b) {System.out.println(a+b);}
//에러 : 함수의 리턴형은 메소드 오버로딩의 조건이 아니다
//public static int hap(double a) { };
public static void main(String[] args) {
hap(3);
hap(2, 4);
hap(5.6);
hap(7.6, 9.8);
//절대값 구하기
System.out.println(Math.abs(-3)); //int형(기본)
System.out.println(Math.abs(4L)); //long형
System.out.println(Math.abs(5.6f)); //float형
System.out.println(Math.abs(7.8d)); //double형(기본), 접미사d 생략가능
}//main() end
}//class end
'Backend > JAVA_Basic' 카테고리의 다른 글
11. JAVA, 클래스 (class, OOP, new 연산자, 객체, 인스턴스, this, 내부클래스) (0) | 2022.05.31 |
---|---|
10. JAVA, 메소드⑵ (Method, 호출 방식, main, Access Modifier) & 연습문제 (0) | 2022.05.31 |
08. JAVA, 배열 (array, 1차원 배열, 2차원 배열) & 연습문제 (0) | 2022.05.30 |
07. JAVA, 제어문 (반복문, 조건문, for, while, if, switch) & 연습문제 (0) | 2022.05.29 |
06. JAVA, Math 클래스 (난수, 올림, 버림, 반올림 등 ) (0) | 2022.05.28 |
댓글