Java supports three kinds of variables: instance variables, class variables, and local variables. Instance variables are defined when you create the class and apply it to all instances (objects) made from that class. Class variables are also defined when you create the class, but they are global to the class. Local variables are defined within a method or other code block and can be used only within that code block. This type of variable definition really defines the scope of the variable and where it can be used.
Variables can basically be declared two ways: via a primitive declaration or by typing the variable after a class. Table 7.1 outlines the basic data types of the Java language. To see how Java data types compare to ABAP (Advanced Business
Application Programming) data types, please refer to Appendix C, 'ABAP/4 Data Types to Java Cross-Reference.'
Table 7.1 Basic Data Types for Variables in Java
Type | Size | Description |
---|---|---|
byte | 8 bits | +/- 2^7 integer values |
short | 16 bits | +/- 2^15 integer values |
int | 32 bits | +/- 2^31 integer values |
long | 64 bits | +/- 2^63 integer values |
float | 32 bits | Single-precision floating-point |
double | 64 bits | Double-precision floating-point |
boolean | 1 bit | True or false |
char | 16 bits | Single Unicode character |
If you are going to type a variable as a class, the class must obviously be defined either as part of standard Java (such as string in the java.lang package) or in your own program.
The standard convention in Java is to start all variable names with a lowercase letter and all class names with an uppercase character. Variable names should be descriptive enough to indicate what the variables contain. Naming a variable 'i' forces people to read through your code to determine the variable's contents, whereas naming the variable 'rowsSelected' gives the reader at least some idea of the contents. When variable names contain multiple words, by convention each word begins with an uppercase character (except for the first one). Here are some examples:
isEnabled totalAmountSpent abortDueToError
Variable names must also follow these rules:
Names must start with a letter, an underscore (_), or a dollar sign ($). Variable names cannot start with a number.
Names can contain a valid Unicode character. Unicode characters comprise all standard character sets plus many others that can represent languages such as Greek, Russian, and Hebrew.
Names cannot be the same as a Java keyword.
Names cannot be the same as another variable in the same scope. Hence you cannot have two class variables named isEnabled (provided that both variable data types are the same); you can, however, have a class variable named 'isEnabled' and a local variable named 'isEnabled'.
Variables are defined by putting either the primitive data type or class first, giving the variable name, and then (optionally) adding the initialization value. Following are some examples:
int numberOfAccounts; String interfaceName = "Finance_Inbound"; boolean applyDiscount = false;
You can also declare multiple variables of the same type in a single statement by separating the variable names with commas, as shown here:
String person1 = "Joe", person2 = "John";
It is always a good practice to assign an initial value to a variable. In Java it is mandatory to do so with local variables. If you have a class or instance variable, the following default values are used if the variable is not initialized before being used.
Booleans-False
Characters-\0
Integer and floating-point-0
Team-Fly |