Beginner’s Guide to Web Development – Java Basics

This section's goal is to give you the information you need in order to write basic Java code. Understanding Java is an important part of JSP development. It gives you the ability to write more complex web pages by utilizing the power of the Java programming language in your JSP code.

One of the first things you need to learn before you can write functional Java programs is the concept of data types. Java has eight built-in data types referred to as "primitive data types". These are the following:

boolean
char
byte
short
int
long
float
double

Java also offers other data types in its standard libraries that are available for your use. For example, without doing any extra work on your part, you can use the following commonly used data types:

String
array

You will often here about objects when reading about the Java programming language. As a general definition, any data type in Java that is not a primitive data type is an object. Thus, the two data types mentioned above - array and String - are both objects.

Java also offers what are known as wrapper classes. These can be used to treat primitive data types as objects. You will see the importance for this in the sample application later on. There is an object wrapper available for each of the primitive data types. The wrappers are the following:

Boolean
Character
Byte
Short
Integer
Long
Float
Double

An understanding of the first ten data types mentioned above along with the eight wrapper classes will give you the ability to write some very effective Java code. Before we move on, please not that case does matter when programming in Java. In other words, "Character" is not the same as "character".

To use a data type, you first need to declare a variable of that type. The following is an example of a declaration:

int x = 7;

The form that a declaration takes is the following:

[type of data type] [name of variable] = [initial value];

Notice that x was declared and given a value on that same line of code. You can also split the declaration and assignment of a value into two parts. For example,

int x;
x = 7;

Once you declare a variable, you can use it in other parts of your code. Take a look at this example:

int x = 7;

x = x + 1;

What does the above section of code do? It simply increments x by 1. Thus, x is now equal to 8.

In order to make sure you are using data types correctly, we are going to go over each of the first ten mentioned above in detail. We will list the data type, the allowable values, and give an example of its declaration.

boolean

a boolean can contain values of true or false

boolean bool = true;
boolean bool2 = false;

char

a char can contain either an integer value or a character literal. A character literal is referred to by using a character with single quotes surrounding it, i.e. 'a'.

char c = 'y';
char c1 = 65;

byte

a byte is an 8-bit integer representation. Thus, the minimum value for a byte is -128. The maximum value is 127.

byte b = 4;

short

a short is a 16-bit integer representation. Thus, the minimum value is -32768 and the maximum value is 32767.

short s = 544;

int

an int is a 32-bit integer representation. The minimum value is -2147483648 and the maximum value is 2147483647.

int i = 44555;

long

a long is a 64-bit integer representation. To ensure a long value, a capital "L" should be appended to the declaration.

long x = 233333L;

float

a float is a 16-bit decimal representation. To ensure a float value, an 'f' should be appended to the declaration.

float f = 1.44f;

double

a double is a 32-bit decimal representation.

double d = 3.14;

String

A String is a representation of one or more characters grouped together inside of quotation marks.

String s = "hello";

array

An array is an ordered collection of data types. These data types can be either primitives or objects. The syntax for declaring an array depends on that data type. Arrays need to be declared with the "new" keyword and need to be given a size for their storage.

To declare an array of ints, do the following:

int[] intArray = new int[10];

To declare an array of Strings, do the following:

String[] strings = new String[20];

Once you have created the array, you can store data into the array or retrieve data from the array. For example, above we declared an array of integers of size 10. To enter an integer in the first cell of this array, you would do the following:

intArray[0] = 44;

Notice that we referred to the first element as element "0". This is because references to array cells start at zero. Thus, the 10th cell in the above array would be referred to as follows:

intArray[9] = 55;

To reference data located in the array, we could do the following:

int i = intArray[0];

In the above case, i would be equal to 44.

Object Wrappers for Primtives

We are now going to go over some examples using the object wrappers for the primitive data types. Often-times, the reason these wrappers are used is to convert String objects to a primitive data type. For example, let's say your web page was passed a piece of numeric data, such as a price, in the form of a String. In order to perform calculations on that data, you need to convert it to the appropriate data type. In the case of a price, the appropriate data type is probably a double or a float because of the decimal point. Here's an example that shows this:

String price = "43.39";
Double dval = new Double(price);
double d = dval.doubleValue();

The above snippet of code converts the String value of 43.39 into a double value. Notice the use of the new operator in the example above. This is a special word that we will cover below. Here's an example using Integer.

String number = "44";
Integer ival = new Integer(number);
int i = ival.intValue();

The "new" operator

The new operator handles allocating memory when initializing Java objects. Because primitives are not Java objects, the "new" keyword does not need to be used when initializing them. There is also one object that does not need the "new" operator when being initialized. This is the String object. Use of the String object is shown in an example earlier in this section.

This concludes our coverage of Java basics. Now we are ready to move on to programming basics.