• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

!= operator

 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
void assertTest(Object obj) {
assert obj != null : throw AssertionError();
}
REASON: Second operand must evaluate to an object or a primitive. (It can also be null.)
---
My question: what is that != operator. i know only ? operator to be used there. can anyone explain please.. Thanks for you in advance
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the operator that tests for unequality.

More information from SUN.
[ August 05, 2008: Message edited by: Ronald Schild ]
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The semi colon : is for displaying additional info which is supplied by expression/function appearing after the semi colon.

Note: You can't call a function that has returns type of void.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An assertion has the form...

assert booleanExp;

OR

assert booleanExp : expression;

If the booleanExp of an assertion is false, then an AssertionError is thrown at runtime. If the second form is used (with the colon and expression), then the expression is passed to the constructor of the AssertionError where it's converted to a String message.

As explained above, != is a relational operator meaning "not equal to." So the line...

assert obj != null;

...is asserting that "obj" is not a null reference.
 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mr.Marc.. i have understood now.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, don't confuse this with the ternary operator

boolean expr ? casetrue : casefalse

that is totally different from the boolean test in an assert.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic