Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Qn on assertion.

 
cs singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


output is done, but if i change //1 as assert t>4,then bar should be printed out, but its not, its still giving the answer "done". can somebody pls explain??
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assertions are disabled by default during runtime. You can use the -ea option to enable assertions in all classes but for the System classes. Your compiler should be javac 1.4 or higher.

java -ea Test that should do.
 
cs singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what should be output then???
 
Krishnamoorthy Sethuraman
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
barException in thread "main" java.lang.AssertionError: 7
at javaranch.Test.main(Test.java:14)
 
Graham Walsh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

what exactly does the following syntax do? I know assert t<4 but what does the subsequent : bar(7) do?

thanks

G


assert t<4:bar(7);// 1

Originally posted by cs singh:


output is done, but if i change //1 as assert t>4,then bar should be printed out, but its not, its still giving the answer "done". can somebody pls explain??
 
sreedhar lak
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just copy and pasted above code to test.But even it is not compiling.

it is giving following warnings and errors,i can ommit warnings but what about errors because of these two errors it is not generating class file.
I dont know the concept of assertions ,can any one help me to find clear idea about this.Like uses.

---------- compile ----------
TestAssert.java:16: warning: as of release 1.4, assert is a keyword, and may not be used as an identifier
assert t<4:bar(7);
^
TestAssert.java:16: ';' expected
assert t<4:bar(7);
^
TestAssert.java:17: warning: as of release 1.4, assert is a keyword, and may not be used as an identifier
assert t>1:foo(8);
^
TestAssert.java:17: ';' expected
assert t>1:foo(8);
^
2 errors
2 warnings
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sreedhar,

For the following program using assert at //1 and //2
public class Test{
public static int y;
public static int foo ( int x){
System.out.println("foo");
return y=x;
}
public static int bar(int z){
System.out.println("bar");
return y=z;
}
public static void main(String args[]){
int t=2;
System.out.println("Before assert");
assert t>4:bar(7); //1
assert t>1:foo(8); //2
System.out.println("done : y : "+y);
}
}

1)You have to compile this java file as, "javac -source 1.4 Test.java" to enable assert.
2)Also while running you have to use, "java -ea Test" to enable assertion during runtime.

If you do the above two things, for //1 you will get an java.lang.AssertionError as the condition (t>4) is false
and for //2 you will not get any assert error as here (t>1) is true.
 
sreedhar lak
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anuradha,
Thank you very much ,it is working fine now.
But,can you tell me some thing about assertion i read in books but i didnt get clear idea about that.
Please ,give me some explanation with uses,if possible by example.

Thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic