Java 02: Data Types, variables and constant

Data type

There are many types of data an application can operate on. These are fundamental or basic data type that are used for data representation. These basic data type later can be used for building more complex type of data like in objects. The basic data type is either numeral or non-numeral.

Numeral data types are integer and floating-point values. Integer values are negative infinity to positive infinity numbers without decimal point in it.

Example: 1, -13, 0, 2138, -1111 etc.

Floating point values are same as integer number but with decimal point values.

Example: 0.1, -123.018, 111.1111 etc.

After knowing what the possible values are each type of data can be used for, now we should know what the size of each data type are. In computer, this variable is residing in memory. And each variable has its own memory space with limited spaces. In each programming language, the size may vary a bit, but in Java the size for int data type is 32 bits.

Other that int, we have byte with size 8 bits, short with size of 16 bit and largest of them all is long with size of 64 bits. So, what does these sizes means. Let’s take byte as example 8-bit size means it can store up to 2 ^ (8-1)-1 as the maximum value. Means the maximum value is 127 and minimum value of 2 ^ (8-1) which is -128. This also means if we say a variable is a byte data type, its value can only contain any one value between -128 and 127, and any other value outside of this range will not be accepted simply because don't have enough space.

TypeKeywordExample
Integerint0 – 2,148,221
Floatfloat0.0 – 2.121..21
Doubledouble0.0 – 4/223
BooleanbooleanTRUE or FALSE
characterChara…Z. Unicode

Variable

Variable is an identifier. Variable refers to memory location. these memory location stores value of data. Memory locations have their own address. To easy programming, java provides a way to refer to this memory location by name instead of memory address.

In our first java program, we did not utilized variable thus did not benefit much of Java capability. Most programs designed to process data. For that we need variables in our program. To use variable, it must be declared first. Following is syntax for Variable declaration.

dataType variableName;

Variable declaration begins in a new line. first word is data type. Variable can be integer, float, double, char or any object. data type is followed by variable name and finally ends with semicolon marking the end of statement. For example, to declare an integer variable by name “age”:

int age;

Example, when compiler executes this statement by assigning memory location to store integer data type and refers to this memory location as age.

class DataTypeExample{
    public static void main(String[] args){
        int a;
        float b;
        double c;
        boolean d;
        char e;
    }
}

Next step is to assigning variable with a value. To assign a value to variable, assignment operator ‘=’ is used. Syntax for variable assignment is:

variable = expression;

Variable is written on left side of assignment operator, and expression on right. expression can be literal value, variable, method returning value or arithmetic calculation. Example of assigning a literal value to a variable.

x = 3;

This statement assigns value 3 to variable x. One thing to note, is that assignment operator is right associative. Following is example assigning result of arithmetic expression

x = (1 + 2) * 3;

This statement evaluates (1+2) *3, and its result is assigned to variable x. Anything on the right side of the assignment operator need to complete it operation before assignment operation takes place.

class DataTypeExample{
    public static void main(String[] args){
        int a;
        float b;
        double c;
        boolean d;
        char e;

        a = 3;
        b = 2.3f;
        c = 1.2;
        c = 1.234 * 1;
        d = true;
        e = 'g';
    }	
}

this example assigns value of an existing variable to new variable

x = y;

This statement will assign value of variable y to variable x. Just make sure variable y contains value first. We also can declare and assign value in same statement as shown below:

datatype variableName = value;

for example, declare an integer variable age and assign 23 as its value in one statement.

int age = 23;

The first value after variable declaration is known as initial value. Assigning initial value is known as initialization. Every variable must be initialized before using them in another statement. This is often the preferred way of variable declaration because it makes sure variable is initialized.

Constant

Variable value can change during program execution. Constant value does not change during execution. Constant is a save way to store data that you don’t when to accidently change. Some of well-known constants are PI value, speed of light, number of days in a month etc. Like variable, constant need to be declared. to declare a constant, final keyword is used. Syntax for constant declaration is:

final datatype CONSTANT_NAME = value;

Constant declaration begins with final keyword, data type indicating type of data this constant will hold, then constant name, where its spelled in upper case to distinguish variables, assignment operator and finally value of the constant. Following example declares a constant for PI value:

final double PI_VALUE = 3.142;

The data type of this constant is double, constant name is PI_VALUE. Finally, PI value 3.142 is assigned to this constant. Following program calculates circumference of a circle.

class ConstantExample{ 
    public static void main (String args[]){
        final double PI_VALUE = 3.142;
        int radius= 5;     
        double cir = 2 * radius * PI_VALUE;
        System.out.println("Circumference of circle is " + cir);
    }
}

In this example, we use variable and constant in arithmetic expression. Following is the output of the example.

Circumference of circle is 31.4199999999999998

A post by Cuber

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net