Saturday, 3 January 2015

Java Local Variables

Declaration

Declaration of local variables is similar to C/C++. A local variable can be declared anywhere in a method or block and can be initialized during declaration. It is not must to initialize a local variable during declaration but it must be initialized in the same block in which it is declared before the first use. More than one variables of the same type can be declared using single declaration statement by separating the variable names with comma.

Examples:

long x, y, z;
float d;
int a=0, b=10, c=5;

Example: The following program demonstrates the declaration and use of local variables.

Step 1: Open a new file named Local.java and type the following Java Program

class LocalVarDemo{
   
     public static void main(String args[]){
          int num1;
          int num2 = 10;
          int sum, product;
          float f1 = 3.5f; f2 = 6.9f, sumf;

          num1 = 15;
          sum = num1+num2;
          System.out.println("Sum of two integer values = " + sum);
         
          sumf = f1+f2;
          System.out.println("Sum of two floating point values = " + sumf);
     }
}

Step 2: Compile above Java program using command javac Local.java in the command window.

Step 3: Execute the program using command java LocalVarDemo in the command window.

The output of the program will be:

          Sum of two integer values = 25
          Sum of two floating point values = 10.4

Java Variables

Variables are the names of the memory locations that can store values. A variable must be declared before we can store value in it.

Java has three kind of variables:


  • Local variables
  • Instance variables
  • Class variables
Local variables are used inside blocks or methods. Instance variables are used to define attributes or state of an object. Class variables are used to define attributes/state, which is common for all the objects of a class.

All the three types of variables are declared in similar manner but use of class and instance variables differ from the use of local variables. There are also differences in terms of the modifiers (e.g. visibility / access modifiers), which can be used with the variables.

Java Character Data Type

Java uses data type char to store individual characters. Java characters are encoded using 16-bit Unicode character set. The char data type is unsigned and the range of values that can be stored in a char vary from 0 to 65535.

The first 128 characters of the Unicode set are the same as the 128 characters of 7-bit ASCII character set and the first 256 characters of the Unicode correspond to the 256 characters of the extended ASCII (8-bit ISO Latin-1) character set.

Java characters can also be used in integer expressions. The Unicode value of the character is used when it is part of an integer expression.

Java Boolean Data Type

Java has a data type boolean. It is used to store boolean values true and false. You can not use zero to represent false or a non-zero value to represent true like C/C++. Boolean values in Java can not be treated like integers and vice-versa.

The size of the boolean data type is undefined but irrespective of the internal storage boolean data type is used to hold just true and false. Boolean values are produced by all relational, conditional and boolean logical operators and are primarily used to govern flow of control during program execution.

Java Floating data types

Floating point data types are similar to C/C++. They are used to store the real numbers. There are two floating-point data types in Java.


Data type Size Range (Absolute value)
float 4 bytes / 32 bits 1.401298464324817E-45f to 3.4028234663852886E38f
long 8 bytes / 64 bits 4.9E-324d to 1.7976931348623157E308d