๊ด€๋ฆฌ ๋ฉ”๋‰ด

Coding Planet

[Java] length vs length()์˜ ์ฐจ์ด ๋ณธ๋ฌธ

๐Ÿ’ป Java Study/Java ์ด๋ก  ์ •๋ฆฌ

[Java] length vs length()์˜ ์ฐจ์ด

jhj.sharon 2023. 8. 26. 00:05
๋ฐ˜์‘ํ˜•
๊ธฐ์ดˆ ์ค‘์˜ ๊ธฐ์ดˆ์ธ๋ฐ ์ตœ๊ทผ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ’€๋ฉด์„œ ์—ฌ์ „ํžˆ ์ด๊ฒƒ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค;; 
๋‹ค์‹œ ์ •๋ฆฌํ•œ๋‹ค...

 

1. length (ํ”„๋กœํผํ‹ฐ)

  • length๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด(ํฌ๊ธฐ)๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ”„๋กœํผํ‹ฐ
  • ๋ชจ๋“  ๋ฐฐ์—ด์— ๋Œ€ํ•ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
int[] arr = {1, 2, 3, 4, 5};
int size = arr.length; // size๋Š” 5

 

2. length() (๋ฉ”์„œ๋“œ)

  • length()๋Š” String ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋กœ, ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜
  • ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” ํ•ด๋‹น ๋ฌธ์ž์—ด์— ํฌํ•จ๋œ ๋ฌธ์ž์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
String str = "hello";
int len = str.length(); // len๋Š” 5

 

๋ฐ˜์‘ํ˜•
Comments