Home ๐Ÿ” isPresent(), isEmpty()
Post
Cancel

๐Ÿ” isPresent(), isEmpty()



์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋‹ค๋ณด๋‹ˆ isPresent()์™€ isEmpty()์˜ ์ฐจ์ด๊ฐ€ ๊ถ๊ธˆํ•ด ์กŒ๋‹ค.
์ฐจ์ด๋Š” ๋ณ„๊ฑฐ ์—†์—ˆ๋‹ค..



isPresent() <-> isEmpty() : java11 ์—์„œ ์ฒ˜์Œ ์‚ฌ์šฉ



isPresent()



1
2
3
4
if (emailOptional.isPresent()) 
	throw new MemberInfoNotExistException();


  • null์ด ์•„๋‹Œ ๊ฒฝ์šฐ ๋‹ค์Œ ์ฝ”๋“œ ์‹คํ–‰

์•„๋ž˜์ฒ˜๋Ÿผ ์กฐ๊ฑด๋ฌธ์— ์ด๋ฆ„์ด ์กด์žฌํ•˜๋Š”์ง€ ์•ˆ ํ•œ๋Š”์ง€ ์ฒดํฌํ•˜๋Š” ๊ฒฝ์šฐ
๋‚ด๋ถ€ ๋กœ์ง์„ ํƒ€๊ธฐ ์ „ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๊ธฐ ์‰ฌ์šด๋ฐ, ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ ๋‹ค์Œ์— ๋ณ€์ˆ˜์— ๋Œ€ํ•œ null๊ฒ€์‚ฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ์„ ์žŠ์–ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋Š” nullPointException์ฒ˜๋ฆฌ๊ฐ€ ๋‚  ํ™•๋ฅ ์ด ๋†’๋‹ค.

1
2
3
4
5
if(name != null) {
    System.out.println(name.length());
}



isEmpty()



1
2
3
4
if (emailOptional.isEmpty()) 
	throw new MemberInfoNotExistException();



This post is licensed under CC BY 4.0 by the author.

/docker/ Docker ๊ธฐ์ดˆ ์ด๋ก 

/Java/ Entity์—์„œ ํ•„๋“œ๋‚˜ ์ปฌ๋Ÿผ์˜ ๊ธธ์ด์ œํ•œ ํ•ด์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜