This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Qn on assertion.

 
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??
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • 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???
 
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)
 
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??

 
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
 
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic