Assertions doubt

Rule - Assertions require that the second expression must evaluate to a value. Here, it is creating a new object, but where are we assigning the address of the new object to a reference variable(to hold the value) to satisfy the rule??
Isn't the value lost??

Its not unsigned hexadecimal ID. Its the hashCode of the object. If there isn't a proper toString(), the Object's toString() prints out the hashCode of the created object.Also, if you want to test it out why not try to run it. Run using java -ea Clumsy.

Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39

posted Jul 29, 2012 11:11:55

0

Nitish Bangera wrote:Its not unsigned hexadecimal ID. Its the hashCode of the object. If there isn't a proper toString(), the Object's toString() prints out the hashCode of the created object.Also, if you want to test it out why not try to run it. Run using java -ea Clumsy.

the second form of the assert statment assert exp1 : exp2 where exp1 must evaluate to boolean. the exp 2 gets passed to constructor which returns its string value. the second form is just to provide us extra information as to what went wrong. so in your case when you supplied new clumsy() as exp2 it would call its inherited( or the overridden one) toString method , which will return Clumsy@<hashcode in hexadecimal format>. so whenever assertion is false the message will be returned along with the AssertionError.