Nov 08

During SCJP-certification it will be distinguished between (non-)appropriate use of assertion. In this context I would like to  give you an overview of unappropriate use:

  1. Just a short reminder: Assertions are code-checks which lead at false to abortion of the program. Surely those assertions could be catched via try/catch/finally, but this is not appropriate
  2. Assertions should not be used in public methods since it can not always be clear while coding from which point this method is called, which values are needed and used. In opposite to this Assertions within private methods are managable and therefore appropriate
  3. As you already know; Assertions can be turned off and on everytime. Therefore they should not be used for values which have been given with program call. Here always used exceptions are more appropriate.
  4. Loops/ statements which should never been reached, should be before, but in the loop/statement checked via assertion
  5. Assertions should leave the program in the same state it had before use of assertion. Therefore it is important to avoid sideeffects and never check a declarion or change of variables. Checking the value itself is appropriate

Please be careful: Unappropriate use does not mean unlegitimate programming. It only means that assertions should not be used in one of the explained ways. All those points above would be compiled and executed without any problem.

Tagged with:
Nov 06

Today I would like to go a little bit deeper in turning asserts on and off:

java -ea -dsa Test

is only turning off assertions in system-classes

java -esa Test

is only turning on assertions in system-classes

It is also possible to turn on assertions only for particular packages:

java -ea -da x… Test

here the class Test is started with assertions on; only for the package x and its subpackages assertions are turned off

java -ea -da x.y Test

in this example assertions are on; but for the package x.y (and only for this) assertions are off

java -ea -da x.y.Angestellter Test

here class Test is started with assertions on; only for class Angestellter in package x.y asertions are off

Surely this is working also the other way around; means starting with assertions off and only turning a few of them on.

In case you are insecure about this DOS-Compiling – stay tuned; I will explain soon more about it.

Tagged with:
Nov 04

assertions will be turned on during program start with

java -ea <path/filename>

or

java -enableassertions <path/filename>

They will be turned off with

java -da <path/filename>

or

java – disableassertions <path/filename>

in older verstions (1.3 and earlier) the name assert was allowed to be used, eg

int assert = 1;

or

int assert = check();

if (assert > 2) { // … }

This is with higher Versions not possible.  Should a code compiled with code like above, this can be done with

javac -source 1.3 <Dateiname>

Therefore the appropriate version need to be implemented.

Tagged with:
Nov 02

Additonally there is a possibility to give a message with the error, eg

assert (a.PersNr > 0): “This is an additional output”;

This leads in context with a negative PersNr to an output like

Exception in thread “main” java.lang.AssertionError: This is an additional output
at Angestellter.main(Angestellter.java:42)

Instead of the additional text there can we also methods called after the colon, eg

assert (a.PersNr > 0): Math.sqrt(144); // In case PersNr < 0 execute method Math.sqrt(144) and print the result

Tagged with:
Oct 30

Assertions Errors or shortly Assertions are code checks.

Let’s have a look on the following example:

Angestellter a = new Angestellter ();
Scanner s = new Scanner (System.in);
a.PersNr=s.nextInt();

s.close();

before Assertions, wrong values whould be checked in a similar way to this:

if (a.PersNr > 0)

System.out.println(“ok”);

else

System.out.println(“not ok”);

with help from Assertions this 4 lines can be compressed in one line:

assert (a.PersNr > 0);

Only in case assert-condition is not true, an AssertionError is thrown … and you know what is the best on it? You can enable and disable it with options -ea (on) respectively -da (off)!

For eclipse, it can be done eg in menu  File/Properties/Run-Debug-Settings/Edit/Reiter Arguments/ Field VM-Arguments:  -ea

The SCJP-DOS-relevant compiling I will explain later in more details.

Tagged with:
Oct 22

This error is in general an sign for missing java-archives in classpaths. Possible reasons could be

  • class is not set to public
  • classpath – environment variable need to contain the folder of the class, respectively the package
  • in case the class is included in a package; it need to be called over the package name, eg java package_name.myClass
  • invoking of the file need to be done without ending .*class

In the following months I will go deeper in classpaths and compilers, but 1st of all we still have some time to spent on exceptions and errors :)

Tagged with:
Oct 20

In case stack is full, StackOverflow error is thrown:

class Angestellter {

static void overflow () {

overflow();

}

public static void main(String args[]) {

overflow();

}

}

in our example a self calling method is defined which is exhausting the stack.

Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs