*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertions doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertions doubt" Watch "Assertions doubt" New topic
Author

Assertions doubt

Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
How come the following code compiles successfully?


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??
Pritish Chakraborty
Ranch Hand

Joined: Jun 12, 2012
Posts: 91

The object won't be referable by a live thread and so is eligible for garbage collection, but that does not change the fact that it is an object still living on the heap.

That is enough to satisfy the compiler. You don't need to be able to refer to the object.

When you read about GC in K&B, 'lost' implied that no live thread could access the object (meaning we couldn't).

But that did not mean that the object was destroyed.


OCJP 6
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

@Kedar new Clumsy() would inturn generate a value. It has nothing to do whether the object created is lost or can be referred. Please try and let us know.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Pritish Chakraborty
Ranch Hand

Joined: Jun 12, 2012
Posts: 91

Now I'm confused Nitish...

I suppose toString() will be called implicitly and the object's unsigned hexadecimal ID of sorts will be printed, in your code.

But does the same happen in assertions?
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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
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.


Thanks Nitish!! That helped!
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Assertions doubt