ocpjp 오라클 자바 자격증

[ocpjp][assert] 자바 assertion

xemaker 2023. 10. 3. 12:56
package asser;

public class Product {
	public double applyDiscount(double price) {
		assert(price>0);
		return price*0.50;
	}
	public static void main(String[] args) {
		Product p=new Product();
		double newPrice=p.applyDiscount(Double.parseDouble(args[0]));
		System.out.println("New Price: "+newPrice);
	}
}

 


the command: java Product 0
What is the result?

출력:
New Price: 0.0

해설:
the command haven't -ea

즉 자바 실행 시 -ea 옵션을 넣지 않았기 때문에 assertion이 적용되지 않는다.

문제에서 java Product 0 으로 되어 있다.

참고로 -ea 의미는 enable assertion 이라는 뜻이다.