๐Ÿ•ต๏ธโ€โ™€๏ธ Java ๋ฌธ๋ฒ• ๋‹ค์ง€๊ธฐ

์•™์ง€๋™ยท2025๋…„ 2์›” 27์ผ
1

JAVA

๋ชฉ๋ก ๋ณด๊ธฐ
2/6
post-thumbnail
  • Java์—์„œ ๋ชจ๋“  ์ฝ”๋“œ๋“ค์€ ํด๋ž˜์Šค(class) ์•ˆ์— ์กด์žฌํ•จ
    class ํด๋ž˜์Šค์ด๋ฆ„ {
	public static void main(String[] args) { // ํ”„๋กœ๊ทธ๋žจ์˜ ์‹œ์ž‘์ 
				// ์‹คํ–‰๋  ๋ช…๋ น์–ด๋“ค์„ ์ž‘์„ฑํ•˜๋Š” ๊ณต๊ฐ„
    }    
  • ์ค‘๊ด„ํ˜ธ {}
    ์‹œ์ž‘๊ณผ ๋์„ ์˜๋ฏธํ•จ.๊ด„ํ˜ธ ์‚ฌ์ด์— ์ž‘์—…ํ•  ๋‚ด์šฉ์„ ์ž‘์„ฑ

  • ์ฃผ์„ //
    ์ฝ”๋“œ์— ์„ค๋ช…์„ ์ ๋Š” ๋ถ€๋ถ„, ํ”„๋กœ๊ทธ๋žจ์— ์˜ํ–ฅ์„ ์ฃผ์ง€์•Š์Œ

๐Ÿ“Œ ๋ชจ๋“ ๊ฒƒ์ด ํด๋ž˜์Šค

  • ์ž๋ฐ”์˜ ์„ธ์ƒ์€ ๋ชจ๋‘ ๊ฐ์ฒด ํด๋ž˜์Šค
๊ฐœ๋…๋น„์œ ์„ค๋ช…
ํด๋ž˜์Šค์„ค๊ณ„๋„๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์ •์˜
๊ฐ์ฒด์„ค๊ณ„๋„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์กด์žฌํด๋ž˜์Šค์—์„œ ๋งŒ๋“ค์–ด์ง„์กด์žฌ

์ด๋ฆ„ ๊ทœ์น™/์ข…๋ฅ˜

  • ์นด๋ฉœ์ผ€์ด์ŠคcamelCase ๐Ÿซ (firstName, lastName)
  • ์Šค๋„ค์ดํฌ์ผ€์ด์Šค snake_case ๐Ÿ(first_name, last_name)

ํด๋ž˜์Šค์ด๋ฆ„ ์ฒซ ๊ธ€์ž ๋Œ€๋ฌธ์ž + ์นด๋ฉœ์ผ€์ด์Šค ์กฐํ•ฉ์œผ๋กœ ์ž‘์„ฑ
ํŒจํ‚ค์ง€์ด๋ฆ„ ์†Œ๋ฌธ์ž๋กœ๋งŒ์ ๋Š”๊ฒƒ์„ ๊ถŒ์žฅ

๐Ÿ“Œ ๋ณ€์ˆ˜ ๋ฐ์ดํ„ฐ ์ €์žฅ

  • ๋ณ€์ˆ˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ปดํ“จํ„ฐ์— ๋ณด๊ด€ํ•˜๊ธฐ์œ„ํ•œ ๊ณต๊ฐ„
  • ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ์นด๋ฉœ์ผ€์ด์Šค ๊ถŒ์žฅํ•˜๋ฉฐ ๋ช…์‚ฌ ํ˜•ํƒœ ์‚ฌ์šฉ
    ex) myCar

[์ž๋ฃŒํ˜•(int)][๋ณ€์ˆ˜์ด๋ฆ„(a)] [์„ธ๋ฏธ์ฝœ๋ก (a)]

int a;
int b;
์„ธ๋ฏธ์ฝœ๋ก  ; ๋ฌธ์žฅ์ด ๋๋‚จ์„ ์•Œ๋ฆฌ๋Š” ๋งˆ์นจํ‘œ.
(๊ผญ ํ•˜์ž.. ๋งค์ผ๊นŒ๋จน์Œ..์ง„์งœ๋กœ ๋ฉ”๋ชจ ์˜ค๋ฅ˜ ๋ฉ์–ด๋ฆฌ ๋ฐ•์ง€์—ฐ ๋„ˆ)

๋ณ€์ˆ˜ ํ™œ์šฉํ•˜๊ธฐ

  • ์„ ์–ธ๊ณผ ๋™์‹œ์— ๊ฐ’ ํ• ๋‹น

a = 1;
- ๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹น
- ๋ฆฌํ„ฐ๋„ ๋ณ€์ˆ˜์•ˆ์— ์ง์ ‘ ๋„ฃ๋Š” ๊ฐ’ (Literal)
- ํ• ๋‹น ๋ณ€์ˆ˜์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ (Assign)

  • ์„ ์–ธ๊ณผ ๋™์‹œ์— ๊ฐ’ ํ• ๋‹น

int c = 3;

  • ๊ธฐ์กด๋ณ€์ˆ˜์— ๋ฐ์ดํ„ฐ ์žฌํ• ๋‹น( ๋ณ€์ˆ˜๋Š” ์žฌ์‚ฌ์šฉ ํ• ์ˆ˜์žˆ์Œ)

c = 4; //(1)
c = 5; //(2)
c = 6; //(3)

  • ๋ณ€์ˆ˜์— ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹น
    ๋ณ€์ˆ˜์— ๋ฏธ๋ฆฌ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹น ํ• ์ˆ˜์žˆ์Œ(๋ฐ์ดํ„ฐ๊ฐ€ ๋ณต์‚ฌ๋˜์–ด ํ• ๋‹น๋จ)
int d = 1;
int e = d;
d = 10;
System.out.println("e = " + e); // ์ •๋‹ต 1
  • ์ดˆ๊ธฐํ•˜๋Š”๋ฒ•

initialization

โญ๏ธ ๐Ÿ“‘ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋‹จ์œ„

  • ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์œผ๋กœ ๋ฐ์ดํ„ฐ์˜ ์œ„์น˜๋ฅผ ํ‘œํ˜„ํ•˜๊ณ  ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ํ‘œํ˜„ํ•จ
์ž๋ฃŒํ˜• ํฌ๊ธฐ (Byte) ์ €์žฅ ๊ฐ€๋Šฅํ•œ ๊ฐ’ ํŠน์ง• (์‰ฝ๊ฒŒ ์„ค๋ช…!)
boolean 1 byte true ๋˜๋Š” false โœ”๏ธ ์ฐธ/๊ฑฐ์ง“์„ ์ €์žฅ!
์˜ˆ: boolean isJavaFun = true;
char 2 byte 0 ~ 65535 (์œ ๋‹ˆ์ฝ”๋“œ) โœ”๏ธ ํ•œ ๊ธ€์ž ์ €์žฅ!
์˜ˆ: char letter = 'A';
byte 1 byte -128 ~ 127 โœ”๏ธ ์ž‘์€ ์ˆซ์ž ์ €์žฅ (๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ)
์˜ˆ: byte age = 25;
short 2 byte -32,768 ~ 32,767 โœ”๏ธ byte๋ณด๋‹ค ์กฐ๊ธˆ ๋” ํฐ ์ •์ˆ˜ ์ €์žฅ
์˜ˆ: short year = 2024;
int 4 byte -2,147,483,648 ~ 2,147,483,647 โœ”๏ธ ์ž๋ฐ”์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ์ด๋Š” ์ •์ˆ˜ํ˜•!
์˜ˆ: int score = 100;
long 8 byte -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 โœ”๏ธ ์•„์ฃผ ํฐ ์ˆซ์ž ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉ!
์˜ˆ: long distance = 123456789L; (๋์— L ํ•„์ˆ˜!)
float 4 byte ์†Œ์ˆ˜์  ์•ฝ 6~7์ž๋ฆฌ โœ”๏ธ ์‹ค์ˆ˜(์†Œ์ˆ˜์  ์žˆ๋Š” ์ˆซ์ž) ์ €์žฅ!
์˜ˆ: float price = 9.99f; (๋์— f ํ•„์ˆ˜!)
double 8 byte ์†Œ์ˆ˜์  ์•ฝ 15~17์ž๋ฆฌ โœ”๏ธ ๋” ์ •๋ฐ€ํ•œ ์‹ค์ˆ˜ ์ €์žฅ!
์˜ˆ: double pi = 3.1415926535;
String ์ฐธ์กฐํ˜• ๊ธ€์ž ์ˆ˜ ์ œํ•œ ์—†์Œ โœ”๏ธ ์—ฌ๋Ÿฌ ๊ธ€์ž ์ €์žฅ (๋ฌธ์ž์—ด)
์˜ˆ: String name = "Java";


1๏ธโƒฃ ๋…ผ๋ฆฌํ˜•

boolean booleanBox = true;
booleanBox = false;
System.out.println("booleanBox = " + booleanBox);

2๏ธโƒฃ ๋ฌธ์žํ˜• ''๋”ฐ์˜ดํ‘œ๋กœ ์‚ฌ์šฉ

char charBox = 'a';
charBox = 'b';
System.out.println("charBox = " + charBox);

3๏ธโƒฃ ์ •์ˆ˜ํ˜•(short)

short shortBox = 1;
shortBox = 2;
System.out.println("shortBox = " + shortBox);

4๏ธโƒฃ ์ •์ˆ˜ํ˜•(int)

int intBox = 1;
intBox = 2;
System.out.println("intBox = " + intBox);

5๏ธโƒฃ ์‹ค์ˆ˜ํ˜•(double)

double doubleBox = 0.12345678901234567;
doubleBox = 0.123456789012345678;
System.out.println("doubleBox = " + doubleBox);

6๏ธโƒฃ ์‹ค์ˆ˜ํ˜•(float) ์†Œ์ˆ˜์  ๋’ค์— f ๋ฅผ ๊ผญ ๋ถ™์—ฌ์•ผํ•จ!

float floatBox = 0.12345678f;
floatBox = 0.1234567890f;
System.out.println("floatBox = " + floatBox);

๐Ÿ“Œ ์—…์บ์ŠคํŒ…

  • ์ž‘์€ ๋ฐ์ดํ„ฐ๋ฅผ ํฐ ์ƒ์ž์— ์˜ฎ๊ฒจ ๋‹ฎ๋Š”๋‹ค
  • ๋ฐ์ดํ„ฐ ์†์‹ค ์—†์Œ
int smallBox = 10;
double bigBox = smallBox;   //  double <- int ์ž๋™ํ˜• ๋ณ€ํ™˜
System.out.println(bigBox); //  ์ถœ๋ ฅ: 10.0

๐Ÿ’ก์‹ค๋ฌด ํ™œ์šฉ ๋ณ€์ˆ˜ ์ข…๋ฅ˜

  • ๋ฌธ์ž์—ด์˜ ์ฒซ๊ธ€์ž๋Š” ๋Œ€๋ฌธ์ž ๋ฐ์ดํ„ฐ๋Š” "" ๋กœ ๋‚˜ํƒ€๋ƒ„

    ๋ฌธ์ž vs ๋ฌธ์ž์—ด
    char a = 'a';
    String str = "abc";
    String stringBox = "๋ฌธ์ž์—ด์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.";

  • ์ •์ˆ˜ํ‘œํ˜„

    int intBox = 1;
    long longBox = 1;

  • ๋…ผ๋ฆฌ ํ‘œํ˜„

    boolean booleanBox = false;

๐Ÿ–จ๏ธ ์ถœ๋ ฅ๋ฐฉ๋ฒ•

  • ์ถœ๋ ฅํ›„ ์ž๋™์œผ๋กœ ์ค„๋ฐ”๊ฟˆ (println)
System.out.println("Hello");
System.out.println("Java!");

// ์ถœ๋ ฅ๊ฒฐ๊ณผ:
Hello
Java!
  • ์ค„๋ฐ”๊ฟˆ ์—†์ด ์ถœ๋ ฅ (print)
System.out.print("Hello");
System.out.print(" Java!");

// ์ถœ๋ ฅ๊ฒฐ๊ณผ:
Hello Java!
  • ๊ฐœํ–‰๋ฌธ์ž \n ( ์ค„๋ฐ”๊ฟˆ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰)
System.out.println("Hello\nWorld!");

// ์ถœ๋ ฅ:
Hello
World!

๐ŸงŸโ€โ™‚๏ธ ๊ฐ์ฒด ์†Œํ™˜

1๏ธโƒฃ Scanner ๊ฐ์ฒด ์†Œํ™˜

Scanner scanner = new Scanner(System.in);

import java.util.Scanner; // Scanner๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด import ํ•„์š”

public class ScannerPractice {

    public static void main(String[] args) {
        // โœ… Scanner ๊ฐ์ฒด ์ƒ์„ฑ & ๋ณ€์ˆ˜์— ๋‹ด๊ธฐ
        Scanner scanner = new Scanner(System.in);        
    }
}

2๏ธโƒฃ ๋ฌธ์ž์—ด ์ž…๋ ฅ ๋ฐ›๊ธฐ

scanner.nextLine()

import java.util.Scanner;

public class ScannerPractice {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("์ข‹์•„ํ•˜๋Š” ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์„ธ์š”: ");
        String sentence = scanner.nextLine();  // โœ… ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
        System.out.println("์ข‹์•„ํ•˜๋Š” ๋ฌธ์žฅ: " + sentence);
    }
}

3๏ธโƒฃ ์ˆซ์ž ์ž…๋ ฅ๋ฐ›๊ธฐ

scanner.nextInt(), scanner.nextDouble(),scanner.nextLong(),

import java.util.Scanner;

public class ScannerPractice {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: ");
        int num1 = scanner.nextInt();

        System.out.print("์‹ค์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: ");
        double num2 = scanner.nextDouble();

        System.out.print("ํฐ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: ");
        long num3 = scanner.nextLong();

        System.out.println("์ž…๋ ฅํ•œ ์ •์ˆ˜: " + num1);
        System.out.println("์ž…๋ ฅํ•œ ์‹ค์ˆ˜: " + num2);
        System.out.println("์ž…๋ ฅํ•œ ํฐ ์ •์ˆ˜: " + num3);
    }
}

๐Ÿ“– ์—ฐ์‚ฐ์ž ๊ณ„์‚ฐ๋ฒ•

๐Ÿ“Œ ์—ฐ์‚ฐ์ž ์šฐ์„ ์ˆœ์œ„

1 2 3 4 5 6 7 8 9 10
() ++, -- ~, ! *, /, % +, - <, >, <=, >= ==, != && || =, +=, -=, *=, /=, %=

๐Ÿ”ธ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž

์—ฐ์‚ฐ์ž ์„ค๋ช… ์˜ˆ์‹œ ๊ฒฐ๊ณผ ์˜ˆ์‹œ
+ ๋ง์…ˆ 3 + 2 5
- ๋บ„์…ˆ 5 - 2 3
* ๊ณฑ์…ˆ 3 * 2 6
/ ๋‚˜๋ˆ—์…ˆ 6 / 2 3
% ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ 5 % 2 1

๐Ÿ”ธ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž ์˜ˆ์‹œ
int a = 10;
     int b = 3;
       int sum = a + b;   
        System.out.println("sum = " + sum);

        int sub = a - b;  
        System.out.println("sub = " + sub);

        int mul = a * b;  
        System.out.println("mul = " + mul);
        
        int div = a / b;  
        System.out.println("div = " + div);
        
        double div2 = a / 3.0;   // ์†Œ์ˆ˜์ ์œ ์ง€ํ•˜๋ ค๋ฉด ์†Œ์ˆ˜์ ๊ณผ ์—ฐ์‚ฐ ..โญ๏ธ
        System.out.println("div2 = " + div2);
        
        int mod = a % b;
        System.out.println("mod = " + mod);

๐Ÿ”ธ ๋Œ€์ž…์—ฐ์‚ฐ์ž/ ๋ณตํ•ฉ๋Œ€์ž…์—ฐ์‚ฐ์ž

์—ฐ์‚ฐ์ž ์„ค๋ช… ์˜ˆ์‹œ ๊ฒฐ๊ณผ ์˜ˆ์‹œ
= ๋Œ€์ž… ์—ฐ์‚ฐ์ž int a = 5; a = 5
+= ๋”ํ•œ ํ›„ ๋Œ€์ž… a += 2; a = a + 2
-= ๋บ€ ํ›„ ๋Œ€์ž… b -= 3; b = b - 3
*= ๊ณฑํ•œ ํ›„ ๋Œ€์ž… c *= 2; c = c * 2
/= ๋‚˜๋ˆˆ ํ›„ ๋Œ€์ž… d /= 4; d = d / 4
%= ๋‚˜๋จธ์ง€ ํ›„ ๋Œ€์ž… e %= 3; e = e % 3

๐Ÿ”ธ ๋Œ€์ž…์—ฐ์‚ฐ์ž/ ๋ณตํ•ฉ๋Œ€์ž…์—ฐ์‚ฐ์ž์˜ˆ์‹œ

int num = 5;
num+= 3; //๋”ํ•˜๊ณ  ๋Œ€์ž…ํ•ด์ค€๋‹ค
System.out.println("num = " + num); // 8

    num -= 2; // num = num -2;
    System.out.println("num = " + num);//6

    num *= 2; // num = num * 2;
    System.out.println("num = " + num);//12

    num /= 3; // num = num / 3;
    System.out.println("num = " + num);//4

    num %= 3; // num = num % 3;
    System.out.println("num = " + num);//1

๐Ÿ”ธ ์ฆ๊ฐ ์—ฐ์‚ฐ์ž

์—ฐ์‚ฐ์ž ์„ค๋ช… ์ „์œ„ ์—ฐ์‚ฐ ์˜ˆ์‹œ ํ›„์œ„ ์—ฐ์‚ฐ ์˜ˆ์‹œ ๊ฒฐ๊ณผ ์˜ˆ์‹œ
++ ์ฆ๊ฐ€ ์—ฐ์‚ฐ์ž ++a a++ ์ „์œ„ ์—ฐ์‚ฐ: a = a + 1 ํ›„ ์‚ฌ์šฉ,
ํ›„์œ„ ์—ฐ์‚ฐ: a ์‚ฌ์šฉ ํ›„ a = a + 1
-- ๊ฐ์†Œ ์—ฐ์‚ฐ์ž --a a-- ์ „์œ„ ์—ฐ์‚ฐ: a = a - 1 ํ›„ ์‚ฌ์šฉ,
ํ›„์œ„ ์—ฐ์‚ฐ: a ์‚ฌ์šฉ ํ›„ a = a - 1

1๏ธโƒฃ ์ „์œ„ ์—ฐ์‚ฐ ++i

int a = 5;  //๋จผ์ € ์ฆ๊ฐ€ ํ›„ ์ถœ๋ ฅ
System.out.println("++a: " + (++a));  // 6 
System.out.println("ํ˜„์žฌ a ๊ฐ’: " + a);  // 6

2๏ธโƒฃํ›„์œ„ ์—ฐ์‚ฐ i++

int b = 5;  //์ถœ๋ ฅ ํ›„ ์ฆ๊ฐ€
System.out.println("b++: " + (b++));  // 5 โญ๏ธ(๋‹ค๋ฅธ์ )
System.out.println("ํ˜„์žฌ b ๊ฐ’: " + b);  // 6

๐Ÿ”ธ ๋น„๊ต ์—ฐ์‚ฐ์ž

์—ฐ์‚ฐ์ž ์„ค๋ช… ์˜ˆ์‹œ ๊ฒฐ๊ณผ ์˜ˆ์‹œ
== ๋‘ ๊ฐ’์ด ๊ฐ™์€์ง€ ๋น„๊ต 5 == 5 true
!= ๋‘ ๊ฐ’์ด ๋‹ค๋ฅธ์ง€ ๋น„๊ต 5 != 3 true
< ์™ผ์ชฝ ๊ฐ’์ด ์˜ค๋ฅธ์ชฝ ๊ฐ’๋ณด๋‹ค ์ž‘์€์ง€ ๋น„๊ต 3 < 5 true
> ์™ผ์ชฝ ๊ฐ’์ด ์˜ค๋ฅธ์ชฝ ๊ฐ’๋ณด๋‹ค ํฐ์ง€ ๋น„๊ต 5 > 3 true
<= ์™ผ์ชฝ ๊ฐ’์ด ์˜ค๋ฅธ์ชฝ ๊ฐ’๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€์ง€ ๋น„๊ต 3 <= 5 true
>= ์™ผ์ชฝ ๊ฐ’์ด ์˜ค๋ฅธ์ชฝ ๊ฐ’๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€์ง€ ๋น„๊ต 5 >= 5 true

๐Ÿ”ธ ๋น„๊ต ์—ฐ์‚ฐ์ž์˜ˆ์‹œ

1๏ธโƒฃ ๋‘ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด true , ๋‹ค๋ฅด๋ฉด false๋ฅผ ๋ฐ˜ํ™˜

System.out.println("10 == 10: " + (10 == 10)); // true

2๏ธโƒฃ ๋‘ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด true , ๋‹ค๋ฅด๋ฉด false๋ฅผ ๋ฐ˜ํ™˜

System.out.println("10 != 5: " + (10 != 5)); // true

3๏ธโƒฃ >, < ,<=,>= ํฌ๊ธฐ ๋น„๊ต ์—ฐ์‚ฐ์ž ๋…ผ๋ฆฌ์— ๋งž์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜

 System.out.println(" 10 < 5 : " +(10 < 5)); // false
 System.out.println(" 10 >= 5 : " +(10 >= 10)); //true
 System.out.println(" 10 <= 5 : " +(10 <= 5)); // false

๐Ÿ”ธ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž

์—ฐ์‚ฐ์ž ์„ค๋ช… ์˜ˆ์‹œ ๊ฒฐ๊ณผ ์˜ˆ์‹œ
! ๋ถ€์ • (NOT) !(true) false
&&& ๋…ผ๋ฆฌ AND (๋‘ ์กฐ๊ฑด์ด ๋ชจ๋‘ ์ฐธ์ผ ๋•Œ ์ฐธ) true && false false
|| ๋…ผ๋ฆฌ OR (๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ์ฐธ์ด๋ฉด ์ฐธ) true || false true

  • AND / && ๋‘˜ ๋‹ค ์ฐธ์ผ ๋•Œ true (์˜ˆ: true && false โ†’ false)

  • OR/|| ํ•˜๋‚˜๋ผ๋„ ์ฐธ์ด๋ฉด true (์˜ˆ: false || true โ†’ true)

  • NOT/! ๋ฐ˜๋Œ€๋กœ ๋ฐ”๊ฟ”์คŒ(์˜ˆ: !true โ†’ false)

๐Ÿ”ธ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž ์˜ˆ์‹œ

int age = 20;
boolean isStudent = true;
System.out.println(age > 18 || isStudent); // true

1๏ธโƒฃ AND๋Š” ๋‘์กฐ๊ฑด์ด ๋ชจ๋‘ ์ฐธ์ผ๋•Œ true

System.out.println("true && true: " + (true && true));  // true
System.out.println("true && false: " + (true && false)); // false
System.out.println("false && false: " + (false && false)); // false

2๏ธโƒฃOR ๋‘˜์ค‘ ํ•˜๋‚˜๋งŒ ์ฐธ์ด๋ฉด true

System.out.println("true || false: " + (true || false)); // true
System.out.println("false || false: " + (false || false)); // false
System.out.println("true || true: " + (true || true)); // true

3๏ธโƒฃNOT ์—ฐ์‚ฐ์ž false๋กœ, false ์ผ๋•Œ true ๋กœ ๋ณ€๊ฒฝ

System.out.println("!true: " + (!true));  // false
System.out.println("!false: " + (!false)); // true

๐Ÿ”ธ ๋ฌธ์ž์—ด ๋น„๊ต๋Š” ๊ผญ .equals() ํ™œ์šฉ ๋™๋“ฑ์„ฑ๋ฌธ์ œ

์—ฐ์‚ฐ์ž ์„ค๋ช… ์˜ˆ์‹œ ๊ฒฐ๊ณผ ์˜ˆ์‹œ
equals() ๋ฌธ์ž์—ด ๋‚ด์šฉ์ด ๊ฐ™์€์ง€ ๋น„๊ต (๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„) "hello".equals("hello") true
equals() ๋ฌธ์ž์—ด ๋‚ด์šฉ์ด ๋‹ค๋ฅธ์ง€ ๋น„๊ต (๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„) "hello".equals("Hello") false

๐Ÿšฉ ๋ฌธ์ž์—ด ๋น„๊ต

 String text1 = "Hello";
String text2 = "Hello";
boolean isEqual = text1.equals(text2);
System.out.println("text1๊ณผ text2๊ฐ€ ๊ฐ™์€๊ฐ€? " + isEqual);

์ถœ๋ ฅ๊ฒฐ๊ณผ
text1๊ณผ text2๊ฐ€ ๊ฐ™์€๊ฐ€? true

๐Ÿ“š if๋ฌธ

  • if๋ฌธ์€ ์‹ ํ˜ธ๋“ฑ์ฒ˜๋Ÿผ ํŠน์ •์กฐ๊ฑด์ฒ˜๋Ÿผ ์กฐ๊ฑด์— ๋”ฐ๋ผ ํ–‰๋™ํ• ์ˆ˜์žˆ๋„๋ก ๋งŒ๋“ค์ˆ˜์žˆ๋‹ค
  • ์ปดํ“จํ„ฐ๊ฐ€ ๋งžํ•  ์กฐ๊ฑด๋“ค์„ ๋ฏธ๋ฆฌ ์ •์˜ํ•ด ๋†“์ž!

โ˜‘๏ธ if๋ฌธ์€ ๊ฑฐ์ง“,false์ผ ๊ฒฝ์šฐ์—๋Š” ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค!
1. String light = "์ดˆ๋ก๋ถˆ"; ๋ณ€์ˆ˜์ €์žฅ
2. light ์ดˆ๋ก๋ถˆ if ์กฐ๊ฑด ํ™•์ธ
3. true ๋ฉด ์‹คํ–‰ "๊ฑด๋„ˆ์„ธ์š”"
4. if-else์„ ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ

๐Ÿšฉ if๋ฌธ

public class Main {
    public static void main(String[] args) {
        String light = "์ดˆ๋ก๋ถˆ"; 
        if (light.equals("์ดˆ๋ก๋ถˆ")) {
            System.out.println("๊ฑด๋„ˆ์„ธ์š”!");
        }
    }
}

๐Ÿšฉ if-else๋ฌธ

public class Main {

    public static void main(String[] args) {

        String light = "์ดˆ๋ก๋ถˆ";

        // (light == "์ดˆ๋ก๋ถˆ")
        if (light.equals("์ดˆ๋ก๋ถˆ")) {
            System.out.println("๊ฑด๋„ˆ์„ธ์š”!");
        }
        System.out.println("๋ฉˆ์ถ”์„ธ์š”!"); โŒ
    }

๐Ÿšฉ else-if๋ฌธ

public class Main {
    public static void main(String[] args) {
        String light = "์ดˆ๋ก๋ถˆ";               

        if (light.equals("์ดˆ๋ก๋ถˆ")) {          
            System.out.println("๊ฑด๋„ˆ์„ธ์š”!");    

        } else if (light.equals("๋…ธ๋ž€๋ถˆ")) {   
            System.out.println("์ฃผ์˜ํ•˜์„ธ์š”!");   

        } else {  
            System.out.println("๋ฉˆ์ถ”์„ธ์š”!");   
        }

        // โœ… ๋‹ค์Œ์ฝ”๋“œ ์ง„ํ–‰ 
    }
}

๐Ÿ”ธ if ์˜ˆ์‹œ

public class Main {
        public static void main(String[] args) {
            String light = "๋นจ๊ฐ„๋ถˆ";

            if ("light".equals("์ดˆ๋ก๋ถˆ")) {
                System.out.println("๊ฑด๋„ˆ์„ธ์š”");

            } else if (light.equals("๋…ธ๋ž€๋ถˆ")) {
                System.out.println("์ฃผ์˜ํ•˜์„ธ์š”");

            } else if(light.equals("๋นจ๊ฐ„๋ถˆ")){
                System.out.println("๋ฉˆ์ถ”์„ธ์š”");

            } else {
                System.out.println("์ž˜๋ชป๋œ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค.");
            }
        }
    }
}

๐Ÿ“š Switch๋ฌธ

  • ๊ด„ํ˜ธ์•ˆ์— ๋‹จ์ผ๊ฐ’๋งŒ ๋“ค์–ด๊ฐˆ์ˆ˜ ์žˆ์Œ (๋ฆฌํ„ฐ๋Ÿด๊ฐ’,๋ณ€์ˆ˜)
  • ์กฐ๊ฑด์‹ ์‚ฌ์šฉ ์•ˆ๋Œ•~ num > 1 ๋…ธ๋…ธ
<enum
๋ณ€์ˆ˜ ํƒ€์ž… ์„ค๋ช… ์‚ฌ์šฉ ์˜ˆ์‹œ
int ์ •์ˆ˜ ๊ฐ’์„ ์‚ฌ์šฉ
int num = 3;
switch (num) {
    case 1: ... }
char ๋ฌธ์ž ๊ฐ’ ์‚ฌ์šฉ
char letter = 'A';
switch (letter) {
    case 'A': ... }
String ๋ฌธ์ž์—ด ๊ฐ’ ์‚ฌ์šฉ
String day = "์›”์š”์ผ";
switch (day) {
    case "์›”์š”์ผ": ... }
boolean ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ์‚ฌ์šฉ
boolean flag = true;
switch (flag) {
    case true: ... }
์—ด๊ฑฐํ˜• ๊ฐ’์„ ์‚ฌ์šฉ
enum Day { MONDAY, TUESDAY, WEDNESDAY }
Day today = Day.MONDAY;
switch (today) {
    case MONDAY: ... }

๐Ÿ“– ๋ธŒ๋ ˆ์ดํฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€์•Š์œผ๋ฉด ๋ฌดํ•œ๊ตด๋ ˆ

int number = 1;
        switch (number) {
            case 1:
                System.out.println("1์ž…๋‹ˆ๋‹ค.");
                break;
            case 2:
                System.out.println("2์ž…๋‹ˆ๋‹ค");
                break;
            default:
                System.out.println("1๋„ ์•„๋‹ˆ๊ณ  2๋„ ์•„๋‹™๋‹ˆ๋‹ค.");
    }

๐Ÿ”ƒ ๋ฐ˜๋ณต๋ฌธ

๐Ÿ“Œfor๋ฐ˜๋ณต๋ฌธ

  • for ๋ฐ˜๋ณต๋ฌธ์—์„œ๋Š” ๋ฐ˜๋ณต์ž‘์—…์„ ์‹œ์ž‘ํ•˜๊ธฐ์ „ ์ข…๊ฒฐ์กฐ๊ฑด์„ ํ™•์ธ
  • ์ข…๊ฒฐ์กฐ๊ฑด์ด ์ฐธ์„๊ฒฝ์šฐ ๋ฐ˜๋ณต์ „์„ ์ˆ˜ํ–‰,๊ฑฐ์ง“์ผ๊ฒฝ์šฐ ๋ฐ˜๋ณต์ข…๋ฃŒ
for (์‹œ์ž‘์กฐ๊ฑด; ์ข…๊ฒฐ์กฐ๊ฑด; ์กฐ๊ฑด๋ณ€ํ™”์ˆ˜์‹) {
}

for (int i = 1; i <= 10; i++) {
    ์‹œ์ž‘์กฐ๊ฑด / ์ข…๊ฒฐ์กฐ๊ฑด /  ์กฐ๊ฑด๋ณ€ํ™”์ˆ˜์‹ 
   
   System.out.println("์•ˆ๋…•ํ•˜์„ธ์š”!"); > ๋ฐ˜๋ณตํ•  ๋ช…๋ น๋ฌธ x 10 
}

break ํƒˆ์ถœํ•จ๊ป˜์‚ฌ์šฉ

  • for ๋ฌธ ํ™œ์šฉ + break
  • ๋ฐ˜๋ณต๋ฌธ ๋ฒ—์–ด๋‚˜์•ผํ• ๋•Œ ์‚ฌ์šฉํ•จ
public class Robot {
 public static void main(String[] args) {
    
		    int customers = 5;
		    
		    for (int i = 1; i <= customers; i++) { 
				    if (i == 4) {  โœ… 3๋ช…๊นŒ์ง€๋งŒ ํ™˜์˜
						    break; โœ… ๋ฐ˜๋ณต ํƒˆ์ถœ!
				    }
				    System.out.println(i + "๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!");
			  }
			  System.out.println("๋ฐ˜๋ณต๋ฌธ์ด ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
    }
}
// ์ถœ๋ ฅ 
์˜ค๋Š˜์˜ ์†๋‹˜ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: 5
1๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!
2๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!
3๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!
๋ฐ˜๋ณต๋ฌธ์ด ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

continue ๊ฑด๋„ˆ๋›ฐ๊ธฐ skip ๋ฒ„ํŠผ

  • for ๋ฌธ ํ™œ์šฉ + continue
  • ํŠน์ • ํšŒ์ฐจ์˜ ๋ฐ˜๋ณต์„ ๊ฑด๋„ˆ๋›ธ๋•Œ ์‚ฌ์šฉ
  • ํŠน์ •์† ๋‹˜ ์ธ์‚ฌ์•ˆํ•ด! ํ• ๋•Œ ์‚ฌ์šฉ
public class Robot {

    public static void main(String[] args) {

		    int customers = 5;

		    for (int i = 1; i <= customers; i++) {
				    if (i == 2) {  โœ… 2๋ฒˆ์งธ ์†๋‹˜ ๋ง˜์—์•ˆ๋“ฌ
						    continue; โœ… ์†๋‹˜ ์Šคํ‚ต
				    }
				    System.out.println(i + "๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!");
			  }
			  System.out.println("๋ฐ˜๋ณต๋ฌธ์ด ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
    }
}
์˜ค๋Š˜์˜ ์†๋‹˜ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: 5
1๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!
3๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!
4๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!
5๋ฒˆ์งธ ์†๋‹˜, ์•ˆ๋…•ํ•˜์„ธ์š”!
๋ฐ˜๋ณต๋ฌธ์ด ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œwhile ๋ฐ˜๋ณต๋ฌธ

  • while ๋ฐ˜๋ณต๋ฌธ์€ ์ข…๊ฒฐ์กฐ๊ฑด๋งŒ ๊ฐ€์ง€๊ณ ์žˆ๋Š” ๋งค์šฐ ์ •์งํ•œ ์•„์ด
  • ์ข…๊ฒฐ์กฐ๊ฑด์‹์ด true ์ผ๋•Œ ๋ฐ˜๋ณตํ•จ
  • ์ข…๊ฒฐ์กฐ๊ฑด์ด ๊ฑฐ์ง“(false)์ด ๋ ๋•Œ๊นŒ์ง€ ๋ช…๋ น๋ฌธ์„ ๋ฐ˜๋ณตํ•˜๋Š” ์ด์ค‘์„ฑ์„ ๊ฐ€์ง€๊ณ ์žˆ์Œ
  • i++;์„ ๊ผญ ๋ถ™์—ฌ์•ผํ•จ

while (์ข…๊ฒฐ์กฐ๊ฑด) {
๋ฐ˜๋ณตํ•  ๋ช…๋ น๋ฌธ;
}

int i = 1;
while (i <= 10) { โœ… 1์€ 10๋ณด๋‹ค ์ž‘์Œ์œผ๋กœ ๋ฐ˜๋ณต!
System.out.println("i ๊ฐ€ 10 ๋ณด๋‹ค ์ž‘์Šต๋‹ˆ๋‹ค.");
i++; // โœ… ์—†์„ ๊ฒฝ์šฐ ๋ฌดํ•œ๋ฃจํ”„ ๋ฐœ์ƒ
}

๐Ÿ“Œdo-while ๋ฐ˜๋ณต๋ฌธ

  • do-while ์€ while ๋ฌธ๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ์ข…๊ฒฐ์กฐ๊ฑด์„ ๋ฐ˜๋ณต ์ „์ด ์•„๋‹ˆ๋ผ ํ›„์— ์ฒดํฌํ•จ

    do {
    ๋ฐ˜๋ณตํ•  ๋ช…๋ น๋ฌธ..
    } while (์ข…๊ฒฐ ์กฐ๊ฑด)
    int i = 1;
    do {
    System.out.println("์•ˆ๋…•ํ•˜์„ธ์š”.");
    i++;
    } while (i <= 10);

๐Ÿ“Œ ๋ฐฐ์—ด(Array)

๐Ÿ”ด new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐฐ์—ด์„ ์„ ์–ธ

์ž๋ฃŒํ˜•[] ๋ณ€์ˆ˜์ด๋ฆ„ = new ์ž๋ฃŒํ˜•[๋ฐฐ์—ด์˜๊ธธ์ด]; // ๋ฐฐ์—ด์„ ์–ธ

1๏ธโƒฃ ์ •์ˆ˜ํ˜• ๋ฐฐ์—ด ์„ ์–ธ
arr = new int[5]; //๊ธธ์ด๊ฐ€ 5์ธ ์ •์ˆ˜ํ˜•

2๏ธโƒฃ ๋ฌธ์ž์—ด ๋ฐฐ์—ด ์„ ์–ธ
String[] arrA = new String[5]; //๊ธธ์ด๊ฐ€ 5์ธ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์„ ์–ธ

3๏ธโƒฃ๋…ผ๋ฆฌํ˜• ๋ฐฐ์—ด์„ ์–ธ
boolean[] arrB = new boolean[3]; //// ๊ธธ์ด๊ฐ€ 3์ธ ๋…ผ๋ฆฌํ˜• ๋ฐฐ์—ด ์„ ์–ธ

๐Ÿ“Œ ์ธ๋ฑ์Šค(index)๋ฐฐ์—ด์š”์†Œ ์ ‘๊ทผ

  • ์ธ๋ฑ์Šค์˜ ์‹œ์ž‘์€ 0์œผ๋กœ ์‹œ์ž‘ํ•จ!

    int[] arr = {10, 20, 30, 40, 50};
    System.out.println("๋ฐฐ์—ด์˜ 1๋ฒˆ์งธ ์š”์†Œ = " + arr[0]);
    System.out.println("๋ฐฐ์—ด์˜ 2๋ฒˆ์งธ ์š”์†Œ = " + arr[1]);
    System.out.println("๋ฐฐ์—ด์˜ 3๋ฒˆ์งธ ์š”์†Œ = " + arr[2]);
    System.out.println("๋ฐฐ์—ด์˜ 4๋ฒˆ์งธ ์š”์†Œ = " + arr[3]);
    System.out.println("๋ฐฐ์—ด์˜ 5๋ฒˆ์งธ ์š”์†Œ = " + arr[4]);

โœ๏ธ ์ธ๋ฑ์Šค(index)ํ™œ์šฉํ•ด ๊ฐ’์„ ์‚ฝ์ž… ์ˆ˜์ •
int[] arr = new int[5];
arr[0] = 100; // โœ… 1๋ฒˆ์งธ ์š”์†Œ๋ฅผ 100๋กœ ์„ค์ •
arr[1] = 200; // โœ… 2๋ฒˆ์งธ ์š”์†Œ๋ฅผ 200๋กœ ์„ค์ •
arr[2] = 300; // โœ… 3๋ฒˆ์งธ ์š”์†Œ๋ฅผ 300๋กœ ์„ค์ •
arr[3] = 400; // โœ… 4๋ฒˆ์งธ ์š”์†Œ๋ฅผ 400๋กœ ์„ค์ •
arr[4] = 500; // โœ… 5๋ฒˆ์งธ ์š”์†Œ๋ฅผ 500๋กœ ์„ค์ •
arr[5] = 600; // โŒ ์ ‘๊ทผ ๋ถˆ๊ฐ€

System.out.println("๋ฐฐ์—ด์˜ 1๋ฒˆ์งธ ์š”์†Œ๋Š” 0๋ฒˆ์งธ ์ธ๋ฑ์Šค: " + arr[0]); // 100
System.out.println("๋ฐฐ์—ด์˜ 2๋ฒˆ์งธ ์š”์†Œ๋Š” 1๋ฒˆ์งธ ์ธ๋ฑ์Šค: " + arr[1]); // 200
System.out.println("๋ฐฐ์—ด์˜ 3๋ฒˆ์งธ ์š”์†Œ๋Š” 2๋ฒˆ์งธ ์ธ๋ฑ์Šค: " + arr[2]); // 300
System.out.println("๋ฐฐ์—ด์˜ 4๋ฒˆ์งธ ์š”์†Œ๋Š” 3๋ฒˆ์งธ ์ธ๋ฑ์Šค: " + arr[3]); // 400
System.out.println("๋ฐฐ์—ด์˜ 5๋ฒˆ์งธ ์š”์†Œ๋Š” 4๋ฒˆ์งธ ์ธ๋ฑ์Šค: " + arr[4]); // 500

๐Ÿ”Ž ๋ฐฐ์—ดํƒ์ƒ‰

๋ณ€์ˆ˜๋ฅผ ์ธ๋ฑ์Šค๋กœ ํ™œ์šฉํ•˜๊ธฐ

int[] arr = {1, 2, 3, 4, 5};

for (int i = 0; i < arr.length; i++) {  โญ๏ธ ์ˆซ์ž๋Œ€์‹  i ํ™œ์šฉ
    System.out.println("numbers[" + i + "] = " + arr[i]); 
}

for (int i = 0; i < arr.length; i++) {
            System.out.println("์ธ๋ฑ์Šค:"+ i+",๊ฐ’:"+ arr[i]); }

๐Ÿ“Œํ–ฅ์ƒ๋œ for๋ฌธ

  • ๋ฐฐ์—ด์˜ ๋ชจ๋“ ์š”์†Œ๋ฅผ ๊บผ๋‚ด์„œ ํ™œ์šฉํ• ๋•Œ ํŽธ๋ฆฌํ•œ ๋ฐ˜๋ณต๋ฌธ
  • ์ธ๋ฑ์Šค์—์„œ ์ง์ ‘ ํ™œ์šฉ ํ•˜์ง€๋ชปํ•จ
  • for (int a : arr) โ†’ ๋ฐฐ์—ด arr ์—์„œ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ a ์— ์ €์žฅ
  • IndexOutOfBoundsException ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ์—†์Œ

    for(๋ฐฐ์—ด์˜์š”์†Œ : ๋ฐฐ์—ด) {
    ๋ช…๋ น๋ฌธ;
    }
    int[] arr = {100, 200, 300, 400, 500};
    for (int a : arr) {
    System.out.println("๊ฐ’ = " + a);
    }

๐Ÿ“š 2์ฐจ์›๋ฐฐ์—ด

  • ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋Š” ํ–‰ = ๊ฐ€๋กœ ๋ฐฉํ–ฅ
  • ๋‘ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋Š” ์—ด = ์„ธ๋กœ ๋ฐฉํ–ฅ

boolean[][] board = new boolean[2][2]; 2์ฐจ์› ๋ฐฐ์—ด ์„ ์–ธ

boolean[][] board = {
		{true, false},  0ํ–‰์˜ 0์—ด, 0ํ–‰์˜ 1์—ด,
		{false, true}   1ํ–‰์˜ 0์—ด, 1ํ–‰์˜ 1์—ด
};

๐Ÿ“Œ๐Ÿค– ๋ฉ”์„œ๋“œ-๋ชจ๋“ˆํ™”์˜์‹œ์ž‘

  • ๋งค์„œ๋“œ์˜ ํ•จ์ˆ˜/๊ธฐ๋Šฅ์€ ํ˜ธ์ถœ๋ถ€ ์„ ์–ธ๋กœ ๋‚˜๋ˆ„์–ด์ง
  • ํ˜ธ์ถœ๋ถ€๋Š” ๋งค์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ณณ, ๋งค๊ฐœ๋ณ€์ˆ˜ ํ™œ์šฉ ๋งค์„œ๋“œ ์—ฐ์‚ฐ ํ•„์š” ๋ฐ์ดํ„ฐ ์ „๋‹ฌํ•จ
  • ์„ ์–ธ๋ถ€ ๋งค์„œ๋“œ ์ •์˜ ๋˜์–ด์žˆ์Œ

๐Ÿ’ก ์„ ์–ธ๋ถ€ ๋ฉ”์„œ๋“œ๊ฐ€ ์ •์˜๋˜์–ด์žˆ๋Š”๊ณณ

  • ๋ฉ”์„œ๋“œ ์ด๋ฆ„์€ ์นด๋ฉœ์ผ€์ด์Šค๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ ๊ธฐ๋Šฅ์„ ์•Œ์ˆ˜์žˆ๋„๋ก ์ฃผ๋กœ ๋™์‚ฌ๋กœ ์‹œ์ž‘
    ex) sum(), printMessage(), findUserById()
  • ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ• ๋•Œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š” ๋ณ€์ˆ˜์˜ ๊ณต๊ฐ„, ์—ฌ๋Ÿฌ๊ฐœ ์‚ฌ์šฉ๊ฐ€๋Šฅ(์‰ผํ‘œ๋กœ๊ตฌ๋ถ„)
  • ์ž‘์—…๋‚ด์šฉ ๋ช…๋ น๋ฌธ์„ {} ์ค‘๊ด„ํ˜ธ ์•ˆ์— ์ž‘์„ฑ๋˜๊ณ  ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์—ฌ๊ธฐ์— ์ž‘์„ฑ๋œ ์ฝ”๋“œ๊ฐ€ ์ฒซ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ ์‹คํ–‰๋จ
  • 4๋ฒˆ ์ž‘์—…ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•  ๊ฒฝ์šฐ return ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉ
  • ๋ฐ˜ํ™˜๊ฐ’์€ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ ๊ณณ์œผ๋กœ ์ „๋‹ฌ๋˜๊ณ  ํ™œ์šฉํ• ์ˆ˜์žˆ์Œ
  • ๋ฐ˜ํ™˜๋œ ์ž๋ฃŒ๋Š” ๋ฉ”์„œ๋“œ ์„ ์–ธ๋ถ€์— ๋ช…์‹œ(๋ฉ”์ธ?)
  • ๋ฐ˜ํ™˜๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ โ†’ ๋ฐ˜ํ™˜๋˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์ž๋ฃŒํ˜•(int, String, double ๋“ฑ)์„ ์ž‘์„ฑ
    ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ โ†’ void๋ฅผ ์ž‘์„ฑ
public class Calculator {

		int sum(int value1, value2) { // โœ… ๋ฉ”์„œ๋“œ ์„ ์–ธ๋œ ๊ณณ
				int result = value1 + value2;
				return result;
		}
}

๐Ÿ’ก ํ˜ธ์ถœ๋ถ€ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๊ณณ

  • ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๊ณ ์žˆ๋Š”๊ณณ์„ ์˜๋ฏธํ•จ
    public class Main {
    public static void main(String[] args {
    Calculator calculator = new Calculator(); // โœ… Calculator ๊ฐ์ฒด ์†Œํ™˜
    int result = calculator.sum(1, 2); // โœ… ํ˜ธ์ถœ๋ถ€
    System.out.println("๊ฒฐ๊ณผ: " + result);
    }
    }

๐Ÿ™…โ€โ™€๏ธ ๋ฐ˜ํ™˜๊ฐ’์ด ์—†์„๋•Œ void

  • void๋Š” ๋น„์–ด์žˆ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ์–ด ๋ฐ˜ํ™˜์ž๋ฃŒํ˜• ์œ„์น˜์— void ๋ฅผ ์„ ์–ธํ•ด ์ฃผ๋ฉด๋จ
public class Calculator {
		void sum(int value1, value2) {  โœ… ๋ฉ”์„œ๋“œํŒŒ์ผ void ์‹œ์ž‘ 
		int result = value1 + value2;
		System.out.println("๊ณ„์‚ฐ๊ฒฐ๊ณผ๋Š” " + result + "์ž…๋‹ˆ๋‹ค.");
		}
}

public class Main {
public static void main(String[] args {
Calculator calculator = new Calculator();
calculator.sum(1, 2); โœ… ๋ฐ˜ํ™˜๋ฐ์ดํ„ฐ ์—†์Œ
		}
}

๐Ÿ™†โ€โ™€๏ธ ๋ฐ˜ํ™˜๊ฐ’์ด ์žˆ์„๋•Œ return

  • return ํ‚ค์›Œ๋“œ๋ฅผ ํ™œ์šฉํ•ด ๋ฐ˜ํ™˜๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œํ˜„ํ•จ
  • ๋ฐ˜ํ™˜๋œ ๋ฐ์ดํ„ฐ๋Š” ํ˜ธ์ถœ๋ถ€์—์„œ ๋ฐ›์•„์„œ ํ™œ์šฉ
public class Calculator {

		int sum(int value1, value2) {
				int result = value1 + value2;
				return result; // โœ… result ๊ฐ€ ๋ฐ˜ํ™˜
		}
}
public class Main {

		public static void main(String[] args {
				Calculator calculator = new Calculator();
				int result = calculator.sum(1, 2); // โœ… ๋ฐ˜ํ™˜๋œ ๋ฐ์ดํ„ฐ๋ฅผ result ๋ณ€์ˆ˜์— ๋‹ด์•„ ์‚ฌ์šฉ
				System.out.println("๊ณ„์‚ฐ๊ฒฐ๊ณผ๋Š” " + result + "์ž…๋‹ˆ๋‹ค.");
		}
}
profile
์šฐ๋‹นํƒ•๊ฐœ๋ฐœ์ผ์ง€

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด