-
[Java] 메소드(Method)Java 2024. 5. 5. 00:07
메소드란?
클래스(class)의 내부에 존재하며, 메소드는 하나의 특정 기능을 수행 한다.
메소드 vs 함수
메소드는 C나 C++의 함수 개념과 유사하다.
하지만 함수는 특정 기능을 수행하는 코드들을 묶어놓은 개념이고, 메소드는 클래스(class) 내에 포함되어 있는 함수라는 점에서 차이점이 있다.
[함수]
#include<stido.h> void print_lines(void) { printf("---"); } int main(void) { print_lines(); return 0; }
[메소드]
public class Main { static void function(String str) { System.out.println("---"); } public static void main(String[] args) { function(); } }
메소드 정의
public int function(String str){ count = 0; //동작 return count; }
메소드는 이런식으로 접근제어자 + 반환형 + 이름 + 매개변수로 구성된다.
[자바에서 이름 설정]
- 패키지 - 소문자
- 클래스 - 첫글자 대문자
- 메서드 - 소문자
메소드 호출
public void main(String[] args){ function(str); }
이런식으로 함수이름을 써주면 된다. (매개변수가 있으면 매개변수도 함께)
'Java' 카테고리의 다른 글
[Java] Java에서 큰 수 다루기(BigInteger) (0) 2024.05.05 [Java] 리터럴(literal)이란? (0) 2022.12.16 [Java] 데이터 타입 (기본타입, 레퍼런스타입) (0) 2022.12.16 [Java] 화면 출력 함수 - print(), println(), printf() (0) 2022.12.12 [Java] 자바 키워드 (0) 2022.11.21