Java TIL(2)

YulHee Kimยท2021๋…„ 8์›” 3์ผ
0

Java

๋ชฉ๋ก ๋ณด๊ธฐ
2/8
post-thumbnail
post-custom-banner

๐Ÿ“š ํ•˜๋ฃจ์— ํ•œ๋ฒˆ์”ฉ ๋ฐฐ์šด ๊ฒƒ๊ณผ ์ง€์‹์„ ์ •๋ฆฌํ•˜๋ ค๊ณ  ์“ฐ๋Š” ๊ธ€

๐Ÿ“— Java ๊ธฐ์ดˆ๋ฌธ๋ฒ•

๐Ÿ“Œ ๋ฐฐ์—ด์ด๋ž€?

์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด์„œ ์ž๋ฃŒํ˜•์ด ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

// ๋ฐฐ์—ด ์„ ์–ธ ํ›„ ์ดˆ๊ธฐํ™”
int[] arr1 = new int[5];
arr1[0] = 100;
arr1[1] = 200;
arr1[2] = 300;
arr1[3] = 400;
arr1[4] = 500;

// ๋ฐฐ์—ด ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™”๋ฅผ ๋™์‹œ์—
int[] arr2 = {10, 20, 30, 40, 50};

๐Ÿ“Œ ๋ฐฐ์—ด์˜ ๊ธฐ๋ณธ ์†์„ฑ

๊ธฐ๋ณธ ์ž๋ฃŒํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜์™€ ๋‹ฌ๋ฆฌ ๋ฐฐ์—ด ๋ณ€์ˆ˜๋Š” ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ์˜ ์ฃผ์†Œ๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.

// ๋ฐฐ์—ด ๊ธฐ๋ณธ์†์„ฑ
int[] arrAtt1 = {10, 20, 30, 40, 50, 60};
int[] arrAtt2 = null;
int[] arrAtt3 = null;

// ๋ฐฐ์—ด ๊ธธ์ด
System.out.println("arrAtt1.length : " + arrAtt1.length);

// ๋ฐฐ์—ด ์š”์†Œ ์ถœ๋ ฅ
System.out.println("arrAtt1 : " + Arrays.toString(arrAtt1));

// ๋ฐฐ์—ด ์š”์†Œ ๋ณต์‚ฌ
arrAtt3 = Arrays.copyOf(arrAtt1, arrAtt1.length);
System.out.println("arrAtt3 : " + Arrays.toString(arrAtt3);

// ๋ฐฐ์—ด ๋ ˆํผ๋Ÿฐ์Šค
arrAtt2 = arrAtt1;
System.out.println("arrAtt1 : " + arrAtt1);
System.out.println("arrAtt2 : " + arrAtt2);
System.out.println("arrAtt3 : " + arrAtt3);

/* ๊ฒฐ๊ณผ
arrAtt1.length : 6
arrAtt1 : [10, 20, 30, 40, 50, 60]
arrAtt3 : [10, 20, 30, 40, 50, 60]
arrAtt1 : [I@4554617c
arrAtt2 : [I@4554617c
arrAtt3 : [I@74a14582
*/

๐Ÿ’ก ๋ฐฐ์—ด1๊ณผ ๋ฐฐ์—ด3์˜ ์ฃผ์†Œ๋Š” ์š”์†Œ์˜ ๊ฐ’๋งŒ ๋ณต์‚ฌํ–ˆ์„ ๋ฟ์ด๋ฏ€๋กœ ๊ฐ์ž ๋‹ค๋ฅด๋‹ค. ๋ฐฐ์—ด1๊ณผ ๋ฐฐ์—ด2๊ฐ€ ๋‹ด์€ ์ฃผ์†Œ๋Š” ๊ฐ™์€ ์ฃผ์†Œ๋‹ค. ๊ฐ™์€ ๊ณณ์„ ๊ฐ€๋ฅดํ‚ค๊ณ  ์žˆ์Œ.

๐Ÿ“Œ ๋‹ค์ฐจ์› ๋ฐฐ์—ด

๋ฐฐ์—ด ์•ˆ์— ๋˜ ๋‹ค๋ฅธ ๋ฐฐ์—ด์ด ์กด์žฌํ•œ๋‹ค.

// ๋‹ค์ฐจ์› ๋ฐฐ์—ด
int[][] arrMul = new int[3][2];
arrMul[0][0] = 10;
arrMul[0][1] = 100;
arrMul[1][0] = 20;
arrMul[1][1] = 200;
arrMul[2][0] = 30;
arrMul[2][1] = 300;

// ์ถœ๋ ฅ ํ›„ ๊ฒฐ๊ณผ
[10, 100]
[20, 200]
[30, 300]

๐Ÿ“Œ ์กฐ๊ฑด๋ฌธ - switch ๋ฌธ

๋น„๊ต๋Œ€์ƒ์ด ๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ’๊ณผ ์„ ํƒ์‚ฌํ•ญ์ด ๋งŽ์„ ๊ฒฝ์šฐ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

// switch๋ฌธ
System.out.print("์ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”. : ")ใ…ฃ
Scanner inputNum = new Scanner(System.in);
int score = inputNum.nextInt();

switch (score) {
case 100:
case 90:
System.out.println("์ˆ˜");
break;

case 80:
System.out.println("์šฐ");
break;

case 70:
System.out.println("๋ฏธ");
break;

default:
System.out.println("try again!!");
break;
}

inputNum.close();

๐Ÿ’กScanner(System.in) ์ž…๋ ฅ๋ฐ›์€ ๋’ค์— close()ํ•ด์ฃผ์–ด์•ผ ํ•จ


๐Ÿ“— Java ๊ฐ์ฒด

๐Ÿ“Œ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ž€?

- ๊ฐ์ฒด๋ž€ ?
์„ธ์ƒ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๊ฒƒ์„ ๋œปํ•˜๋ฉฐ, ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์†์„ฑ๊ณผ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๋Š” ํ”„๋กœ๊ทธ๋žจ ๋‹จ์œ„์ด๋‹ค.
- ํด๋ž˜์Šค๋ž€ ?
๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ํˆด๋กœ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋œ๋‹ค.
- ํด๋ž˜์Šค ๊ตฌ์„ฑ์š”์†Œ
ํด๋ž˜์Šค๋Š” ์†์„ฑ(๋ฉค๋ฒ„ ๋ณ€์ˆ˜)์™€ ๊ธฐ๋Šฅ(๋ฉ”์„œ๋“œ)๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
ex) ์ž์ „๊ฑฐ
์†์„ฑ(๋ฉค๋ฒ„ ๋ณ€์ˆ˜) : ์•ˆ์žฅ, ํ•ธ๋“ค, ๋ฐ”๊ตฌ๋‹ˆ, ๊ธฐ์–ด, ํŽ˜๋‹ฌ, ๋ฐ”ํ€ด
๊ธฐ๋Šฅ(๋ฉ”์„œ๋“œ) : ๊ธฐ์–ด ๋ณ€์†, ๊ฐ€์†, ๋ธŒ๋ ˆ์ดํฌ

๐Ÿ“Œ ํด๋ž˜์Šค ์ œ์ž‘๊ณผ ๊ฐ์ฒด ์ƒ์„ฑ

๐Ÿ“Ž ํด๋ž˜์Šค๋Š” ๋ฉค๋ฒ„ ๋ณ€์ˆ˜(์†์„ฑ), ๋ฉ”์„œ๋“œ(๊ธฐ๋Šฅ), ์ƒ์„ฑ์ž ๋“ฑ์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.

public class Grandeur {
//ํด๋ž˜์Šค ์ด๋ฆ„ : ์ผ๋ฐ˜์ ์œผ๋กœ ์ฒซ ๊ธ€์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ํ•œ๋‹ค.
    public String color;
    public String gear;
    public int price;
    // ๋ฉค๋ฒ„ ๋ณ€์ˆ˜(์†์„ฑ)
    public Grandeur() {
    	System.out.println("Grandeur constructor");
    }
    public Grandeur(String c, int p) {
    	System.out.println("Grandeur constructor");
        color = c;
        price = p;
    }
    // ์ƒ์„ฑ์ž : ํด๋ž˜์Šค์—์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ๋œ๋‹ค.
    public String run() {
    	System.out.println("--run--");
        return "Hello";
    }
    // ๋ฉ”์„œ๋“œ(๊ธฐ๋Šฅ), ๋ฐ˜ํ™˜๊ฐ’ Stirng
    public void stop() {
    	System.out.println("--stop--");
    }
    // ๋ฉ”์„œ๋“œ(๊ธฐ๋Šฅ)
}

๐Ÿ“Ž ๊ฐ์ฒด ์ƒ์„ฑ : ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ 'new'๋ฅผ ์ด์šฉํ•ด ๊ฐ์ฒด ์ƒ์„ฑ

Grandeur myCar1 = new Grandeur();
myCar1.color = "red";
myCar1.gear = "auto";
myCar1.price = 30000000;

myCar1.run();
myCar1.stop();
myCar1.info();

๐Ÿ“Œ ๋ฉ”์„œ๋“œ

์˜ค๋ฒ„๋กœ๋”ฉ (์ค‘๋ณต ๋ฉ”์„œ๋“œ)
์ด๋ฆ„์€ ๊ฐ™๊ณ , ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜ ๋˜๋Š” ํƒ€์ž…์ด ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

์ ‘๊ทผ์ž
private : ์™ธ๋ถ€์—์„  ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ  ๊ฐ์ฒด ๋‚ด์—์„  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
public : ์™ธ๋ถ€์—์„œ๋„ ํ˜ธ์ถœ ๊ฐ€๋Šฅ.

profile
๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž
post-custom-banner

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