Like most of the programming language, Java allows us to store data of different types. Java supports a wide range of variable types. This page focuses on variable types and their behavior. These variables are classes and are refereed to as primitive data types.

Java Integers type includes int, short, byte, long and they store whole numbers, like 3,234,567. Java Floating type includes float, double and they store decimal numbers like 34.5, 56.7 etc. Java character type include char and it stores characters, symbols. 'c', 'g', '#' etc. Java boolean type is bool, and it stores just 2 values, TRUE or FALSE.

Let us look at the below example program, to understand their usage.

 public class VariableTypes {
     public static void main(String[] args) {
                                     /* Declaring a float variable. 
         float var1 = (float) 11.3;   * We will see more about typecasting in the next section.
         int var2 = 3;      	   // Declaring an int variable.
         float var3;
         var3 = var1*var2;
         System.out.println("Var1 * Var2 ( " + var1 + " * " + var2 + " ) is  "  + var3);

Compile/Run this program and here is the output:

 Var1 * Var2 ( 11.3 * 3 ) is  33.900000000000006

Let us notice the difference in the output, when the data type is changed from float to double.

 public class VariableTypes {
     public static void main(String[] args) {
         double var1 =  11.3; // Declaring a double variable.
         int var2 = 3;       // Declaring an int variable.
         double var3;

         var3 = var1*var2;
         System.out.println("Var1 * Var2 ( " + var1 + " * " + var2 + " ) is  "  + var3);

Compile/Run this program and here is the output:

 Var1 * Var2 ( 11.3 * 3 ) is  33.9

Looking at the output, you can make out the difference in the precision between float and double. Similar difference would be seen in byte, int, short and long. The below example demonstrates the usage of different data types.

 public class VariableTypes {
     public static void main(String[] args) {
         boolean var1 = true; // Declaring a bool variable.
         short var2 = 3777;       // Declaring an short variable.
         char ch = 'h';
         byte var3 = 44;
         long var4 = 456789042;

         System.out.println("Var1:" + var1 + ", Var2: " + var2 + ", Var3: " 
                                 + var3 +  ", Var4: " + var4 + ", ch:"  + ch);

Compile/Run this program and here is the output:

 Var1:true, Var2: 3777, Var3: 44, Var4: 456789042, ch: h

Please note that if you assign a higher value for var2 ( which is of type short), the compiler throws an error. for instance, var2 = 37772; throws an error. 37772 is not in the range of short. Let us look at the ranges of each data type.

Table: Variable Types/Ranges in Java
Data TypeRangeSize
int-2^31 to 2^31 -132 bits
short-32,768 to 32,76716 bits
byte-128 to 1288 bits
long-2^63 to 2^63 -164 bits
floatSingle precision decimal numbers32 bits
doubleDouble precision decimal numbers64 bits
char0 - 65,53516 bits
booltrue or falseNot Precisely defined

At this point, you might be wondering, what's the need for different variable types, to store similar data values. Like, short and int for storing whole numbers; float and double for decimal numbers. Here is the reason, all of them differs in the size, precision and the range of the data they store.

Scope of the variable

As you notice, so far we have declared all the variables, at the start of the main function. Java allows us (Unlike, C Programming language which requires all the variables to be declared at the start of the function) to declare a variable as and when you need it. This brings in the concept "Scope of the variable". In general, a variable is visible to only certain block of a program. A variable declared at the start of the function is visible through out that function, but not to other functions (This is what we have seen so far).

 void function () {
     int y;

     if (condition) {  // Scope of this blocks starts here. 
                       // What ever defined in this block is visible only to this block.
         int x = 10;  // x is declared inside this "if block".
         y = x;
     } // Scope of the block ends here.

     int z = 5;  // This statement throws an error, because x is not visible to this block of the code.

As show in the above code snippet, a variable can be declared any where in the function. For example, variable "z" is declared at the end of the function and variable x is defined inside an if block, whose scope is limited only to that block.

Type Casting

We can copy a variable of type t1 to a variable of type t2, if t1 and t2 are of same type and t1 and t2 are compatible. However if t2 is larger than t1 and we try to copy, then the compiler may not be happy with that, for such cases you would need to use casting before the copying.

 int y = 20;
 long x = y; // x and y are of different types and hence typecasting is done here.
 	     // Since int and long are compatible, internal typecasting is performed by the compiler itself.

 long x;
 x = (long) y; // Though typecasting is not required, explicit typecasting can be done.

 int x = 20;
 byte y;
 y = x;  // This throws an error, compiler cannot do the implicit conversion because byte and int are not compatible. 
 y = (byte)x; // This requires an explicit conversion.

 double d =  235.64;
 float f;
 x = (int)d; // Type casting double to int.
 f = (float)d; // Type casting double to float.

 char ch = 'a';
 x = (int) ch; //  Int and char can be typecasted too. This does not throw an error, but X value would not be 'c' 
 	      // instead it would be ASCII value of 'a'.

Like shown in the above code snippet, typecasting can be done when the variables are of different types. Sometimes, implicit typecasting and some other times, typecasting needs to be done explicitly.

Variable operators

Besides simple operators to do addition and multiplication, Java provides additional arithmetic operators as well: subtraction using "-", division using "/", modulo using "%" etc. We should note that the modulo operator has an important constraint: both of the variables should be integral (integer, short, char) and not float or double.

Java also provides a set of operators that operate the value of a variable. For example, if we were to increase the value of "var1" by 10, we could do "var1 = var1 + 10" or more crisply, "var1 += 10". Likewise, if we were to multiply the value of "var1" by 10, we could do "var1 = var1 * 10" or "var1 *= 10". Same rule applies to several other operators like subtraction "-"), division ("/"), modulo ("%"), bit-shifts ("<<" or ">>"), logical and ("&"), logical or ("|"), etc.

Another set of handy operators are the unary operators: "++" and "--", these operators increment and decrement an integer variable by 1 respectively. Thus, "var1 = var1 + 1" is same as "var1++". Likewise, "var1 = var1 -1" is same as "var1--".

However, we should note that the above unary operations can be applied either before or after a variable and accordingly, it can have different meanings. For example, "var1++" means that we use the variable first and then increment it, where as, "++var1" means we increment the variable first and then use it. Thus, "var2 = ++var1" and "var2 = var1++" mean different assignments to var2. It is a bad idea to have complicated expressions involving these operators; overzealous usage of these operators can create confusing statements that will reduce code-readability!

Here is the list of the operators available in Java.

Table: Common Operations in Java
+, -, *, /Arithmetic operations
%Modulus operation
==, &&, ||, >=, <=Relational Operation
++, --Increment, decrement operations
*=, +=, -=, /=Arithmetic operations along with assignment
~, &, |, <<, >>, ^Bitwise operations

comments powered by Disqus