aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Qn on assertion. 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 "Qn on assertion." Watch "Qn on assertion." New topic
Author

Qn on assertion.

cs singh
Ranch Hand

Joined: Dec 28, 2005
Posts: 36


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

Joined: Feb 24, 2006
Posts: 6662
    
    5

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.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
cs singh
Ranch Hand

Joined: Dec 28, 2005
Posts: 36
what should be output then???
Krishnamoorthy Sethuraman
Greenhorn

Joined: Dec 12, 2005
Posts: 24
barException in thread "main" java.lang.AssertionError: 7
at javaranch.Test.main(Test.java:14)
Graham Walsh
Greenhorn

Joined: Mar 08, 2006
Posts: 23
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

Joined: Feb 26, 2006
Posts: 42
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


Thanks a lot<br />Learn from yesterday, live for today, hope for tomorrow. The important thing is to not stop questioning
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
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.


SCJP 6.0 90%
sreedhar lak
Ranch Hand

Joined: Feb 26, 2006
Posts: 42
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
 
 
subject: Qn on assertion.