Backend/JAVA2 멘토시리즈

JAVA Eclipse 17 method 메서드(메소드)

쏠솔랄라 2023. 4. 7. 08:24

 

 

메서드

 

: 여러 명령들의 나열된 묶음 ; 멤버 함수

 

클래스 내부에서 생성

입력값을 받아 내부에서 처리한 후 결과를 밖으로 출력 또는 반환하는 역할

 

 

 

메서드의 사용 목적

여러 명령들을 하나의 이름을 호출(사용)함으로써, 

한번에 명령을 내리고자 하는 것

 

 

메서드의 구조

 

 

메서드의 구조

구현부
반환타입 메서드명 (타입 변수명, 타입 변수명) {
명령1;
명령2;
return 값;
}

static void open () {
System.out.println("문 열기");
System.out.println("바닥 청소하기");
System.out.println("창문 열기");
}
호출부
메서드명 ( ); public static void main (String[ ] args ) {

Method1.open( );
}

 

 

ex1.

타입 변수명: 매개변수 ; 함수의 호출부에서 구현부로 전달하고 싶은 값이 있을 때 전달할 값을 저장할 공간

 

반환타입 메서드명 (타입 변수명, 타입 변수명) {
명령1;
명령2;
return 값;
}

  

public class Method1 {

public static void main(String[] args) {

f1(100);

}

 

static void f1 (int x) {

System.out.println(x);

}

}

 

 

* return 타입이 void인 경우 리턴값이 존재하지 않는다

 

 

ex2.

 

반환타입과 return타입

return 값 : 함수에 구현된 모든 명령 실행 후 함수가 갖게 될 결과값
return 타입 (반환 타입) : 반환하는 값의 자료형

 

public class Method2 {

public static void main(String[] args) {

System.out.println(f());

}

 

static int f(){

System.out.println("f() 호출");

return 100;

}

}

 

 

함수 f( )를 호출하고 나서 돌려받는 값 : 100

 

 

ex3.

public class Method3 {

public static void main(String[] args) {

System.out.println("사각형의 넓이 : " + calculator(10, 20));

}

 

static int calculator (int a, int b) {

int square = a*b;

return square;

}

}

 

 

  


 

 

JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63

JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71

JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74

JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78

JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80

JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
https://developernew.tistory.com/84

JAVA Eclipse 07 제어문 : 조건문
https://developernew.tistory.com/88

JAVA Eclipse 08 제어문 : 조건문 switch + Random 클래스
https://developernew.tistory.com/93

 

JAVA Eclipse 09 제어문 : 반복문 for

https://developernew.tistory.com/102

 

JAVA Eclipse 10 제어문 : 반복문 while, do-while

https://developernew.tistory.com/103

 

JAVA Eclipse 11 제어문 : 반복문의 break, continue

https://developernew.tistory.com/106

 

JAVA Eclipse 12 배열 : 배열의 개념 및 사용

https://developernew.tistory.com/107

 

JAVA Eclipse 13 배열 : 예제풀이, 로또번호 생성기

https://developernew.tistory.com/115

 

JAVA Eclipse 14 배열 : 다차원배열

https://developernew.tistory.com/117

 

JAVA Eclipse 15 배열의 복제, for ~ each문
https://developernew.tistory.com/120

 

JAVA Eclipse 16 카페 주문 시스템(키오스크) 배열과 제어문(반복문, 조건문)으로 풀기https://developernew.tistory.com/124