It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion issue" Watch "Assertion issue" New topic

Assertion issue

Ajit Sawant
Ranch Hand

Joined: Nov 26, 2008
Posts: 33

public class Testassert{
public static void main (String [] args){
System.out.println("before assert " +args[0]);
assert (args[0]== "y");
System.out.println("after assert " +args[0]);
For the program, I am always getting assertion error java.lang.AssertionError.I expect to see that error only when somthing other than "y" is passed as an argument to the program.
java -ea Testassert y --- should not give any error
java -ea Testassert x --- should give the error

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

How do you compare strings in Java ?

[My Blog]
All roads lead to JavaRanch
Ajit Sawant
Ranch Hand

Joined: Nov 26, 2008
Posts: 33
I corrected it to args[0].equals("y"). Now it works.
Thanks for making me to think.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
I think it is not good to use Assertions to validate Command-line arguments....

SCJP5 and SCWCD1.5
Think Twice Act Wise...
I agree. Here's the link:
subject: Assertion issue
It's not a secret anymore!