This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Asserts in JDK1.4  RSS feed

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has ne 1 used asserts in their project ? Would they really help in the development phase of a project ?
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Deepak Mahbubani:
Has ne 1 used asserts in their project ? Would they really help in the development phase of a project ?


I've used assert in C++ projects. In my opinion, they do help but I've learned to adapt my debugging to Java so I'm not used to having them available.
If assert works in Java the way it does in C++, I think it will be a very useful addition to the language.
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
and what are asserts ? never heard this before in this relation. so if anybody has the time to enlighten me......:-) feel free.
thanks
k
 
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by karl koch:
hi,
and what are asserts ? never heard this before in this relation. so if anybody has the time to enlighten me......:-) feel free.
thanks
k


Hi, same here. what is assert? maybe i am old and out of the loop. enlight us.
 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check out this article about asserts in 1.4 http://www.javaworld.com/javaworld/jw-12-2001/jw-1214-assert.html?
 
Jason Kilgrow
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by karl koch:
hi,
and what are asserts ? never heard this before in this relation. so if anybody has the time to enlighten me......:-) feel free.
thanks
k


Use asserts during testing/debugging when you are sure that a condition should never exist and wouldn't kill the program but you want the program to die if the condition ever does happen.
Is that about it in a nutshell?
That's a good article and it sounds like Java asserts are going to be a bit more than just a debugging/testing tool.

[This message has been edited by Jason Kilgrow (edited December 19, 2001).]
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C++, an assert statement is a function(method) that takes a logical(Boolean) expression as a parameter and halts the program if the expression is false.
Its mostly used to test preconditions on functions(methods).


Assertions are generally only used while code is in development as a debugging tool. VERY RARELY are they left in a production program.
Hope this helps.
------------------
Jason R. Kretzer
Software Engineer http://alia.iwarp.com
[This message has been edited by Jason Kretzer (edited December 19, 2001).]
[This message has been edited by Jason Kretzer (edited December 19, 2001).]
[This message has been edited by Jason Kretzer (edited December 19, 2001).]
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I production program that has asserts... Has anyone else ever gotten an error message from jikes about some timestamp assert failing? Very weird when it happens. The ideas of class invarients and asserts are generally most useful when debugging, but they can't hurt too much in deployment situations. Certainly you don't want a bunch of debug statements being printed, but I often like to keep asserts just in case.
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can just imagine the look on some poor guy's face when his accounting software stops and a DOS console window comes up saying that an assertion failed. Hilarious.
Anyway, before I submit my code for review or for distribution, I remove my assert statements with the "search and replace" function of my editor as a matter of habit.
I am excited(about as much as one can be about it ) about the assert being added to Java though. As stated earlier, I don't think I will use it very much because I have gotten used to not being able to. However, it could help new users from C++ feel more comfortable with Java.

------------------
Jason R. Kretzer
Software Engineer
http://alia.iwarp.com
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, an assertion is better than a core dump. By the way, what language are you using? Removing your assert()'s by search and replace doesn't sound like a good idea. Do you remove the asserts from a separate source tree so they're still available, or just delete them permanently? In C++ it's typical to use macros, but in languages without a preprocessor or explicit assert statements it sounds like it'd be awkward.
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is so true-- an assertion popping up is better than a core dump.
I generally just open up each source and header file(this is C++) and use a Ctrl+f to find each one and delete it. I then replace it with a comment. Since the things I write are not generally that big, it does not take that long. I know it is awkward but this way I know for sure that I got each one.
I try to stay away from macros, simply because I like to do things myself.
------------------
Jason R. Kretzer
Software Engineer
http://alia.iwarp.com
[This message has been edited by Jason Kretzer (edited December 19, 2001).]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Speaking of assertions being in/excluded in production versions, does anyone know how Java will handle this?
 
Jason Kilgrow
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by David Garland:
Speaking of assertions being in/excluded in production versions, does anyone know how Java will handle this?


From the article on JavaWorld that was mentioned earlier, it sounds to me like they are going to be used in a production environment. Not like C++ assertions at all. That's just my interpretation of what I read in the article.
 
Author
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by David Garland:
Speaking of assertions being in/excluded in production versions, does anyone know how Java will handle this?


It's enabled on a package by package / class-by-class basis, using the JVM launch flag -enableassertions (or -ea for short).
You can enable assertions on all classes, or on a subset of classes, using a combination of -enableassertions and -disableassertions flags.
There's a programmatic API that enables you to enable assertions from inside a program - but whether assertions are enabled on a particular class or not is set at the time that class is loaded, and fixed from then on - it can't be switched later. You enable assertions for a class that hasn't yet been loaded by talking to the ClassLoader.

As for whether you turn them on at runtime or not, there's two schools of thought: one is that assertions should be used during testing to identify bugs and stop the program. The other is that assertions should be enabled at runtime, since they document exactly what the programmer thinks is going on at that point in program execution. If an assertion fails, it means the programmer was wrong - so the program should probably not carry on going.
If an assertion fails in your accounting software, like, say, after recording a balance transfer, you checked that the total balances of all accounts was the same - and it turned out not to be - then that means there's a bug in your program. Would you really prefer your accounts guy to carry on regardless under those circumstances? An assertion failure tells you the program is broken, and closes it down, putting a big sign on the door saying 'danger - do not enter'.
------------------
James Hart
Wrox
Author of "Early Adopter J2SE 1.4"
[This message has been edited by James Hart (edited December 19, 2001).]
 
mooooooo ..... tiny ad ....
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!