Nov 26, 2024
Nov 26, 2024
In the last article we saw what exactly are the statements and expressions and how they are written. We also saw as to what are three kinds of variables used in Java like instance variables, class variables, and local variables. Lets now see as to how to declare a variable and what are the various variable types.
To use any variable in a Java program, you must first declare it. Variable declarations consist of a type and a variable name:
int myAge;
String myName;
boolean isTired;
Variable definitions can go anywhere in a method definition (that is, anywhere a regular Java statement can go), although they are most commonly declared at the beginning of the definition before they are used:
public static void main (String args[ ] ) {
int count;
String title;the
boolean isAsleep;
...
}
You can string together variable names with the same type:
int x, y, z;
String firstName, LastName;
You can also give each variable an initial value when you declare it:
int myAge, mySize, numShoes = 28;
String myName = "Laura";
boolean isTired = true;
int a = 4, b = 5, c = 6;
If there are multiple variables on the same line with only one initializer (as in the first of the previous examples), the initial value applies to only the last variable in a declaration. You can also group individual variables and initializers on the same line using commas, as with the last example, above. Local variables must be given values before they can be used (your Java program will not compile if you try to use an unassigned local variable). For this reason, it's a good idea always to give local variables initial values. Instance and class variable definitions do not have this restriction (their initial value depends on the type of the variable: null for instances of classes, 0 for numeric variables, ' ' for characters, and false for booleans).
Points on Variable Names
Variable names in Java can start with a letter, an underscore (_), or a dollar sign ($).
They cannot start with a number.
After the first character, your variable names can include any letter or number.
Symbols, such as %, *, @, and so on, are often reserved for operators in Java, so be careful when using symbols in variable names.
In addition, the Java language uses the Unicode character set. Unicode is a character set definition that not only offers characters in the standard ASCII character set, but also several million other characters for representing most international alphabets.
Java language is case-sensitive, which means that uppercase letters are different from lowercase letters. This means that the variable X is different from the variable x, and a pen is not a Pen is not a PEN. Keep this in mind as you write your own Java programs and as you read Java code other people have written.
By convention, Java variables have meaningful names, often made up of several words combined. The first word is lowercase, but all following words have an initial uppercase letter:
Button theButton;
long reallyBigNumber;
boolean currentWeatherStateOfPlanetXShortVersion;
Variable Types
In addition to the variable name, each variable declaration must have a type, which defines what values that variable can hold. The variable type can be one of three things:
One of the eight basic primitive data types - The eight primitive data types handle common types for integers, floating-point numbers, characters, and boolean values (true or false). They're called primitive because they're built into the system and are not actual objects, which makes them more efficient to use. Note that these data types are machine-independent, which means that you can rely on their sizes and characteristics to be consistent across your Java programs.
The name of a class or interface
An array
There are four Java integer types, each with a different range of values (as listed in Table A below). All are signed, which means they can hold either positive or negative numbers. Which type you choose for your variables depends on the range of values you expect that variable to hold; if a value becomes too big for the variable type, it is silently truncated.
Table A Integer types.
Type Size Range
byte 8 bits —128 to 127
short 16 bits —32,768 to 32,767
int 32 bits —2,147,483,648 to 2,147,483,647
long 64 bits —9223372036854775808 to 9223372036854775807
Floating-point numbers are used for numbers with a decimal part. Java floating-point numbers are compliant with IEEE 754 (an international standard for defining floating-point numbers and arithmetic). There are two floating-point types: float (32 bits, single-precision) and double (64 bits, double-precision).
The char type is used for individual characters. Because Java uses the Unicode character set, the char type has 16 bits of precision, unsigned.
Finally, the boolean type can have one of two values, true or false. Note that unlike in other C-like languages, boolean is not a number, nor can it be treated as one. All tests of boolean variables should test for true or false.
In addition to the eight basic data types, variables in Java can also be declared to hold an instance of a particular class:
String LastName;
Font basicFont;
OvalShape myOval;
Each of these variables can then hold only instances of the given class. As you create new classes, you can declare variables to hold instances of those classes (and their subclasses) as well.
Remember that Java does not have a typedef statement (as in C and C++). To declare new types in Java, you declare a new class; then variables can be declared to be of that class's type.
In the next article we will see as to how to assign values to variables.
23-Sep-2001
More by : Deepak Chandrasekaran