JiwonDev

#3. Java - 1 ๊ธฐ์ดˆ๋ฌธ๋ฒ•

by JiwonDev

# IntelliJ

introduce value, reformat file, line duplicate, line delete

javadocs plugins

# ๋ชจ๋˜ ์ž๋ฐ”, Java 8~

2014๋…„, Java8 (1.8)์ด ๋‚˜์˜ค๊ธฐ ์ „๊นŒ์ง€๋Š” ์ž๋ฐ”๋Š” ์—…๋ฐ์ดํŠธ๋ฅผ ์ž์ฃผํ•˜์ง€ ์•Š์•˜๋‹ค. ํ•˜๋”๋ผ๋„ ํฌ๊ฒŒ ์ƒˆ๋กœ์šด ๊ฐœ๋…๋„ ์—†์—ˆ๋‹ค. ํ•˜์ง€๋งŒ Java 8 ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ชจ๋˜ํ•œ ์–ธ์–ด๋“ค์˜ ๊ธฐ๋Šฅ๋“ค์ด ๋งŽ์ด ์ถ”๊ฐ€๋˜์—ˆ๋Š”๋ฐ ์ด ๊ธฐ์ ์„ ์‹œ์ž‘์œผ๋กœ ๋งค๋…„ 3์›”, 9์›”๋งˆ๋‹ค ๊พธ์ค€ํžˆ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•ด์ฃผ๊ณ  ์žˆ๋‹ค.

  • ๋žŒ๋‹ค ํ‘œํ˜„์‹
  • ๋ฉ”์†Œ๋“œ ๋ ˆํผ๋Ÿฐ์Šค
  • ์ŠคํŠธ๋ฆผ API
  • Optional

์ง€๊ธˆ ํ˜„์—…์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋˜ ์ž๋ฐ”๋Š” Java 8์—์„œ ๋‚˜์˜จ ์ƒˆ๋กœ์šด ๊ฐœ๋…์„ ํฌํ•จํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. ์ฐธ๊ณ ๋กœ ์ง€๊ธˆ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” LTS(Long-Term-Service)๋ฒ„์ „์€ Java11์ด๊ณ  ํ˜„์žฌ Java17๋ฒ„์ „์„ LTS๋กœ ์—…๋ฐ์ดํŠธ ํ•  ์˜ˆ์ •์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

ํ•ด๋‹น ๊ธ€์—์„œ๋Š” ์ž๋ฐ”์˜ ๊ธฐ๋ณธ๊ธฐ๋ฅผ ๋‹ค๋ฃจ๊ณ , ๋ชจ๋˜ Java์˜ ๊ธฐ๋Šฅ๋“ค์€ ๋‹ค์Œ ๊ธ€์— ๋”ฐ๋กœ ๋‹ค๋ฃจ๊ฒ ๋‹ค.


# Hello World

์ž๋ฐ”์—์„œ๋Š” ๋ชจ๋“  ๊ฒƒ์ด ํด๋ž˜์Šค์ด๋‹ค. ํ•˜๋‚˜์˜ ์ž๋ฐ” App์—์„œ๋Š” ์ตœ์†Œ ํ•˜๋‚˜์˜ Main Method ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

์ฐธ๊ณ ๋กœ ๋‚˜์ค‘์— ๋ฐฐ์šธ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ ๋“ฑ์—์„œ๋Š” Main Method ๊ฐ€ ์‹œ์ž‘์ง€์ ์ด ์•„๋‹Œ๋ฐ ์ด๋Š” ๋‚˜์ค‘์— ๋ฐฐ์šฐ๋„๋ก ํ•˜์ž.

package com.company;

public class Main {

    public static void main(String[] args) {
	// write your code here
        System.out.println("์•ˆ๋…•ํ•˜์„ธ์š”");
    }
}

์ฐธ๊ณ ๋กœ ์ž๋ฐ” ๊ธฐ๋ณธ ํŒจํ‚ค์ง€ (java.lang)๋Š” ๊ธฐ๋ณธ์œผ๋กœ import ๋œ๋‹ค. ๊ทธ๋ž˜์„œ String, System ๊ณผ ๊ฐ™์€ ํด๋ž˜์Šค๋“ค์€ ๋”ฐ๋กœ import ๋ฌธ์„ ์ ์ง€ ์•Š์•„๋„ ๋ฐ”๋กœ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค. 


# ๋ฐ์ดํ„ฐ ํƒ€์ž…

# ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•(Primitive)

๋ณ€์ˆ˜ ์ด๋ฆ„ ์ง“๋Š” ๋ฐฉ๋ฒ•์€ ์ž๋ฐ” ๋„ค์ด๋ฐ ์ปจ๋ฒค์…˜์„ ํ•œ๋ฒˆ ์ฐธ๊ณ ํ•˜์ž. ์ž๋ฃŒํ˜• ๋ฉ”๋ชจ๋ฆฌํฌ๊ธฐ ํ‘œํ˜„ ๊ฐ€๋Šฅ ๋ฒ”์œ„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ฐธ๊ณ ๋กœ ๋ชจ๋“  ์šด์˜์ฒด์ œ์— ๋™์ผํ•˜๊ฒŒ ์ ์šฉ๋œ๋‹ค.

์ž๋ฃŒํ˜• ํฌ๊ธฐ (๊ณ ์ •) ๋ฒ”์œ„
byte 1 Byte -128 ~ 127
short 2 Byte -32,768 ~ 32,767
int 4 Byte ์ •์ˆ˜ (-2,147,483,648 ~ 2,147,483,647 )
long 8 Byte ํฐ ์ •์ˆ˜ (-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807)
char 2 Byte ์œ ๋‹ˆ์ฝ”๋“œ (#u0000~ #uFFFF)
float 4 Byte ์‹ค์ˆ˜ (1.4E-45 ~ 3.4028235E38)
double 8 Byte ์‹ค์ˆ˜ (4.9E-324 ~ 1.7976931348623157E308)
boolean 1 Byte ๋…ผ๋ฆฌ (true, false)

์ฐธ๊ณ ๋กœ float ๊ณผ double์€ ๋ถ€๋™์†Œ์ˆ˜์  ๋ฐฉ์‹์ด๋ผ 0์„ ์ •ํ™•ํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š”๊ฑธ ๊ธฐ์–ตํ•˜์ž.

float 4 Byte ๋ถ€ํ˜ธ(1bit) + ์ง€์ˆ˜(8bit) + ๊ฐ€์ˆ˜(23bit) = 32 bit = 4 Byte
double 8 Byte ๋ถ€ํ˜ธ(1bit) + ์ง€์ˆ˜(11bit) + ๊ฐ€์ˆ˜(52bit) = 64 bit = 8 Byte

// ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ ์˜๋ฏธ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ง“์ž.
int accountDoumentCount = 10;
int humonResourceDoumentCount = 20;

for (int i = 0; i < 10; i++){
// ๋ฌผ๋ก  ์ž ๊น ์‚ฌ์šฉํ•˜๋Š” ์ง€์—ญ๋ณ€์ˆ˜(i,j,k)๊นŒ์ง€ ์˜๋ฏธ๋ฅผ ๋ถ€์—ฌ ํ•  ํ•„์š”๋Š” ์—†๋‹ค.
}


#์ƒ์ˆ˜์™€ ๋ฆฌํ„ฐ๋„(literal)

์ƒ์ˆ˜์™€ ๋ฆฌํ„ฐ๋„์„ ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ƒ์ˆ˜๋Š” '์ดˆ๊ธฐํ™” ์ดํ›„ ๊ฐ’์ด ๋ณ€ํ•˜์ง€ ์•Š๋Š” ์ˆ˜' ์ด๋‹ค. ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ ํ•  ๋•Œ final ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ƒ์ˆ˜๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

final int DAY_OF_WEEK = 7; // ์ฐธ๊ณ ) ๊ฐ€๋Šฅํ•˜๋ฉด ์ƒ์ˆ˜๊ฐ’์€ get ๋ฉ”์„œ๋“œ๋กœ ์ œ๊ณตํ•˜๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค.

๋ฆฌํ„ฐ๋„์€ ๋ง ๊ทธ๋Œ€๋กœ ์„ ์–ธ์—†์ด ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”, ๋ฌธ์ž ๊ทธ๋Œ€๋กœ์˜(=๋ฆฌํ„ฐ๋„ํ•œ) ์ƒ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค. immediate value ๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

3
"HEllO"
3.213
'H'

 

๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ์ƒ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ’๋“ค๋„ ๊ณ ์œ ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค.

์ฐธ๊ณ ๋กœ ๋ฆฌํ„ฐ๋„ ๋’ค์— (L, F, D)๊ฐ™์€ ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ ๋ช…์‹œ์ ์œผ๋กœ ํ•ด๋‹น ๋ฆฌํ„ฐ๋„์˜ ํƒ€์ž…์„ ์ง€์ • ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

object.setNumber(3) // ๋ฆฌํ„ฐ๋„ '3'์˜ ์ž๋ฃŒํ˜•์€ int์ด๋‹ค.
object.setName("Hello") // ๋ฆฌํ„ฐ๋„ "Hello"์˜ ํด๋ž˜์Šค๋Š” String์ด๋‹ค.

object.setNumber(3.0) // ๋ฆฌํ„ฐ๋„ '3.0'์˜ ์ž๋ฃŒํ˜•์€ int..? float..? double..?
object.setNumber(3.0F) // ์ด๋ ‡๊ฒŒ ๋ช…์‹œ์ ์œผ๋กœ Floatํ˜• ์ด๋ผ๊ณ  ์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.


int a = 1 // ์†Œ์ˆ˜์ ์ด ์—†์œผ๋ฉด intํ˜• ๋ฆฌํ„ฐ๋„์ด๋‹ค. ๋”ฐ๋กœ ๋ฆฌํ„ฐ๋„ ํ‘œ์‹œ๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
boolean b = true // boolean์€ ๊ฐ’์ด true, false 2๊ฐœ๋ฐ–์— ์—†์–ด ๋”ฐ๋กœ ๋ฆฌํ„ฐ๋„ ํ‘œ์‹œ๋ฅผ ์•ˆํ•ด๋„ ๋œ๋‹ค.

// ์‹ค์ˆ˜ ๋ฆฌํ„ฐ๋„์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ ๋ช…์‹œ์ ์œผ๋กœ ํ‘œ์‹œํ•ด์ฃผ์ž.
long count = 10000L;
float f1 = 2.3F;
double d1 = 5.6D;

 


# ์ฐธ์กฐํ˜•(Reference)

ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๊ณ  new ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ž๋ฐ”์—์„œ๋Š” ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฒƒ์„ ์ฐธ์กฐํ˜•์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์‰ฝ๊ฒŒ๋งํ•ด ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๊ณ , ์‹ฌ๋ณผ(=์ด๋ฆ„)๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค ์‹คํ–‰ํ•˜๋Š” ์‹œ์ ์— ๋ฉ”๋ชจ๋ฆฌ์— ์ ‘๊ทผํ•œ๋‹ค๋Š” ๋ง.

//ํด๋ž˜์Šค์ด๋ฆ„ ๋ ˆํผ๋Ÿฐ์Šค๋ช… = new ํด๋ž˜์Šค์ด๋ฆ„();
MadPlay instance = new MadPlay();

 


# String ํด๋ž˜์Šค

์ž๋ฐ”์—์„œ๋Š” ๋ฌธ์ž์—ด์ด ํด๋ž˜์Šค๋กœ ์ œ๊ณต๋˜์–ด์ง„๋‹ค. ํ•˜์ง€๋งŒ String์˜ ๊ฒฝ์šฐ ์˜ˆ์™ธ์ ์œผ๋กœ ๊ธฐ๋ณธ์ž๋ฃŒํ˜• ์ฒ˜๋Ÿผ new ํ‚ค์›Œ๋“œ ์—†์ด ์ƒ์„ฑํ•  ์ˆ˜ ๋„ ์žˆ๋Š”๋ฐ, ์ด๋ฅผ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋„ ์ƒ์„ฑ ๋ฐฉ์‹์ด๋ผ๊ณ  ํ•˜๊ณ  new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ๋ฉ”๋ชจ๋ฆฌ์— ์ ์žฌ ์žฅ์†Œ๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค.

// ๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Heap ์˜์—ญ์— ํ• ๋‹น๋œ๋‹ค.
String str1 = new String("madplay");

// ์ŠคํŠธ๋ง ์ƒ์ˆ˜๊ฐ’์œผ๋กœ ๊ด€๋ฆฌ๋œ๋‹ค. String Constant Pool ์ด๋ผ๋Š” ์˜์—ญ์— ์ €์žฅ๋œ๋‹ค.
String str2 = "madplay";

String str3 = "madplay"; // ๊ธฐ์กด "madplay" ์ƒ์ˆ˜ ์žฌํ™œ์šฉ. str 2์ด๋ž‘ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ์ฐธ์กฐ
String str4 = new String("madplay"); // ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ. str1์ด๋ž‘ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ ์ฐธ์กฐ

 

String์ด ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ์ƒ์ˆ˜ ๊ฐ’์ธ ๊ฑด ์•„๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ๊ฐ์ฒด๋„ str.intern( ) ์„ ์ด์šฉํ•ด ์ƒ์ˆ˜๋กœ ๋“ฑ๋ก ํ•  ์ˆ˜์žˆ๋‹ค.

์ฐธ๊ณ ๋กœ String Constant Pool์€ ๊ธฐ์กด์—๋Š” ์ƒ์ˆ˜๊ฐ’ ์ „์šฉ ๋ฉ”๋ชจ๋ฆฌ์— (Perm ์˜์—ญ) ๋”ฐ๋กœ ์ €์žฅ๋˜์–ด์กŒ๋Š”๋ฐ, ๋Ÿฐํƒ€์ž„์ค‘ ๋ฉ”๋ชจ๋ฆฌ ์ฆ๊ฐ€๋กœ ์ธํ•œ OutOfMemory ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์–ด Java6 ์ดํ›„๋กœ๋Š” ๊ฐ์ฒด์™€ ํ•จ๊ป˜ Heap ๋ฉ”๋ชจ๋ฆฌ์˜์—ญ์—์„œ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋‹ค.

 

 


# == ๊ณผ equal

String์€ ๊ฐ’์ด ์•„๋‹Œ ๊ฐ์ฒด์ด๋‹ค. ๊ฐ์ฒด์— ์žˆ์–ด (==) ์—ฐ์‚ฐ์€ ๊ฐ’์ด ์•„๋‹Œ ์ฃผ์†Œ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ์—ฐ์‚ฐ์ด๋‹ค.

๋งŒ์•ฝ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋„๋กœ ์„ ์–ธํ–ˆ๋‹ค๋ฉด ๊ฐ™์€ ์ƒ์ˆ˜ ๊ฐ’ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ธฐ์— ๊ฐ’์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ฐ์ฒด (new String)๋กœ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด ์ •์ƒ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด ๊ฒฝ์šฐ String ๊ฐ์ฒด ๋ฉ”์„œ๋“œ equal์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค.

public class MadPlay {
    public static void main(String[] args) {
        String someLiteral = "kimtaeng";
        String someObject = new String("kimtaeng");
        
        System.out.println(someLiteral.equals(someObject)); // print 'true'
        System.out.println(someLiteral == someObject); // print 'false'
        
        System.out.println("kimtaeng" == someLiteral); // print 'true'
        System.out.println("kimtaeng" == someObject); // print 'false'
    }
}


# Array์™€ java.util.ArrayList

์ž๋ฐ”์—๋„ ๊ธฐ๋ณธ ๋ฐฐ์—ด ๊ฐ์ฒด (Array)๊ฐ€ ์กด์žฌํ•˜๋‚˜, ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ Collection Framework์— ์žˆ๋Š” (ArrayList)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋ฌผ๋ก  ์ž˜ ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š๋”๋ผ๋„ ๋ฐฐ์—ด์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์ •๋„๋Š” ์•Œ์•„๋†“์ž.

// ์•„๋ž˜ 3์ฝ”๋“œ์˜ ๋™์ž‘์€ ๋™์ผํ•˜๋‹ค.
String[] myStringArray = new String[3]; // { "", "", "" } ๋นˆ ์ŠคํŠธ๋ง ์ƒ์„ฑ
String[] myStringArray = new String[]{"a", "b", "c"}; // ์ŠคํŠธ๋ง ์ดˆ๊ธฐํ™”
String[] myStringArray = {"a", "b", "c"}; // ์ž๋ฐ”์—์„œ ์ œ๊ณตํ•˜๋Š” ์ดˆ๊ธฐํ™” ๊ธฐ๋Šฅ.

// = { "a" ... } ๋ผ๊ณ  ์ ์—ˆ๋‹ค๊ณ  ํ•ด์„œ {} ๋ถ€๋ถ„์ด ์ƒ์ˆ˜, ๋ฆฌํ„ฐ๋„์ธ๊ฑด ์•„๋‹ˆ๋‹ค. ์ž๋ฐ”์—์„œ ์ œ๊ณตํ•˜๋Š” ํŽธ์˜๊ธฐ๋Šฅ์ผ ๋ฟ.
String[] myStrings;
myStrings = {"a", "b", "c"}; // ์ปดํŒŒ์ผ ์—๋Ÿฌ! ๋ถˆ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ.
// ์ด 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๋กœ ์ ‘๊ทผ

๋‚˜์ค‘์— ์ปฌ๋ ‰์…˜๊ณผ ์ž๋ฐ” API ํด๋ž˜์Šค๋ฅผ ๋‹ค์‹œ ๋‹ค๋ฃจ๊ฒ ์ง€๋งŒ, ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ์— ์žˆ๋Š” ArrayList๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.

์ฐธ๊ณ ๋กœ java.util ์•ˆ์—๋Š” Arrays, Calendar, Date, StringTokenizer, Random ๋“ฑ ๋งŽ์€ ํด๋ž˜์Šค๋“ค์ด ์กด์žฌํ•œ๋‹ค.

import java.util.ArrayList; // ArrayList๋ฅผ Importํ•ด์˜จ๋‹ค.

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> integers1 = new ArrayList<Integer>(); // ํƒ€์ž… ์ง€์ •
        ArrayList<Integer> integers2 = new ArrayList<>(); // ํƒ€์ž… ์ƒ๋žต ๊ฐ€๋Šฅ
        ArrayList<Integer> integers3 = new ArrayList<>(10); // ์ดˆ๊ธฐ ์šฉ๋Ÿ‰(Capacity) ์„ค์ •
        ArrayList<Integer> integers4 = new ArrayList<>(integers1); // ๋‹ค๋ฅธ Collection๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
        
        // Arrays.asList๋ฅผ ํ†ตํ•ด ์ƒ์ˆ˜๊ฐ’์„ ์ด์šฉํ•œ ์ดˆ๊ธฐํ™”
        // Arrays๋Š” java.util์— ์žˆ๋Š” ์ž๋ฐ” API ๊ฐ์ฒด์ด๋ฉฐ ๋ฐฐ์—ด์„ ์‰ฝ๊ฒŒ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ๋“ค์ด ํฌํ•จ๋˜์–ด์žˆ๋‹ค.
        ArrayList<Integer> integers5 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); 
        
        // ArrayList -> Array
        String[] array = arrayList.toArray(new String[arrayList.size()]);
        
        // Array -> ArrayList, ๋ฌผ๋ก  ๊ทธ๋ƒฅ for ๋…ธ๊ฐ€๋‹ค๋กœ ๋ณ€ํ™˜ํ•ด๋„ ๋œ๋‹ค.
        ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(array));
        
        // ArrayList๋Š” ์„ฑ๋Šฅ์ด ์ตœ์ ํ™”๋˜์–ด์žˆ๊ณ , ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.
        ArrayList<String> colors = new ArrayList<>();
        // add() method
        colors.add("Black");
        colors.add("White");
        colors.add(0, "0๋ฒˆ์‚ฝ์ž…");
        colors.add(2, "2๋ฒˆ์‚ฝ์ž…");
        colors.add("Red");

        // set() method
        colors.set(3, "Blue");
        
        // [0๋ฒˆ์‚ฝ์ž…, Black, 2๋ฒˆ์‚ฝ์ž…, Blue, Red]
        System.out.println(colors);
    }
}

# ์กฐ๊ฑด (if)

if - if else - else

๋…ผ๋ฆฌ์—ฐ์‚ฐ์„ ํ•  ๋•Œ๋Š” ๋‹ค๋ฅธ์–ธ์–ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๋ฏธ ๋‹ต์ด ๊ฒฐ์ •๋‚ฌ์œผ๋ฉด ๋”์ด์ƒ ์กฐ๊ฑด์„ ํ‰๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ž๋ฐ”์—์„œ๋Š” if๋ฌธ์•ˆ์— boolean ํƒ€์ž…์ด ์•„๋‹Œ ๋‹ค๋ฅธํƒ€์ž…์€ ์ปดํŒŒ์ผ ๋˜์ง€ ์•Š๋Š”๋‹ค.

public class Hello {
    public static void main(String[] args) {
        System.out.println("start");
        // isDone() ์€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋ฏธ ์•ž์—์„œ false๋กœ ๋‹ต์ด ๊ฒฐ์ •๋‚ฌ๊ธฐ ๋•Œ๋ฌธ.
        if (isNagive() && isDone()){
            System.out.println("it doesn't print(false)");
        }
        // :: start
        // :: IsNagive
    }

    public static boolean isDone(){
        System.out.println("IsDone");
        return true;
    }

    public static boolean isNagive(){
        System.out.println("IsNagive");
        return false;
    }
}

# ์กฐ๊ฑด(switch)

break; ๊ฐ€ ์—†์œผ๋ฉด ์•„๋ž˜ ์ฝ”๋“œ๋„ ์‹คํ–‰๋จ์„ ์œ ์˜ํ•˜์ž. ์ž๋ฐ”๋„ ์กฐ๊ฑด์œผ๋กœ String ์„ ์ง€์›ํ•œ๋‹ค. 

package Hello;

public class Hello {
    public static void main(String[] args) {
        int month = 8;
        String monthString = "1์›”";
        switch (monthString) {
            case "1์›”":
                monthString = "January";
                break;
            case "2์›”":
                monthString = "February";
                break;
            default:
                monthString = "May";
                break;
        }
        
        System.out.println(monthString);
    }
}

2020๋…„ Java14 ๋ถ€ํ„ฐ Switch๋ฌธ์˜ ๊ฐœ์„ ๋œ ๋ฌธ๋ฒ•์„ ์ง€์›ํ•œ๋‹ค. ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํŒจํ„ด๋งค์นญ์ฒ˜๋Ÿผ, break ์—†์ด ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

public class Hello {
    public static void main(String[] args) {
        // 2020๋…„ java14๋ถ€ํ„ฐ ์ง€์›ํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋ผ ์•„์ง ์ž˜ ์“ฐ์ด์ง€๋Š” ์•Š๋Š”๋‹ค.
        String monthString = "1์›”";
        monthString = switch (monthString) {
            case "1์›”" -> "January";
            case "2์›”" -> "February";
            case "3์›”" -> "March";
            case "4์›”","5์›”" -> "4,5์›”";
            default -> "May";
        };
        
        // ์—ด๊ฑฐํ˜•์„ ์‚ฌ์šฉํ•˜๋ฉด 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์›”";
        };


        System.out.println(monthString);
    }
}

# ๋ฐ˜๋ณต๋ฌธ (for, while, for-each)

ํŠน๋ณ„ํ•œ ์ ์€ ์—†๋‹ค. continue์™€ break๋ฅผ ์ง€์›ํ•˜๋ฉฐ for-each๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ์ฒ˜๋Ÿผ ์ˆœํšŒํ•œ๋‹ค.

String[] numbers = {"one", "two", "three"};

// ๊ณ ์ „์ ์ธ for๋ฌธ
for(int i=0; i<numbers.length; i++) {
    System.out.println(numbers[i]);
}

// ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ์ด๋‹ค.
for(String number: numbers) {
    System.out.println(number);
}

 

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

JiwonDev

JiwonDev

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