|
|
||
|
Home | Hindi | Kabir | Poetry | Workshop | BoloKids | Writers | Contribute | Search | Contact | Share This Page! Shop Online |
|||
|
Computing |
Java Declaring Variables 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, '\0' for characters, and false for booleans). Points on Variable Names
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:
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. –
Deepak Chandrasekaran By Deepak Chandrasekaran JAVA: A Beginner's Guide Oops... I did it Again! Objects and Classes First Java Application Your First Java Applet Statements and Expressions Variable Type & Declaration Variables and Strings Arrays and Overloading in JAVA
By Neeraj Mathur
By Ruchi Gupta
Computing |
|
|
|
|
Analysis |
Architecture |
Astrology |
Ayurveda |
Book Reviews |
Buddhism |
Cartoons | Cinema |
Computing |
Culture |
Dances |
|
Home | Hindi | Bolography | BoloKids | Kabir | Poetry | Quotes | Workshop | Writers | Contribute | Search | Contact |
|
|