aspose file tools*
The moose likes Beginning Java and the fly likes java assertions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java assertions" Watch "java assertions" New topic
Author

java assertions

Adi Sharma
Ranch Hand

Joined: May 18, 2009
Posts: 33
Hi
I was reading SCJP 6 and came across:

The rule is, an assert expression should leave the program in the same state it was
in before the expression

public void doStuff() {
assert (modifyThings());
// continues on
}
public boolean modifyThings() {
y = x++;
return true;
}


Can anyone explain what it means


Thanks
Aditya Sharma
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Assertions can be turned on or off by a simple command line flag. That means that your behaviour changes if you turn them on or off.

In this example, if assertions are turned on, x will be increased and y will be assigned. If they are turned off, both will not occur. This may break your code, and you will be left wondering why.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Adi Sharma
Ranch Hand

Joined: May 18, 2009
Posts: 33
thanks a lot

Aditya SHarma
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
Adi Sharma wrote:The rule is, an assert expression should leave the program in the same state it was in before the expression
I presume that code was given as an example of what to avoid.

And please use the CODE button.
Adi Sharma
Ranch Hand

Joined: May 18, 2009
Posts: 33
Hi
I have another question regarding assertions.

I came across this :

The second expression, used only with the simple version of an assert
statement, can be anything that results in a value.


What does he means by " can be anything that results in a value"?

Thanks
Aditya Sharma
Adi Sharma
Ranch Hand

Joined: May 18, 2009
Posts: 33
Hi
I would also like to ask what the following line means:

java -ea -dsa : Enable assertions in general, but disable assertions in system classes.

My question is what does -dsa stands for and what are system classes?

Thanks
Aditya Sharma
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Adi Sharma wrote:What does he means by " can be anything that results in a value"?

It can be any literal value, variable, tertiary operator (x ? y : z) or method that does not return void. So:
- 1
- "x"
- false
- myVariable
- new Object()
- myObject.getValue()

Adi Sharma wrote:java -ea -dsa : Enable assertions in general, but disable assertions in system classes.

My question is what does -dsa stands for and what are system classes?

-dsa is a command line flag which means "disable system assertions". System classes are those in rt.jar, a.k.a. those in the Java API, a.k.a. classes provided to you by Sun itself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java assertions