03_메소드

강제형변환은 메소드 안의 출력문에서 바꿔도 되고 main의 형태를 바꿔줘도 된다

메소드 강제형변환, 여러인자 한번에 호출

강제형변환은 메소드 안의 출력문에서 바꿔도 되고 main의 형태를 바꿔줘도 된다

단, 메소드의 인자선언에서 바꾸면 오류남

=⇒ 당연한거 int hp = 10000; 에서 char hp 10000; 오류날수밖에 없음

package com.ohgiraffers.section01.method;

public class Test10 {

    public static void main(String[] args) {

        String name = "소환사 이름";
        int hp = 10000;
        char mp = '겨';
        double time = 20.666;
        Test10 app10 = new Test10();
        app10.methodTest(name,hp,mp,time);


    }


    public void methodTest(String name,int hp,char mp,double time){

        System.out.println("name = " + name + ", hp = " + hp + ", mp = " + (double)mp + ", time = " + (int)time);


    }
}

리턴

package com.ohgiraffers.section01.method;


public class Test09 {

    public static void main(String[] args) {
        // 리턴 연습

        int a = 10;
        int b = 20;

        int e = (Test09.methodTest(a, b)) * 8;
        System.out.println("e = " + e);
    }

    public static int methodTest(int a, int b) {


        return a + b;
    }
}

리턴의 값이 무엇으로 돌아가는지 이해 못하였었는데

Test09.methodTest() 자체가 값을 가지고 있는 거였음.

void 는 컴파일러에서 자동으로 리턴해줌

나머지는 return 구문을 반드시 써야함.

C에서 return 0; 느낌

스태틱

컴파일하면 static은 main 메소드 읽기전 미리 읽어둠

메모리는 잡아먹지만 많이쓰는 메소드일경우 쓰임.

다른 클래스의 메소드 호출

//Calculator
package com.ohgiraffers.section01.method;

public class Calculator {

   public int minNumberOf(int first, int second){

       // 필기. 두 수를 전달 받아 작은 수를 반환

       return (first > second) ? second : first;
   }

   public static int maxNumberOf(int first, int second) {

       // 필기. 두 수를 전달 받아 큰 수를 반환

       return (first > second) ? first : second;


   }

}
package com.ohgiraffers.section01.method;

public class Test11 {

    public static void main(String[] args) {

        // Calculator 클래스 에 있는 계산기를 가져올거임
        // minNumberOf

        Calculator app11 = new Calculator();
        int result1 = app11.minNumberOf(20,30);           // non-static
        System.out.println("result1 = " + result1);
        int result2 = Calculator.maxNumberOf(40,60);                    // static
        System.out.println("result2 = " + result2);
    }
}

non - static 은 원래 메소드 부르듯이

⇒ 상자안에 클래스 만들어주고

static 은 바로 불러올수 있음.

패키지

다른 패키지에서 클래스 가져오는기

package com.ohgiraffers.section02.package_and_import;

import com.ohgiraffers.section01.method.Calculator;

public class Test01 {
    public static void main(String[] args) {


        com.ohgiraffers.section01.method.Calculator app1 = new com.ohgiraffers.section01.method.Calculator();
        app1.minNumberOf(20,40);

        Calculator.maxNumberOf(70,20);
        // static 은 다른 패키지도 미리 로딩됨???

    }
}

임포트

package com.ohgiraffers.section02.package_and_import;

import com.ohgiraffers.section01.method.Calculator;




public class ImportTest02 {

    public static void main(String[] args) {

        Calculator app2 = new Calculator();
        int result1 = app2.minNumberOf(100,40);
        System.out.println("result1 = " + result1);

        int result2 = Calculator.maxNumberOf(500,20);
        System.out.println("result2 = " + result2);


    }

}

스태틱 maxNumberOf 를 불러오려면 Calculator.maxNumberOf 를 불러와야하는데

빨간줄 뜸 why???

API

package com.ohgiraffers.section03.math;

import java.util.Random;

public class Test01 {

    public static void main(String[] args) {

       // 절댓값 출력
        int a = -200;
        int result = java.lang.Math.abs(a);

        System.out.println("result = " + result);

        // 절댓값 출력뒤 루트값 출력
        int b = -400;
        double c = Math.sqrt(Math.abs(b));
        System.out.println("c = " + c);
    }
}

랜덤값

package com.ohgiraffers.section03.math;

public class Test02 {

    public static void main(String[] args) {


        // 20~ 40 까지의 랜덤값

        int result = (int) (Math.random() * 21) + 20;
        System.out.println("result = " + result);
            

    }
}

java.lang패키지는 import 하지 않고 사용할 수 있도록 해 놓았다.

package com.ohgiraffers.section03.math;

import java.util.Random;

public class Test02 {

    public static void main(String[] args) {


        // 20~ 40 까지의 랜덤값

        int result = (int) (Math.random() * 21) + 20;
        
        System.out.println("result = " + result);
        // java.lang패키지는 import 하지 않고 사용할 수 있도록 해 놓았다.
        
        java.util.Random random = new Random();
        
        int result1 = random.nextInt(21) + 20;
        
        System.out.println("result1 = " + result1);

    }
}