JiwonDev

#6. Java - 2 ํด๋ž˜์Šค (์ž‘์„ฑ์˜ˆ์ •)

by JiwonDev

Java - 1 ์š”์•ฝ

2021.07.01 - [Backend/Java] - #3. Java - 1

๋”๋ณด๊ธฐ
1. ์ž๋ฐ”์˜ ๋ชจ๋“  ๊ฒƒ์€ ํด๋ž˜์Šค์ด๋‹ค. ์ฆ‰ ๊ธฐ๋ณธ์ž๋ฃŒํ˜•์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฒƒ์ด ์ฐธ์กฐํ˜•(Reference)์ด๋‹ค.
2. ์ž๋ฐ”์˜ ์ž๋ฃŒํ˜•์€ ๋ชจ๋“  ์šด์˜์ฒด์ œ์— ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค. (๋…๋ฆฝ์„ฑ ์œ ์ง€)
3. ์ž๋ฐ” ๋„ค์ด๋ฐ ์ปจ๋ฒค์…˜ 2021.06.30 - [Backend/Java] - # 2. Java Naming Conventions

4. ์‹ค์ˆ˜ ๋ฆฌํ„ฐ๋„์—๋Š” ์ž๋ฃŒํ˜• ํ‘œ๊ธฐ๋ฅผ ํ•ด์ฃผ์ž. (100L, 2.4F, 5.6D)
5. String์€ ๊ฐ์ฒด๋กœ ์ƒ์„ฑ(new)ํ•˜๊ฑฐ๋‚˜ ๋ฆฌํ„ฐ๋„๋กœ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฆฌํ„ฐ๋„๋กœ ํ•˜๋ฉด ์ƒ์ˆ˜ ํ’€์— ์ €์žฅ๋œ๋‹ค.
6. == ์—ฐ์‚ฐ์€ ๊ฐ์ฒด์˜ ์ฃผ์†Œ๋ฅผ ๋น„๊ตํ•œ๋‹ค. ๋ฆฌํ„ฐ๋„์€ ์ ์šฉ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, String์€ ์•ˆ๋œ๋‹ค. str.equal()์„ ์‚ฌ์šฉํ•˜์ž.

7. ์ž๋ฐ”์—๋„ ๊ธฐ๋ณธ Array ๊ฐ€ ์กด์žฌํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋ณดํ†ต ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ(util.ArrayList)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ฐธ๊ณ ๋กœ ์ž๋ฐ”์˜ ๊ธฐ๋ณธ๋ฉ”์„œ๋“œ๋Š” java.base ์— ์žˆ๊ณ (์ž๋™ import), ์ปฌ๋ ‰์…˜์€ java.util ์— ์žˆ๋‹ค.(์ง์ ‘ import ํ•„์š”)
// ์ด 2๊ฐœ๋Š” ๊ฐ™์€ ์ฝ”๋“œ์ด๋‹ค. ๋‹ค๋งŒ ํƒ€์ž…์„ ๊ตฌ๋ถ„ ์ง“๋Š” int[] a1 ๊ฐ€ ์ผ๋ฐ˜์ ์ด๋‹ค.
int[] a1; int a2[];

int[] intArr = {1,2,3};
int[] doubleArr = new double[4];
String[] objArr = new String[5]; //object type

int[] myIntArray = new int[3]; // {0,0,0}์ด ํ• ๋‹น๋œ๋‹ค.
int[] myIntArray = {1, 2, 3}; // ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ์ด๋‹ค. ์ž๋ฐ”์˜ ์ดˆ๊ธฐํ™” ํŽธ์˜๊ธฐ๋Šฅ.
int[] myIntArray = new int[]{1, 2, 3}; // new int[]๋กœ ํƒ€์ž…์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚ด์ฃผ์—ˆ๋‹ค.

// ํ•ด๋‹น ์ฝ”๋“œ๋Š” ์ปดํŒŒ์ผ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. ์ƒ์„ฑํ•  ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค.
int badArray[] = new int[]; 

// ํ•ด๋‹น ์ฝ”๋“œ๋Š” ์ปดํŒŒ์ผ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. 
// ์•ž์— ์ ํ˜€์žˆ๋Š” new int[]๋Š” ์ž๋ฃŒํ˜•์„ ์ง€์ •ํ•œ ๊ฒƒ์ด์ง€, ๊ฐ์ฒด์˜ ํฌ๊ธฐ๋ฅผ ์ ๋Š” ๊ณณ์ด ์•„๋‹ˆ๋‹ค.
int badArray[] = new int[3] {1, 2, 3} ; 

// Since Java 8. Doc of IntStream: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html
int [] myIntArray = IntStream.range(0, 100).toArray(); // From 0 to 99
int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 100
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).toArray(); // The order is preserved.
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).sorted().toArray(); // Sort
 
// Array๋„ ๊ฐ์ฒด์ด๊ธฐ์— ๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค.
int arr[] = new int[3];
for (int i = 0; i < arr.length; i++) { ... } // length ๋กœ ์ ‘๊ทผ
for (int a : arr) { ... } // iter๋กœ ์ ‘๊ทผ

 

8. if ์กฐ๊ฑด๋ฌธ์—์„œ ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž (&& ||)๋Š” ์ตœ์ ํ™” ๋œ๋‹ค. ์•ž์—์„œ ๊ฒฐ์ •์ด ๋‚ฌ๋‹ค๋ฉด ๋’ค์˜ ๊ฐ’์€ ํ‰๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.
9. ๊ธฐ์กด์˜ switch๋ฌธ์€ break๊ฐ€ ํ•„์š”ํ–ˆ๋‹ค. 2020๋…„ java14์—์„œ ๋งคํ„ด๋งค์นญ + enum ๋Š๋‚Œ์˜ switch๋ฌธ์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

        // isTrue() ๋ฉ”์„œ๋“œ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋ฏธ ์•ž์—์„œ false๋กœ ๋‹ต์ด ๊ฒฐ์ •๋‚ฌ๊ธฐ ๋•Œ๋ฌธ.
        if (isFalse() && isTrue()){
            System.out.println("it doesn't print(false)");
        }        
        
        // ์—ด๊ฑฐํ˜•์„ ์‚ฌ์šฉํ•˜๋ฉด default ์—†์ด switch๋ฌธ์„ ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.
        enum Month { January, February, March, April }
        Month value = Month.January;
        monthString = switch (value) {
            case January -> "January";
            case February -> "February";
            case March -> "March";
            case April -> "4,5์›”";
        };

 

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ