|
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.
| 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 | 8 | 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 | Uses at bits to represent 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 | 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 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) Previous Page |Next Page
|