Java Reference
In-Depth Information
applets via the applet tag parameters. An applet hypertext tag includes the
param
sub-tag:
<applet...>
<
param name
="
string1
"
value
="
string2
">
<
/applet
>
The
name
parameter provides an identifier for the string passed in the
value
parameter to the applet program. For example, we could pass two numbers as
follows:
<
applet code
="
MyApplet
"
width
="
100
"
height
="
50
">
<
param name
="
fpNumber
"
value
="
12.45
">
<
param name
="
intNumber
"
value
="
10
">
<
/applet
>
To obtain the parameter values the Applet class provides this method:
String getParameter (String paramName);
The parameter returns as a string from this method so we need to convert it to a
numerical primitive type value. The wrapper classes provide tools for this in the
form of static methods. (Since they are static, they can be called without having an
instance of the wrapper class.) This is illustrated in the following example code:
public void init () {
string fpStr
=
getParameter (
"
fpNumber
"
);
double fpNum
=
Double.parseDouble (fpStr);
String intStr
=
getParameter (
"
intNumber
"
);
int intNum
=
Integer.parseInt (intStr);
....
Here the
getParameter (String)
method returns the string value for
the
fpNumber
parameter, and the static method
Double.parseDouble
(String)
from the
Double
wrapper class converts it to a
double
value.
Similarly, we get the integer parameter using the
parseInt (String)
static
method from the
Integer
class.
The
parseInt()
method has been available since Java 1.0, but
parseDouble()
only appeared with Java 1.2. Previously, the
valueOf()
method was used to return a
Double
value, which in turn could provide the
double
primitive value using the
doubleValue()
method:
double fpNum
=
Double.valueOf(fpStr).doubleValue();
Search WWH ::
Custom Search