HomeJava & J2EE Page 2 - The JSP Files (part 2): Attack Of The Killer Fortune Cookies
Flavour Of The Month - Java
The second part of our introductory JSP tutorial discussesarithmetic, logical and comparison operators, together with simple examplesand illustrations. You'll also learn the basics of JSP's numerousconditional expressions, including the "if", "if-else" and "switch"statements, and find out a little more about the String object.
And just as you can compare numbers, JSP also allows you to compare strings, with a couple of very useful String object methods.
First, the equals() method allows you to check whether the value of a particular string variable matches another. The following example should demonstrate this.
<%
// define variables
String myFavourite = "chocolate";
String yourFavourite = "strawberry";
// compare strings
if (myFavourite.equals(yourFavourite))
{
out.println("A match made in heaven!");
}
else
{
out.println("Naw - try again!");
}
%>
Try changing the values of the variables to match each other,
and gasp in awe as the output changes.
In case the equals() method doesn't appeal to you, JSP offers you a choice in the form of the compareTo() method, which returns a value indicating which of the two strings is greater. Take a look:
In this case, if the value of the variable "beta" is greater
than that of the variable "alpha", the compareTo() method will return a negative integer; if it's the other way around, the comparison will return a positive integer. And if the two strings are identical, the comparison will return 0.
Incidentally, the comparison is based on both the first character of the string, and the number of characters in the string. One string is considered "greater" than another if the numeric value of its first character is greater, or if its length is greater. In the example above, "z" has a greater numeric code than "a", and so the comparison will return a negative integer. But don't take our word for it - try it yourself and see!{mospagebreak title=Turning Up The Heat} Why do you need to know all this? Well, comparison operators come in very useful when building conditional expressions - and conditional expressions come in very useful when adding control routines to your code. Control routines check for the existence of certain conditions, and execute appropriate program code depending on what they find.
The first - and simplest - decision-making routine is the "if" statement, which looks like this:
if (condition)
{
do this!
}
The "condition" here refers to a conditional expression,
which evaluates to either true or false. For example,
if (hard drive crashes)
{
get down on knees and pray for redemption
}
or, in JSP-lingo.
<%
if (hdd == 0)
{
pray();
}
%>
If the conditional expression evaluates as true, all
statements within the curly braces are executed. If the conditional expression evaluates as false, all statements within the curly braces will be ignored, and the lines of code following the "if" block will be executed.
Here's a simple program that illustrates the basics of the "if" statement.
<%!
// declare temperature variable
int temp = 50;
%>
<%
// check temperature and display output
if (temp > 30)
{
out.println("Man, it's hot out there!");
}
%>
In this case, a variable named "temp" has been defined, and
initialized to the value 50. Next, an "if" statement has been used to check the value of the "temp" variable and display a message if it's over 30. Note our usage of the greater-than (>) conditional operator in the conditional expression.
An important point to note - and one which many novice programmers fall foul of - is the difference between the assignment operator [=] and the equality operator [==]. The former is used to assign a value to a variable, while the latter is used to test for equality in a conditional expression.