Skip to main content

Variables

Declaring Variables
All variables must be declared before they can be used.
How to declare a variable:
1.  Choose the type you need.
2.  Choose a name for the variable.
3.  Use the following format for a declaration statement:
               datatype variable identifier; 
4.  You may declare more than one variable of the same
     type by separating the variable names with commas.
                int age, weight, height;
5.  You may initialize a variable (place a value into the
     variable location) in a declaration statement.
                double mass = 3.45;



When you declare a variable, Java reserves memory locations of sufficient size to store the variable type. The actual data values will be stored in these memory locations.

Variables that are not initialized are NOT empty.  If you do not initialize your variables, they will contain junk ("garbage") values left over from the program that last used the memory they occupy, until such time as the program places a value at that memory location.  
Constant Variables

Using final you can define variables whose values never change.  You MUST place an initial value into such a "constant" variable.  If you do not place this initial value, Java will never let you assign a value at a later time because you cannot do anything to change the value of a final (constant) variable.
final int ageLimit = 21; // this value cannot be changed
Java will allow you to declare a variable anywhere in the program as long as the variable is declared before you use it.
A good programming practice is to declare variables near the top of the program.  Declaring variables in this manner makes for easier readability of the program.

Comments

Popular posts from this blog

java defination

About the java Technology java technology is both a programming language and a platform. The Java Programming Language  the java programming language is a high-level language that can be characterized by all of the following buzzword. Simple Object oriented Distributed Multithreaded Dynamic portable High performance Robust Secure       In the java programming language,all source code is first written in plain text files ending with the .java extension.Those source files are then compiled into .class  by the java compiler.  a .class file does not contain code that is native to your processor.it's contain the bytecode - the machine language of the java virtual Machine (java VM).  Because the java virtual machine is available on many different operating system,the same .class files are capable of running on microsoft Windows,Linux or MAC. java Program class hello { public static void main(string args[]) { System....