๋กœ์ผ“๐Ÿพ
article thumbnail
๋ฐ˜์‘ํ˜•

 

 

์˜ค๋Š˜์˜ ์ž๋ฐ”์—์„œ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜์ธ isEmpty์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. isEmpty๋Š” String ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ์ด๋ฏ€๋กœ ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  isEmpty๋Š” null์„ ์ฒดํฌํ•ด์ฃผ์ง„ ์•Š์Šต๋‹ˆ๋‹ค.

 

l ์˜ˆ์‹œ

 

package com.ex01;

public class test01 {

	public static void main(String[] args) {
		String str = "test";
		String str2 = "";//๊ณต๋ฐฑ ์—†์Œ
		String str3 = " ";//๊ณต๋ฐฑ ์žˆ์Œ
		
		System.out.println(str.isEmpty());//=>false
		System.out.println(str2.isEmpty());//=>true
		System.out.println(str3.isEmpty());//=>false
	}

}

 

isEmpty๋Š” ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ ๊ทธ ๊ฐ์ฒด๊ฐ€ ๋น„์–ด์žˆ์„๋•Œ true๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค. ๊ณต๋ฐฑ์˜ ๊ฒฝ์šฐ ๊ณต๋ฐฑ๋„ ๊ฐ์ฒด์•ˆ์— ๋‚ด์šฉ์ด ์กด์žฌํ•œ๋‹ค๊ณ  ํŒ๋‹จํ•˜์—ฌ false๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

(isBlack ๊ฐ™์€ ๊ฒฝ์šฐ ๊ณต๋ฐฑ๋„ true๋กœ ์ฒ˜๋ฆฌํ•ด์ค๋‹ˆ๋‹ค.)

 

package com.ex01;

public class test01 {

	public static void main(String[] args) {
		String str = null;
	
		System.out.println(str.isEmpty());//=>Error NullPointerException
	}

}

 

null์— isEmpty ํ•  ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. null ์ผ ๊ฒฝ์šฐ String ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” isEmpty ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

์ฆ‰, null ๊ฐ™์€ ๊ฒฝ์šฐ ๋ฉ”๋ชจ๋ฆฌ์— ์•„์˜ˆ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๊ณ , isEmpty์˜ ๊ฒฝ์šฐ ๊ฐ์ฒด๋Š” ์žˆ์ง€๋งŒ ๋น„์–ด์žˆ์„๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


l ์š”์•ฝ

str str == null  str.isEmpty()
null true NullPointerException
"" false true
"test" false false

 

 

 

 

 

 

 

์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์งˆ๋ฌธ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.

 

"๋‚œ ๋ฐ˜๋“œ์‹œ ๋ฐฑ์—”๋“œ ์™•์ด ๋ ๊ฑฐ์•ผ"

๋ฐ˜์‘ํ˜•
profile on loading

Loading...