Variable and Datatype in Java

Variable and Datatype in Java
Data type determines the kind of the value that can be stored in a variable. While declaring variables you must have to specify the Data Type for those variables to instruct compiler how much memory to be allocated for each variable and which kind of the value can be stored in each variable.

Type checking is performed strictly in Java. For example in C and C++ you can assign floating point value to an integer variable while in Java you can not do so.

In JAVA basic Data Types are divided into following four categories:

(1. Integer:

  • This category contains four data types: byte, short, int and long.
  • Following table summarize the four data types with their size in byte and their range.
    Data Types Byte Range
    Byte 1 –128 to 127
    Short 2 –32,768 to 32,767
    Int 4 –2,147,483,648 to 2,147,483,647
    Long 8 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • All the data types mentioned in the above table are signed data type which means they can store both positive as well as negative numbers. In Java there are no unsigned data types.

(2. Floating Point:

  • This category contains two data types: float and double.
  • Following table summarize the two data types with their size in byte and their range
    Data Types Byte Range
    Float 4 1.4e−045 to 3.4e+038
    Double 8 4.9e–324 to 1.8e+308
  • Floating point data type is used to store numbers with decimal points.
  • float data type is known as single precision. It is useful when accuracy is not an issue. It is useful in the situation where better accuracy is not required. 
  • double data type is known as double precision. It is useful when accuracy is an issue. It is useful in the situation where accuracy is important.  

(3. Character:

  • This category contains only one data type called char. 
  • char data type is used to store single character that is enclosed between single quotation mark. 
  • char data type occupies 2 bytes in memory. 
  • The range of a char is 0 to 65,536.
  • Default value for this data type is NULL.

(4. Boolean:

  • This category contains only one data type called boolean. 
  • boolean data type is used to store the value true or false.
  • Default value for this data type is false. 

Variables:

  • A variable is a named location in computer’s memory. When we declare the variable in the program the compiler allocates fixed bytes of memory to that variable at the time of compiling the program and assigns a name to that allocated memory space. 
  • A variable is used to store data temporary during the execution of the program.
  • A Variable has a name and value of specific type.
  • One of the important characteristic of variable is that the value of the variable may change during the execution of the program.

Declaring a Variable:

Like C and C++, in Java, all variables must be declared before they can be referenced in the program.
The general syntax for declaring variable is given below:
DataType VariableName [=value];
Data Type determines which kind of the value can be store in the variable and how much memory to be allocated for that variable.

You can assign initial value for the variable at the time of declaring it. However it is optional.

You can also declare multiple variables of the same data type in the single statement by separating each of them using comma operator.

Consider Following examples:

int a;
int a=5;
float b=3.5;
int a, b, c;
int a=5, b=6, c=7;
Previous
Next Post »

If you have any kind of question about any post, Feel free to ask.You can simply drop a comment below post. Your feedback and suggestions will be highly appreciated. ConversionConversion EmoticonEmoticon