Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java assertions

 
Adi Sharma
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adi Sharma
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot

Aditya SHarma
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic