#자바 #Method #add #max

 

 public static void main(String[] args) {
  
  int x = 10;
  int y = 20;
  
  int sum = add(x,y); // 메서드 호출(call)
  System.out.println("sum = " + sum);
  
  int maxNum = max(x,y);
  System.out.println("maxNum = " + maxNum); // 메서드 호출(call)
  System.out.println("큰 수 = " + max(152,156)); // 당연히 이렇게, 따로 변수에 담지 않고 바로 사용도 가능하다
 }
 
 // public(해당 메소드를 호출 할 수 있는 접근 제한자)
 // public > protected > default(아무것도 적지 않는 경우) > private
 /* 누구나 접근 가능 > 같은 패키지(폴더) 내 접근 가능 cf. 상속 관계에선 가능 > 같은 패키지(폴더) 내 접근 가능
     > 외부(클래스)에서 접근 불가, 오직 자기 자신(클래스)만 접근 가능 */
 
 // 메서드 선언(덧셈)


 public static int add(int a, int b) {
  
  int result = a + b;
  return result; // 결과값, Output
 }


 // 메서드 선언(삼항 연산자를 사용해 최대값 구하기)

 // return x >= y ? x : y;  => x가 y보다 크거나 같으면 x 반환(참,True), x가 y보다 작으면 y 반환(거짓,false)

 public static int max(int x, int y) {
  return x >= y ? x : y;
  
 }

+ Recent posts