eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

Introduction to Web Programming
Variables  
  • You'll recall from our discussion of Perl yesterday that one of the foundations of programming is the use of variables.

  • Variables allow you to assign a bit of information to a space in memory and allow you to manipulate that data over the life of your program.

  • You'll recall that in Perl, we handle variable assignment with something like the following

    $age = 28;

  • As in Perl, Java has support for variables. However, in Java, the syntax is quite a bit different and there are a few more issues you need to deal with, than simply saying

    variable name = variable value.

Naming Variables

  • In Java you can name your variablesanything you like so long as they are not a "Java Keyword" and contain only characters within the set of Unicode characters. However, a good practice is to use characters within the ranges of "A-Z, a-z, 0-9, or _".

  • As we said yesterday, variable names should help you understand what is happening in your program. Thus, it is useful to name your variables intelligently such as firstName.

  • Notice that we created a variable firstName in which the first word was lower case, the second word began with an uppercase letter and there were no spaces. In Perl, this may have been $first_name. This is pretty standard practice and is a good habit for you to get into. Also, many developers use an underscore to prefix private variables such as "_adminName".

Data Types

  • Since Java is a "strongly typed language" the type of every variable must be declared.

  • For example, in order to perform the variable assignment shown above, we would first need to tell the Java compiler that the variable named "age" should expect to hold an integer.

  • The reason for typing is so that Java can manage the usage of the computer's memory when a Java program is running. Each type of variable is allotted a different amount of memory depending on how much it needs. A two-digit integer for example, takes up much less space than a 12-digit decimal. Thus, when we are typing a variable, what we are really doing is telling the computer how much memory to make available for that variable's value. The benefit of strong typing is that Java code can be easily ported from one machine to another. However, you should be aware that if you choose to type a variable one way, and then assign to it a value which it cannot hold, the value will be truncated to fit in the variable and your program will produce incorrect results.

  • In Java, there are eight types of variables: int, short, long, byte, float, double, char, and boolean.

  • Let's look at each one of these so we can better understand what they are.

TypeMemory in BytesComments
int4An integer between -2,147,483,648 and 2,147,483,647. This is the most commonly used integer type because how often are you counting values over 2 billion?
short2An integer between -32,768 to 32,767. If your variable will be bounded, using a short instead of an int is a good way to save memory.
long8An integer between -9,223,372,036,854,775,808L to 9,223,372,036,854,775, 807L.If you are counting numbers that large, you must be working for NASA or the accounting Dept. for Congress.
byte1Uses at bits to represent a number from -128 to 127
float4float is used to represent integers with fractional parts such as 12.3456. Valid values span 6-7 decimal digits
double8A double works like an even more precise float. Valid values span 15 decimal digits. In most cases, you will use a double instead of a float since the memory use is not usually too burdensome and the precision is quite a bit better.
char2The char type is used to represent single characters between single quotes using Unicode encoding.
boolean1This type of variable can be either true or false.

Declaring Variables

  • If you want to use a variable, you must specifically declare its type. To declare a variable's typeyou simply use the type followed by the variable name. Consider the following examples

    byte b;
    short age;
    long nationalDebt;
    boolean isMale;

  • You can also declare multiple variables of one type in one expression such as in the following example:

    int age, yrsEmployed, numChildren;

Variable Assignment and Initialization

  • Once you have declared the type of a variable, you are free to initialize it and assign to it some value.

  • Assignment and initialization works just as they did in Perl. You simply use variable name = some value. For example, consider the following code:

    int age;
    age = 28;

  • Of course you can also declare variables and assign values to them at the same time using the following syntax:

    int age = 28;

Casting (Changing from one type to another)

  • What if you want to multiply 2 x 1.5,Or more generically, int x double? Will the result be an int or a double or something else?

  • Well, when in doubt, Java will convert to the less restrictive type to be safe. Thus, in the above example, the result will be a double since double is less restrictive.

  • The following chart shows how types will be caste if possible.

    byte --> short --> int --> long --> float
         --> double

  • However, what if you are going the other way? Suppose you have two doubles and you want to make an int out of the product

  • To do this type of caste, you simply perform an assignment using the type to be casted to in parentheses before the value to be casted. Consider the following example in which we caste from a double to an int:

    double d = 123.456;
    int    i = (int) d;     

  • In this case, it will be assigned a value of "123".

Previous Page |Next Page