Variables 

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 variables anything 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 _". 
  • 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 defining 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 defining 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 defining 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, depending on the type of variable CLASH you will get a compile or runtime error. 
  • 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.
Type Memory in Bytes Comments
int 4 An 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?
short 2 An 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.
long An 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.
byte 1 Represents a number from -128 to 127
float 4 float is used to represent integers with fractional parts such as 12.3456. Valid values span 6-7 decimal digits
double 8 A 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.
char 2 The char type is used to represent single characters between single quotes using Unicode encoding.
boolean 1/8th - i.e. 1 bit This 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 type you 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 cast 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 cast, 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 cast 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".
Setting Your Development Environment
Table of Contents
Strings


Graphics & Media Lab. >> Библиотека | Курсы | Графикон

Hosted by Graphics & Media Lab
http://graphics.cs.msu.su
lab_logo
mailto: Laboratory