Loading

Java Tutorial Hub

Training & Tutorials for Java Beginners

Java Tutorial 9 - Variables

 

A variable can be thought of as a container which holds value for you during the life of your program.

Every variable is assigned a data type which designates the type and quantity of a value it can hold.

 

 

The different Data Type are ---

Integer data types

byte (1 byte)

short (2 bytes)

int (4 bytes)

long (8 bytes)


java-varaibles

Floating Data type

float (4 bytes)

double (8 bytes)

Textual Data Type

char (2 bytes)

Logical

boolean (1 byte) (true/false)

 

Points to Remember:

  • All numeric data types are signed(+/-).
  • The size of data types remain the same on all platforms (standardized)
  • char data type in Java is 2 bytes because it uses UNICODE character set. By virtue of it, Java supports internationalization. UNICODE is a character set which covers all known scripts and language in the world

 

To use a variable in your program you need to perform 2 steps

  1. Variable Declaration
  2. Variable Initialization

 

1) Variable Declaration

To declare a variable , you must specify the data type & give the variable a unique name.

Vriable-Type-Name-Declaration

Examples of other Valid Declarations are

    int a,b,c;

    float pi;

    double d;

    char a;

 

2) Variable Initialization:

 

To initialize a variable you must assign it a valid value.

java-variable-initialization

Example of other Valid Initializations are

  pi =3.14f;

  do =20.22d;

  a=’v’;


You can combine variable declaration and initialization.

java-variable-initialization-declaration-combined

Example :

    int a=2,b=4,c=6;

    float pi=3.14f;

    double do=20.22d;

    char a=’v’;

Variable Type Conversion & Type Casting

 

A variable of one type can receive the value of another type.Here there are 2 cases -

 

case 1) Variable of smaller capacity is  be assigned to another variable of bigger capacity.

   java-type-conversion

This process is Automatic, and non-explicit  is known as Conversion

 

case 2) Variable of larger capacity is  be assigned to another variable of smaller capacity

java-type-cast-operator

 

In such cases you have to  explicitly specify the type cast operator. This process is know as Type Casting.

In case, you do not specify a type cast operator, the compiler gives an error. Since this rule is enforced by the compiler , it makes the programmer aware that the conversion he is about to do may cause some loss in data and prevents accidental losses.

 

Assignement: To Understand Type Casting

Step 1) Copy the following code into an editor.

class Demo{
 public static void main(String args[]){
 byte x;
 int a=270;
 double b =128.128;
 System.out.println("int converted to byte");
 x=(byte) a;
 System.out.println("a and x "+ a +" "+x);
 System.out.println("double converted to int");
 a=(int) b;
 System.out.println("b and a "+ b +" "+a);
 System.out.println("\n double converted to byte");
 x= b;
 System.out.println("b and x "+b +" "+x);
 }
}

 

Step 2) Save, Compile & Run the code.

Step 2) Error =? Try to debug. Hint - Typecasting is missing for one operation.

 

 

                                                         Back to Java Tutorials Index Page
 
 
Joomla 1.5 Templates by Joomlashack