Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com

Essential.

In this applet a multi-line component is used to show what the program is doing :

The first statements executed are those in the applet's constructor :

A constructor always carries the same name as the class.

The TextArea is created as one showing 20 rows and 30 columns/positions :

Then a line of text is placed in this component :

\n meaning end-of-line, new-line, a special character.

After construction, the init method is called, BECAUSE THIS IS AN APPLET.

The TextArea is added to the applet ....

and the applet is told to place it properly sized and positioned :

Text is added to the TextArea to show we're in the init method.

As you can see, the position of a method inside a class has nothing to do with when it is executed.

( It makes sense to put the most important methods at the top of the class, with regard to maintenance. )

The doThis method is called.

Here, three java.lang.String references (pointers to them) are used .

The first two will refer to these two Strings.

The method goTogether is called and the references to "Love" and "Marriage" are passed as arguments :

In this method the reference to "Love" is called s1, and that to "Marriage" is called s2.

A new String is created, combining the Strings "Love", " and ", "Marriage" :

The reference to this new String, res , is returned, leaving the method and taking the new String's reference back to where the method was called,....

so it can be stored in strjoined :

Notice that the return type of the method goTogether is String, not void :

As you see, they don't have to be on the same line. This is often done to improve readability.

The same method is called again, but with different strings.

Then 2 variables of type double are declared ....

and one of type int .

The double number is initalized . A double value is used containing a decimal-point.

This in contrast to the int .

The method raise is called with the variables number and power as its arguments .

The arguments are copied and these copies are named nr and pw .

Notice that the arguments' data types correspond exactly to those in the call : double and int.

If pw equals zero :

( two times '=' means EQUALS !!! )

,the double 1.0 is returned. Since this is not the case in our example, we will continue.

A double, res, is declared and nr's value, 2.0, is copied to it :

Then res and nr are multiplied 7 times :

This could also have been coded as follows :
    int i = 1 ;
    while ( i < pw )
    {
      res = res * nr ;
      i++ ;
    }

The resulting value, 256.0 , is returned by means of res ,....

so this can be stored in result, back in the doThis method ....

and shown in the TextArea.

The method raise is called again with different values as arguments, on-the-spot, the returned value will be part of the String that's parameter of ta.append.

Next the method isDividable is called with arguments 52 and 4 :

Here, 52 is divided by 4, and we check if there's a remainder :

If there's not, as is the present case, true is returned :

so this can be stored in the variable itIsSo, back in the doThis method :

and the proper text is shown in the TextArea.

The method is called again with different values, but the boolean it returns is not stored separately, but used on-the-spot, so to speak :

=========

NOTE

=========

Don't write


public void Language

by mistake, which would make this code block A METHOD , NOT A CONSTRUCTOR. Some compiler versions don't see this as a mistake.

=========

We implement the method raise as an example only, because such a method exists in the Java lib already :
  ... =  Math.pow ( .. , .. )

.