CodeGym /Java Course /๋ชจ๋“ˆ 1 /๏ปฟ์ฝ˜์†”์—์„œ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ํ•˜๊ธฐ

๏ปฟ์ฝ˜์†”์—์„œ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ํ•˜๊ธฐ

๋ชจ๋“ˆ 1
๋ ˆ๋ฒจ 5 , ๋ ˆ์Šจ 3
์‚ฌ์šฉ ๊ฐ€๋Šฅ

์ด์ „ ํ•™์Šต์—์„œ ์šฐ๋ฆฌ๋Š” ์ž‘์€ ํ”„๋กœ๊ทธ๋žจ์„ ์ปดํŒŒ์ผํ–ˆ๊ณ  ๊ทธ ๋Œ€๊ฐ€๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ๋ฐ”์ดํŠธ์ฝ”๋“œ๋กœ ํฌํ•จํ•˜๋Š” MySolution.class ํŒŒ์ผ์„ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. ์†Œ์Šค ์ฝ”๋“œ๋Š” ์ด๋žฌ์Šต๋‹ˆ๋‹ค.


class MySolution {
   public static void main(String[] args) {
      System.out.println("Hi, command line!");
   }
}

์ด์ œ ์ด .class ํŒŒ์ผ์„ JVM์— ์ „๋‹ฌํ•˜์—ฌ ์‹คํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ ค๋ฉด java ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•˜๋Š” ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค .


D:\temp>java MySolution

" ์•ˆ๋…•ํ•˜์„ธ์š” , ๋ช…๋ น์ค„!" ์ฝ˜์†”์—์„œ.

์—ฌ๊ธฐ์„œ ํŒŒ์ผ ์ด๋ฆ„( MySolution.class )์ด ์•„๋‹ˆ๋ผ ํด๋ž˜์Šค ์ด๋ฆ„( MySolution ) ์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค .

์ฝ˜์†”์—์„œ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ์˜ ์ž…๋ ฅ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ args ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค .


public class MyArgs {
    public static void main(String[] args) {
        if (args.length == 3) {
            System.out.println(args[0].toLowerCase());
            System.out.println(args[1].toUpperCase());
            System.out.println(args[2].length());
        } else {
            System.out.println("Three parameters are expected.");
        }
    }
}

์ปดํŒŒ์ผํ•˜์ž...


D:\temp>javac MyArgs.java

๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.


D:\temp>java MyArgs

๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์„ธ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค .

๊ฐ€์žฅ ์ตœ๊ทผ ๋ช…๋ น์—์„œ ํด๋ž˜์Šค ์ด๋ฆ„ ๋’ค์— args ๋ฌธ์ž์—ด ๋ฐฐ์—ด ๋กœ ๋๋‚  ์ธ์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค . ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ:


D:\temp>java MyArgs One Two Three

๊ทธ๋Ÿฐ ๋‹ค์Œ args ๋ฐฐ์—ด์€ ["One", "Two", "Three"]๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

ํ™”๋ฉด ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

ํ•˜๋‚˜
๋‘˜
5

์ธ์ˆ˜์— ๊ณต๋ฐฑ์„ ํฌํ•จํ•˜๋ ค๋ฉด ํฐ๋”ฐ์˜ดํ‘œ๋กœ ๋ฌถ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


D:\temp>java MyArgs "One Two" Three "Four Five Six"

์‚ฐ์ถœ:

ํ•˜๋‚˜ ๋‘˜
์…‹
13

ํ”„๋กœ๊ทธ๋žจ์ด ๋‹จ์ผ ํŒŒ์ผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฒฝ์šฐ ๋ช…์‹œ์ ์œผ๋กœ ์ปดํŒŒ์ผํ•˜์ง€ ์•Š๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. java ์œ ํ‹ธ๋ฆฌํ‹ฐ์— ํŒŒ์ผ ์ด๋ฆ„( .java ํ™•์žฅ์ž ํฌํ•จ)๊ณผ ์ธ์ˆ˜๋ฅผ ์•Œ๋ ค์ฃผ์‹ญ์‹œ์˜ค .


D:\temp>java MyArgs.java param1 param2

์ด ๊ธฐ๋Šฅ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ๋ฐฐ์šฐ๊ธฐ ์‹œ์ž‘ํ•œ ์‚ฌ๋žŒ๋“ค์ด ๋” ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก Java 11์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋” ์ž์„ธํ•œ ์ •๋ณด:
java ๋ช…๋ น ์—๋Š” ๋„์›€๋ง์ด ๋‚ด์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ‘œ์‹œํ•˜๋ ค๋ฉด ๋ช…๋ น์ค„์—์„œ ๋‹ค์Œ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • ์ž๋ฐ” --๋„์›€๋ง
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION