Section 3.5. Variable Assignment and Initialization

   

3.5 Variable Assignment and Initialization

There are a few conventions used in initializing variables , and some rules.

3.5.1 Naming Requirements

When you create a new variable, your naming of the variable must follow these rules:

  • The first character can be a dollar sign ($), an underscore (_), or a letter (A “Z or a “z).

  • Characters following the first character can be any of the above or a numeric character (0 “9).

  • Java is case sensitive ( iscurrent is not the same as isCurrent ).

  • You cannot name a variable using a Java keyword.

Note

ColdFusion is not case sensitive ”Java is!


3.5.2 Java Keywords

The Java keywords, which are reserved and therefore may not be used to, for instance, name variables, are shown in Table 3.6.

Note

Many of the keywords, such as try / catch / throw and switch / case are ColdFusion keywords too. Notice that import is a Java keyword that is also now a keyword in ColdFusion MX. A number of these constructs even work in Java similarly to how they work in ColdFusion.


Table 3.6. Java Keywords

abstract

boolean

break

byte

case

catch

char

class

const

continue

default

  do  

double

else

extends

false

final

finally

float

for

goto

  if  

implements

import

instanceof

int

interface

long

native

new

null

package

private

protected

public

return

short

static

strictfp

super

switch

synchronized

this

throw

throws

transient

true

try

void

volatile

while

 

3.5.3 Naming Conventions

Like many other things in the world, the rules are outnumbered by the conventions. Not only does Java have a number of naming and other coding conventions, they are all followed with real consistency throughout the community. Some of the conventions are concerned with topics we have not yet covered. We have touched on creating a String, however. While our example above shows name = new String("Jeremy"); as the way to create a String (and this is technically correct), one very rarely sees a String created in this manner. It is far more common to see this: String name = "Jeremy"; (I wrote it the long way to demonstrate how objects of all kinds are commonly created, and to just make the distinction between objects and primitive types).

There are some conventions for naming variables:

  • Each variable (identifier) should begin with a lowercase letter.

  • If more than one word is used in an identifier, each subsequent word should begin with an uppercase letter, like this: isCurrent . Identifier words should not be separated with underscores, spaces, or other text.

The above conventions are routinely followed. Other miscellaneous notes about variable naming are as follows :

  • In naming a variable, you can also use any Unicode character that denotes a letter in a language other than English.

  • Constants (examined below) are named using all uppercase letters , and an underscore is used to separate each word.

  • You (obviously) cannot declare two variables of the same name in the same scope.

  • It is legal to put declarations just about anywhere in your code.

  • The length of a variable name is not limited by Java.

There are some conventions regarding variable naming that are perhaps less consistently followed:

  • It is a bad idea to make two variables that differ only in their case. For instance while you can legally create two different variables, called String name; and String Name; , this would be very confusing, so it should not be done.

  • While it is possible to create an object with the same name as the type, this would be very confusing as well, and it should not be done. For example, given an object of type Employee , it is a bad idea to instantiate an object of this type with the variable name employee . Instead, try putting "a," "an," or "my" in front of it; for example: anEmployee . These are all commonly used.

3.5.4 Creating Primitive Variables and Assigning Value

There are two steps to getting a variable assigned. First you must declare it, which means reserving space for it in memory. Then you must initialize it, which means assigning a value to it.

To declare a variable, first write the type of variable you want to create. We have so far only examined primitive types, but it works the same when you start creating variables of objects.

Here is an example of declaration:

 int numberOfEmployees; // a declaration 

This variable has only been declared, it has not been initialized , and Java will tell you so in an error message at compile time if you don't do anything else with it. You need to assign it a value first. Here is an example of assignment:

 numberOfEmployees = 48; 

You can declare multiple variables of the same type at the same time. You just separate the variable names with commas, like this:

 boolean x, y, z; // declares 3 different booleans 

Some programmers find multiple declarations like this confusing; some find it intuitive organization. You might want to create a new class at this time to start testing some of this. Here are a couple more examples:

 char help; // declare  help = 'h'; // initialize double taxOwed; // declare taxOwed = 12643.0; // initialize 

It is also possible to both declare and initialize a variable in the same statement, like this:

 double taxOwed = 12643.0; 

This may also look familiar by now (though a string is not a primitive type):

 String name = "Jeremy"; 

3.5.5 Constants

A constant is a variable whose value does not change. A constant is denoted with the keyword final in the variable declaration. Constants cannot be overwritten. Attempting to assign a value to a constant will generate a compile-time error. As mentioned above, constants are named with all uppercase letters, and underscores separate words in the variable name. Here is an example:

3.5.6 constants.java

 /*  File: chp3.constants.java Purpose: demonstrate constants with final keyword */ public class constants { public static void main(String [] args) { //declare constant var final double CIRCUMFERENCE_OF_EARTH = 24901.5; // print it out to command line     System.out.println("Earth's circumference: " +         CIRCUMFERENCE_OF_EARTH);     } } 

The advantage of using a constant is that the programmer can reference the variable in other parts of the program without having to worry about its value or about accidentally changing its value. Declaring a constant in the manner shown in constants.java is perhaps not terribly useful. While this variable would be a constant, and would thus make it easier to read the program and protect it from being overwritten, it is declared here inside the main method. Constants are generally declared as public, class-level variables; that is, they are accessible by methods defined in other classes.

Note

ColdFusion does not have constant variables that act in this way.



   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net