Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion issue

 
Ajit Sawant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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

Thanks,
Ajit
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you compare strings in Java ?
 
Ajit Sawant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I corrected it to args[0].equals("y"). Now it works.
Thanks for making me to think.
Ajit
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is not good to use Assertions to validate Command-line arguments....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic