Programming Tutorials

Data Types and Vairables in Java

By: PriyaBabu in Java Tutorials on 2006-12-15  

 Data types and Variables

            Data types are used to define what kind of information is going to assign to a variable. Java contains two kinds of built-in data types are object oriented and non-object oriented data types. The object oriented data types are defined by a classes. The non object oriented data types are called primitive data types. Java defines the range and the behavior for each data type. However unlike other languages in java three is  no need to specify a size of the data type because of it's portability.

Primitive Data types

    There are eight primitive data types available in Java are segregated in terms of Integers, Floating-Point and Boolean data types.

Integers

    There are four data types to store Integers. They are byte, short, int and long These data types are signed which means that they can hold either positive or negative values.

The range and size of each data type has been given below

Type Size Range
byte 8 -128 to 127
short 16 -32,768 to 32,767
int 32 -2,147,483.648  to 2,147,483,647
long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

        Most commonly used integer type is int. The smallest integer type is byte. It is used to store the raw binary data. The short type is mostly applicable to 16 bit computers.

/*  This is a example for using Integer Data types

    Find the addition of two numbers */

class addition

    {

        public static void main(String args[])

            {

                    int i=8;

                    int j=7;

                    int k;

                    k= i + j ;

                    System.out.println(" The addition of " + i + "and" + j + " is" + k);

            }

}

The output will be

The addition of 8 and 7 is 15

Here in this example the data type int has assigned to the variables i ,j and k. We can also declare the  variables with the same type of data types in the same statement separated by comma. When we need an integer that has a range greater than int, then we should go for long

Floating-Point Types

Floating point numbers are numbers with decimal point. There are two kinds of floating-point types, float and double. The range and size of float and double has given here.

Type Size Range
float 32 1.4e -045 to 3.4e+038
double 64 4.9e-324 to 1.8e+308

Among these two, double is the most commonly used because all of the math function in Java's library use double values. 

/* This is example for using floating-point Data Types

    Find the square root of given numbers    */

class squareroot

    {

        public static void main(String args[])

            {

                   double x,y,z;

                    x=13;

                    y=17;

                    z=Math.sqrt(x+y);

                    System.out.println(" The Square Root is "  +z);

          }

}

The output will be 

The Square Root is 5.477225575051661

    In this example we have used the floating point data type double. Here we have used the new term Math.sqrt(), Math is a Java's built in class and sqrt() is a method in class Math.

Characters

The char type is used for individual characters, such as letters, numbers, punctuation and other symbols. In Java characters are not 8 bit type like they are in other computer languages. Java uses Unicode to represent the characters.  Unicode is a character set that can represent all of the characters found in all human language. So, in Java char type is unsigned 16 bit type range from 0 to 65,536. Since the standard 8 bit ASCII set is subset of Unicode, the ASCII characters are still valid Java characters.

    A character variable can be assigned a value by enclosing the character in single quotes.

For example, the letter x assigns to variable ch as like this

char ch;

ch='x';

    Since char is an unsigned 16-bit type, it is possible to perform the arithmetic manipulations on a char variable.

/* This is a example for using the Data type character

    Doing some arithmetic manipulation in character */

class CharManipulation

    {

        public static void main(String args[])

            {

                    char ch='x';

                    System.out.println(" Now the ch is " +ch);

                    ch++;

                    System.out.println(" Now the ch is " +ch);

                    ch++;

                    System.out.println(" Now the ch is " +ch);

            }

}

The Output will be

Now the ch is x

Now the ch is y

Now the ch is 

In this example the variable ch is declared char variable and it has assigned to the value of 'x'

In the next line we are incrementing the value of ch by 1. So the corresponding Unicode value  will be assigned to ch now. So the letter 'y' will be assigned. In the next line also we are doing the same procedure of incrementing the ch value by 1. So now  the value of ch will be 'z'.        

Boolean Type

The Boolean type holds either true or false using the reserved word true or false. The expression of the variable using boolean datatype will be either true or false.

For example the boolean data type will be assigned as

boolean b;

b=true;

or

boolean b= true;

 Variable declaration in Java

         We can define Variable is a place where information can be stored while program is executing. The value of the variable can be changed at any point in the program.  The syntax of the variable declaration is

type var-name;

where type is the data type of the variable, and var-name is the name of the variable. We can assign any valid data type to a variable. As we had seen before the data type will decide what kind of values can be assigned to a variable. Variables can be declared anywhere in the Java program, only thing is it should be declared before they can be used. As we said earlier when we want to declare several variables with the same data type, we can declare all of them in the same statement. Variable names in Java must start with a letter, a underscore character( _ ) or with a dollar sign. They cannot start with a number.

         There are three kinds of  variables can be declared in Java called as instance variable, class variable and local variable. Here we are going to discuss about the local variables. The local variables can be assigned inside the method or inside the block of statements within a method. All the local variables must be initialized before it is been used.

For Example

class VarInit

    {

        public static void main(String args[])

            {

                      int i=12,j=16,k=30;

                     System.out.println(" The value of i is " + i );

                     System.out.println(" The value of j is " + j );   

                    System.out.println(" The value of k is " + k );

            }

}

The Output will be

The value of i is 12

The value of i is 16

The value of i is 30

    Here in this example since the variables i,j and k hold the same data type we declared them in the same statement. Notice that they have also been initialized with some values.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)