Thursday, December 4, 2014

Remember me !!!

What would happen if you can not remember anything ???
The world would be so bad if you can not remember anything. Suppose, you can not remember if you have already collected a ticket while travelling, you will end up buying a lot of tickets.

Isn't it nice that we can remember things.

Same is the case of Java. It gives us a choice to store data in memory and use whenever required.

With that said, now comes a formal definition of Variable.

Variable: Variable is a reserved memory location to store data with a name and also provides a set of operations to be performed on the data. Programmers use the name of the variable to work on.

A variable declaration have the following syntax
data_type variable_name = [value];

Java is a strongly typed programming language. Each variable must have a particular data type. Java only allow programs to execute if an operation is valid for the particular data type.
For example, you can not perform an operation on numeric data that is supposed to be done on text content.

Now, we know that variable declaration has three parts, data_type, variable_name and value. Lets's go into each part.

 Data Type: Java has two types of data type

  • Primitive Data Type
  • Reference Data type
Primitive Data Type: 8 primitive data types are available in Java. Those are predefined in the Java language and each type has special operations and memory allocation. Let's take a look on each of these,

We can divide these 8 parts in different sections.
Whole number declaration
All the whole number declaration in Java is in two's complement form. 4 whole number declaration is possible in Java.

byte: This is the shortest possible signed whole number declaration in Java. It uses 8 bits (1 Byte). It has a minimum value of -128 and maximum value of 127. So, using this data type, you are only allowed to assign a variable with numeric value range -128 to 127.

short: This is also signed whole number declaration in  Java. The difference with byte is the range and memory space. It uses 16 bits (2 Bytes). It has a minimum value of -32768 and maximum value of 32767. So, using this data type, you are only allowed to assign a variable with numeric value range -32768 to 32767.

int: This is also signed whole number declaration in  Java. The difference with byte and short is the range and memory space. It uses 32 bits (4 Bytes). It has a minimum value of -2,147,483,648 and maximum value of 2,147,483,647So, using this data type, you are only allowed to assign a variable with numeric value range -2,147,483,648 to 2,147,483,647.

long: This is the largest possible signed whole number declaration in  Java. The difference with byte and short and int is the range and memory space. It uses 64 bits (8 Bytes). It has a minimum value of -9,223,372,036,854,775,808 and maximum value of 9,223,372,036,854,775,807So, using this data type, you are only allowed to assign a variable with numeric value range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Real number declaration
Real numbers in Java is represented as IEEE 754 standard and two types of variable is there, one for single precision and another for double precision.

float: This data type is a single-precision 32-bit IEEE 754 floating point. This data type should never be used for precise values, such as currency.

double: This data type is a single-precision 32-bit IEEE 754 floating point.  For decimal values, this data type is generally the default choice. This data type should never be used for precise values, such as currency.

These were related to numeric calculations. Another two data types are for character declaration and boolean declaration.

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

These were the primitive data types you can use your program. Apart from this set, Java provides special support for character sequence through java.lang.String class. You can define any String as primitive declaration and it will be automatically converted to String object. We will be discussing String in more detail later.

Reference Data Type: Apart from the primitives, it is also possible in Java to declare and assign Class types (like Human). More on this in later articles.



So, putting everything in a big picture looks like.


Next, we'll see how a variable name to construct.



Prev      Next
Palash Kanti Kundu

No comments:

Post a Comment