Home arrow Java & J2EE arrow 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.

  1. The JSP Files (part 2): Attack Of The Killer Fortune Cookies
  2. Flavour Of The Month
  3. Do It Or Else...
  4. Cookie-Cutter Code
  5. Lunch In Milan
  6. Switching Things Around
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 4
February 22, 2001

print this article


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:

<% // define variables String alpha = "abcdef"; String beta = "zyxwvu"; // compare strings out.println(alpha.compareTo(beta)); %>
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.


a = 47;
assigns the value 47 to the variable a, while

a == 47
tests whether the value of a is equal to 47.

>>> More Java & J2EE Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: