CodeGym /Java Course /์ž๋ฐ” ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋”ฉ /์ˆซ์ž(์ •์ˆ˜, Long, ๋ฐ”์ดํŠธ)

์ˆซ์ž(์ •์ˆ˜, Long, ๋ฐ”์ดํŠธ)

์ž๋ฐ” ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋”ฉ
๋ ˆ๋ฒจ 9 , ๋ ˆ์Šจ 4
์‚ฌ์šฉ ๊ฐ€๋Šฅ

"๊ฐœ๋ฐœ์ž๋Š” ์ˆซ์ž๋ฅผ ์„ค๋ช…ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ ๊ฐœ๋ฐœ์ž์ฒ˜๋Ÿผ ์ฐฝ์˜์ ์ด๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค."

"์ฒซ์งธ, ๊ทธ๋“ค์€ Byte, Short, Integer, Long, Float ๋ฐ Double์ด ํŒŒ์ƒ๋˜๋Š” ์ถ”์ƒ Number ํด๋ž˜์Šค๋ฅผ ์ƒ๊ฐํ•ด ๋ƒˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ์ˆซ์ž๋ฅผ ๋‹ค๋ฅธ ์ˆซ์ž ์œ ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค."

Number ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ
1
int intValue()
2
long longValue()
์‚ผ
float floatValue()
4
double doubleValue()
5
byte byteValue()
6
short shortValue()

"๋งž์•„์š”. ๊ฒฐ๊ตญ ์ด๋ ‡๊ฒŒ ์“ฐ๋ฉด ์•ˆ ๋ผ์š”."

Long x = 100000;
Integer y = (Integer) x;

"์˜ˆ, ์ด๋Ÿฌํ•œ ์œ ํ˜•์€ ํ”„๋ฆฌ๋ฏธํ‹ฐ๋ธŒ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์šฐ๋ฆฌ๊ฐ€ Number ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค."

Long x = 100000;
Integer y = x.intValue();

"๊ทธ๋Ÿฌ๋‚˜ ์—ฌ์ „ํžˆ ๊ณ ๋ คํ•ด์•ผ ํ•  ๋ช‡ ๊ฐ€์ง€ ์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Integer๋Š” int๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— Integer ๊ฐœ์ฒด๋Š” ๊ณ ์ „์ ์ธ ยซ==ยป ์—ฐ์‚ฐ์ž์™€ ๋น„๊ตํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."

๊ธฐ๋ณธ ์œ ํ˜• ๋น„๊ต
int x = 500;
int y = 500;
x == y; //true
๊ธฐ๋ณธ์ด ์•„๋‹Œ ์œ ํ˜• ๋น„๊ต
Integer x = 500;
Integer y = 500;
x == y; //false
x.equals(y); //true

"๋งž์•„์š”. ์–ด์ฉ์ง€ ๊ฐ‘์ž๊ธฐ ๊ทธ๋Ÿฐ ์ƒ๊ฐ์ด ์•ˆ ๋‚ฌ์–ด์š”."

"ํ•˜์ง€๋งŒ ๋” ์žˆ์Šต๋‹ˆ๋‹ค."

"๋‹น์‹ ์€ ๋‚ด ํšŒ๋กœ๋ฅผ ๋‹จ๋ฝ์‹œํ‚ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค! ๋˜ ๋ฌด์—‡์ด ์žˆ์Šต๋‹ˆ๊นŒ?"

"Integer ๋ณ€์ˆ˜์— int ๊ฐ’์„ ํ• ๋‹นํ•˜๋ฉด Integer.valueOf ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค."

์•”ํ˜ธ ์‹ค์ œ๋กœ ์ผ์–ด๋‚˜๋Š” ์ผ
Integer x = 5;
Integer x = Integer.valueOf(5);

"์˜ˆ, ์œ„์˜ ์˜ˆ์—์„œ ์ด๋ฏธ ์ดํ•ดํ–ˆ์Šต๋‹ˆ๋‹ค."

"๊ทธ๋Ÿฌ๋‚˜ valueOf ํ•จ์ˆ˜๊ฐ€ ํ•ญ์ƒ ์ƒˆ๋กœ์šด Integer ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค."

"์–ด, ยซ์–ธ์ œ๋‚˜ ๊ทธ๋ ‡์ง€๋Š” ์•Š๋‹คยป๋Š”๊ฒŒ ๋ฌด์Šจ ๋œป์ด์•ผ?"

"-128์—์„œ 127๊นŒ์ง€์˜ ๊ฐ’์„ ์บ์‹œํ•ฉ๋‹ˆ๋‹ค."

์•”ํ˜ธ ์‹ค์ œ๋กœ ์ผ์–ด๋‚˜๋Š” ์ผ ์„ค๋ช…
Integer x = 300;
Integer y = 300;
Integer z = 300;
Integer x = Integer.valueOf(300);
Integer y = Integer.valueOf(300);
Integer z = Integer.valueOf(300);
๋ณ€์ˆ˜ x, y ๋ฐ z๋Š” ๋‹ค๋ฅธ ๊ฐœ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
Integer x = 100;
Integer y = 100;
Integer z = 100;
Integer x = Integer.valueOf(100);
Integer y = Integer.valueOf(100);
Integer z = Integer.valueOf(100);
๋ณ€์ˆ˜ x, y ๋ฐ z๋Š” ๋™์ผํ•œ ๊ฐœ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
Integer x = new Integer(10)
Integer y = new Integer(10)
Integer z = 10;
Integer t = 10;
Integer x = new Integer(10)
Integer y = new Integer(10)
Integer z = Integer.valueOf(10);
Integer t = Integer.valueOf(10);
๋ณ€์ˆ˜ z์™€ t๋Š” ๋™์ผํ•œ ๊ฐœ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

"์ฆ‰, ์ƒํ™ฉ์€ ์ด๋ ‡์Šต๋‹ˆ๋‹ค."

1)  ยซnew Integer()ยป๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์ƒˆ ๊ฐ์ฒด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2)  ๋ช…์‹œ์ ์œผ๋กœ ๋˜๋Š” ์˜คํ† ๋ฐ•์‹ฑ์„ ํ†ตํ•ด Integer.valueOf()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด number ์ธ์ˆ˜๊ฐ€ -128์—์„œ 127 ์‚ฌ์ด์˜ ๋ฒ”์œ„์— ์žˆ์œผ๋ฉด ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒˆ ๊ฐœ์ฒด ๋˜๋Š” ์บ์‹œ๋œ ๊ฐœ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

"์บ์‹œ์—์„œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์™œ ๊ทธ๋ ‡๊ฒŒ ๋”์ฐํ•œ๊ฐ€์š”?"

"์‹ ๊ฒฝ ์“ฐ์ง€ ๋งˆ์„ธ์š”. ๋•Œ๋•Œ๋กœ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ฒฝ์šฐ ๊ฐœ์ฒด๊ฐ€ ๊ฐ™์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ‰๋“ฑ์ด ์žˆ๋Š” ๋ชจ๋“  ๊ฒƒ์€ ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค. ํ”„๋ฆฌ๋ฏธํ‹ฐ๋ธŒ๋ฅผ ํ”„๋ฆฌ๋ฏธํ‹ฐ๋ธŒ๊ฐ€ ์•„๋‹Œ ๊ฒƒ๊ณผ ๋น„๊ตํ•˜๋ฉด ํ”„๋ฆฌ๋ฏธํ‹ฐ๋ธŒ๋กœ ๋น„๊ต๋ฉ๋‹ˆ๋‹ค."

๋น„๊ต ๋ฌธ์ œ
int x = 300;
Integer y = 300;
Integer z = 300;

x == y; //true (comparison based on primitive value)
x == z; //true (comparison based on primitive value)
y == z; //false (comparison based on references)
ํ›จ์”ฌ ๋” ํฅ๋ฏธ๋กœ์šด ์˜ˆ์ž…๋‹ˆ๋‹ค. ์บ์‹œ๋Š” ์—ฌ๊ธฐ์— ๊ทธ๋ฆผ์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค
int x = 100;
Integer y = 100;
Integer z = 100;

x == y; //true (comparison based on primitive value)
x == z; //true (comparison based on primitive value)
y == z; //true (comparison based on references; they point to the same object)
๊ทธ๋Ÿฌ๋‚˜ ์บ์‹œ๋Š” ์—ฌ๊ธฐ์— ๊ด€์—ฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค
int x = 100;
Integer y = new Integer(100);
Integer z = 100;

x == y; //true (comparison based on primitive value)
x == z; //true (comparison based on primitive value)
y == z; //false (comparison based on references; they point to different objects)

"์ข‹์•„์š”... ๊ทธ๋Ÿฐ๋ฐ ์ด๊ฑธ ์–ด๋–ป๊ฒŒ ๋‹ค ์™ธ์šฐ์ฃ ?"

"์ด๊ฒƒ์„ ์™ธ์šธ ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋“  ๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ํ”„๋ฆฌ๋ฏธํ‹ฐ๋ธŒ์™€ ๋น„ํ”„๋ฆฌ๋ฏธํ‹ฐ๋ธŒ๊ฐ€ ์ž‘๋™ํ•  ๋•Œ ์‹ค์ œ๋กœ ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ์ดํ•ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค."

"๋˜ํ•œ Integer ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํ›Œ๋ฅญํ•˜๊ณ  ์œ ์šฉํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ๊ฝค ๋งŽ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ๊ทธ ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ฝค ์ž์ฃผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค."

"๊ทธ๋ž˜, ๊ธฐ์–ต๋‚˜. Integer.parseInt();"

์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION