Assertions basically throw an error when the Boolean expression you provide is false. It is a simple mechanism. You could easily accomplish the same thing with an if statement, where you would throw an exception if the expression is false. However, the three primary advantages of an assertion are that they use simple syntax, they document code, and they can be ignored by the compiler with a command-line switch. Notice that you can throw an error with an if statement, but you can't tell the compiler to ignore that if statement. An assertion enables you to place test points in your code and is a quick and effective way to detect and correct bugs . For example, if you want to stop your program running at the point a given variable reaches a predetermined value, you could use an assertion. Mind you, assertions are designed for debugging and easy removal from production classes. As mentioned, an advantage of assertions is you can compile your code so that they are ignored. That way, you can develop with them, but deploy classes without them. This is how they are normally used. You might want to use assertions in your project to help speed development. The RMI portion is especially difficult. In fact, RMI still needs polish, which is what makes this functionality so hard to work with. I spent weeks trying to fix a problem with it. The RMI portion of my project worked well on Windows, but not on Solaris because there is a bug in the RMI library. I finally figured out a workaround so that my client on Windows could finally talk to the server on Solaris. In theory, the same code, except for OS-dependent items such as the file path , should work the same on all Java Virtual Machines (JVMs), regardless of the OS. That is usually true, but Java isn't perfect. Using assertions is a new feature to help ease debugging tasks and make it much easier to test code sections. There are two ways to write an assertion. The only difference between the two is whether you want to pass a message to the thrown AssertionError in case the expression is false. The statement that doesn't pass a message looks like so: assert expression; The statement that does pass a message looks like this: assert expression: message; The following is an example: assert file(filename)!= null: "could not find " + filename;
|