method
ํ ์ธ์ด์ ํจ์ function ๊ณผ ๊ฐ์ ๊ฐ๋
์๋ฐ๋ ๋ชจ๋ ๊ฒ์ด ํด๋์ค์ ์์์ด๋ฏ๋ก ๋ฉ์๋ method ๋ผ ๋ถ๋ฆ
main() ๋ฉ์๋
์๋ฐ ์์ฉํ๋ก๊ทธ๋จ์ ์คํ ์์ ๋ฉ์๋
๋ฐ๋์ static, public, void, ๋งค๊ฐ ๋ณ์ ํ์ ์ ๋ฌธ์์ด ๋ฐฐ์ด

package sec04.chap05;
public class MainMethod {
public static void main(String[] args) {
String a = args[0];
String b = args[1];
String c = args[2];
String d = args[3];
}
}




์์ 3-13
๋ฉ์๋์ ์๋ฏธ 1 . ๋ฐ๋ณต์ ์ต์ํ
double x = 3, y = 4;
System.out.printf("%f + %f = %f%n", x, y, x + y);
System.out.printf("%f - %f = %f%n", x, y, x - y);
System.out.printf("%f * %f = %f%n", x, y, x * y);
System.out.printf("%f / %f = %f%n", x, y, x / y);
x = 10; y = 2;
System.out.printf("%f + %f = %f%n", x, y, x + y);
System.out.printf("%f - %f = %f%n", x, y, x - y);
System.out.printf("%f * %f = %f%n", x, y, x * y);
System.out.printf("%f / %f = %f%n", x, y, x / y);
x = 7; y = 5;
System.out.printf("%f + %f = %f%n", x, y, x + y);
System.out.printf("%f - %f = %f%n", x, y, x - y);
System.out.printf("%f * %f = %f%n", x, y, x * y);
System.out.printf("%f / %f = %f%n", x, y, x / y);
package sec04.chap05;
public class Ex01 {
public static void main(String[] args) {
double xx = 3, yy = 4;
addSubtMultDiv(xx, yy);
xx = 10; yy = 2;
addSubtMultDiv(xx, yy);
xx = 7; yy = 5;
addSubtMultDiv(xx, yy);
}
// โญ๏ธ ๋ฉ์ธ ๋ฉ์๋ ์ธ๋ถ์ ์ ์ธํ ๊ฒ
static void addSubtMultDiv (double a, double b) {
System.out.printf("%f + %f = %f%n", a, b, a + b);
System.out.printf("%f - %f = %f%n", a, b, a - b);
System.out.printf("%f * %f = %f%n", a, b, a * b);
System.out.printf("%f / %f = %f%n", a, b, a / b);
}
}
๋ฉ์๋์ ์๋ฏธ2. ๊ฐ์ ๋ฐ๊ณ ์ฐ์ฐํ์ฌ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํ - ํจ์
package sec04.chap05;
public class Ex02 {
public static void main(String[] args) {
// โญ๏ธ ๋ฐํํ๋ค๋ ๊ฒ: ๋ฐ๊ฟ ์ธ ์ ์๋ค๋ ๊ฒ
// ๋ฉ์๋ ์คํ๋ฌธ์ ๋ฉ์๋์ ๋ฐํ๊ฐ์ผ๋ก ์นํ
int int1 = add(2, 3);
System.out.println(int1);
System.out.println(add(4, 5));
int int2 = add(add(6, 7), add(8, 9));
System.out.println(int2);
}
static int add (int num1, int num2) {
return num1 + num2;
}
}
static boolean checkIfContain (String text, String token) {
return text.toLowerCase().contains(token.toLowerCase());
}
System.out.println("\n- - - - -\n");
if (checkIfContain("Hello World","hello")){
System.out.println("ํฌํจ๋จ");
}else {
System.out.println("ํฌํจ ์ ๋จ");
}

static
: ์ ์ ๋ฉ์๋ (ํด๋์ค ๋ฉ์๋) ์์ ํธ์ถํ๋ ค๋ฉด(main ๋ฑ) ๋ถ์ด์ผ ํจ์ ์ ์ด ์๋ ๋ฉ์๋ (์ธ์คํด์ค ๋ฉ์๋) ๋ ๊ฐ์ฒด์งํฅ ์น์ ์์ ๋ฐฐ์ธ ๊ฒ
๋งค๊ฐ๋ณ์ parameter : ๊ฐ๊ฐ ์๋ฃํ๊ณผ ๋ณ์๋ช ์ ์ ์. ์์ ์ ๋น ๊ดํธ
ํธ์ถํ ๋ ๋ฃ๋ ๊ฐ (
add(3, 4)
์ 3๊ณผ 4)์ ์ธ์ argument ๋ผ๊ณ ํจ
return
: ๋ฐํํ๋ ๊ฐ์ด ์์ ๋, ๋งจ ๋ง์ง๋ง์ ๋ถ์
๋ฉ์๋ ์ฌ์ฉ ์์
package sec04.chap05;
public class Ex03 {
public static void main(String[] args) {
sayHello();
int count1 = getCount();
int count2 = getCount();
int count3 = getCount();
int count4 = getCount();
}
// ๋งค๊ฐ๋ณ์๋, ๋ฐํ๊ฐ๋ ์๋ ๋ฉ์๋
static void sayHello () {
System.out.println("์๋
ํ์ธ์!");
}
static int count = 0;
// ๋งค๊ฐ๋ณ์ ์์ด ๊ฐ๋ง ๋ฐํํ๋ ๋ฉ์๋
// ์ธ๋ถ ๋ณ์ ๊ฐ์ ๋ณํ์ํด (static์ด๋ฏ๋ก static ๋ณ์๋ง ๊ฐ๋ฅ)
static int getCount () {
System.out.println("์นด์ดํธ ์ฆ๊ฐ");
return ++count;
}
}
return
์println
์์ค๋ก ์ฎ๊ฒจ ๋ณผ ๊ฒreturn
์ ๋ธ๋ก์ ์ข ๋ฃํ๋ฏ๋ก ์ดํ์ ์ฝ๋ ๋ฌดํจํ - ์ปดํ์ผ ์๋ฌ ๋ฐ์
๋ฐํ๊ฐ์ ์๋ฃํ์ ๋ฐ๊ฟ ๋ณผ ๊ฒ *(
short
,long
, ๊ธฐํโฆ)๋ฐํ๊ฐ์ด๋ ์ธ์์ ์๋ฃํ์ ์ด๊ธ๋ ๊ฒฝ์ฐ ์ปดํ์ผ ์๋ฌ ๋ฐ์
โ ๏ธ ์ธ๋ถ์ ๋ณ์ ๊ฐ์ ๋ฐ๊พธ๋ ๊ฒ์ ์ข์ ๋ฉ์๋๊ฐ ์๋
package sec04.chap05;
public class Ex04 {
public static void main(String[] args) {
double avg = getAverage(new int[] {3, 5, 4, 13, 7});
}
static double getAverage (int[] nums) {
double sum = 0;
for (int num : nums) {
sum += num;
}
return sum / nums.length;
}
}
package sec04.chap05;
public class Ex05 {
public static void main(String[] args) {
int[] numbers = {3, 5, 9, 2, 8, 1, 4};
// ๋ณ์์ ๋ด์ ๋ณด๋ค ํจ์จ์ ์ผ๋ก ๋ฐ๊ฟ๋ณผ ๊ฒ
int maxOfNumbers = getMaxAndMin(numbers)[0];
int minOfNumbers = getMaxAndMin(numbers)[1];
}
// ์๋ฐ์ ๋ฉ์๋๋ ํ๋์ ๊ฐ๋ง ๋ฐํ ๊ฐ๋ฅ
// ์ฌ๋ฌ ๊ฐ์ ๋ฐํํ๋ ค๋ฉด ๋ฐฐ์ด ๋๋ ์ดํ ๋ฐฐ์ธ ๊ฐ์ฒด๋ฅผ ํ์ฉ
static int[] getMaxAndMin (int[] nums) {
int max = nums[0];
int min = nums[0];
for (int num : nums) {
max = max > num ? max : num;
min = min < num ? min : num;
}
return new int[] {max, min};
}
}
Last updated