Monday, 3 December 2012

Declaring String Variables

A String is composed of words and characters and must be enclosed in quotation marks.A String is an object that has its own class.The String class provides methods for string manipulation.

Simple Program Using Strings:


<html>
<head>
<title> Simple JSP page </title>
</head>
<body>
<h3>Using the Declaration tag declare page level variables: </h3>
<p>
<%!
String myname;
String surname="Manikandan";
String Email,Street,City;
%>
<p>
<b>Notes:</b>The Declaration tag does not produce any output.
<p>
<%
myname="Saranya";
%>
My Name is <%=myname%> <%=surname%>
</body>
</html>

Description:

  • Type <%! to open the Declaration tag.
  • Type the String class name followed by the variable name and a semicolon.This initializes a single variable as a string object.(String myname;)
  • Use = to assign a value to the variable when it is initialized.This is explicit initialization.(String surname="Manikandan";)
  • The value assigned to a String must be enclosed in quotation marks.
  • You can initialize more than variable of same type by separating with a comma.(String Email,Street,City;)
  • Type %> to close the Declaration tag.
  • Type = to assign a value to a variable.The value must be placed on the right side of the equals sign. (myname="Saranya";)
  • Use the expression tag to print the value of a variable.(<%=myname%> <%=surname%>)


***************************OUTPUT**********************************************

Using the Declaration tag declare page level variables:

Notes:The Declaration tag does not produce any output.
My Name is Saranya Manikandan

*********************************************************************************

No comments:

Post a Comment