Example we cannot use a variable say “xyz” in the code before declaring this like:- String xyz;
What is a Variable?
Variable is the name given to the reserved area allocated in the memory.So we are creating a variable,we are blocking a new address space with a unique name.
Variable Type Variable Name;
Where String = Variable Type , Learn = Variable Name.
Variable Type :- it gives what the variable can and cannot hold, as above variable “Learn” can store “String” data.
Variable Name:-Unique name to access the variable.
1.We can declare and assign value to a variable in one go or in two steps,
String Learn = “java”;
String Learn; Learn = “java”;
String Learn = “java”, Revise = “selenium”;
Learn = “java”;
Revise = “selenium”
2.Naming conventions for variable Name :-
- Spaces are not allowed in the Name
- Characters allowed are A-Z and a-z(small case)
- Numbers (0-9)
- Special Characters Only $ and _(underscore) are allowed
- Name cannot begin with a number but above special characters are allowed.
- Reserved keyword in java are not allowed as variable name like new,for,case,continue…
What is a Data type?
Data type specifies the size and the type of values that can be stored in an variable name.
Primitive & Non primitive.
Primitive type is the default data type in Java language and they come with predefined number of operations.We can not define a new operation for such primitive types.
Following 8 data types are marked as primitive.
|Primitive Data Type|
How the range of a Data Type is assigned:-
Like why byte datatype range starts from -128 to 127.
In our above diagram we associated a particular range set with a given data type , say Int is 4 byte(32 bit) but how this is computed.We would go into our school days to comprehend this process.
Computer memory store numbers in the form of binary system(meaning 0 or 1). And our memory is like a grid of cell.Now each cell contains binary data(in the form of 0 or 1 called as bit).
Our data flow in the form of 8 bits meaning we have 8 places to put our bits , thus incase we make them all 1 in each cell it comes as (1111111 in binary ) which is equivalent to 255 in decimal system.Further to take the minimum we can have all bits have 0 each. So the number from 0 to 255 can fit the 8 bits.
Out of this 8 bits one bit is reserved for holding the sign of a number(positive or negative) so we are left with 7 bits , thus maximum 7 bits can hold is (1111111 = )127 in decimal system.Similarly for minimum values we have 7 bits of 000000 and 1 bit for sign which comes as(10000000) – 128 in decimal system.
So our 8 bits can hold numbers ranging from -128 to 127(including 0) or a total of 256 numbers.
Non primitive (Reference type) :- classes(String Class) , arrays.
Primitive v/s Reference type:-
For a variable of a primitive type, the value of the variable is stored in the memory location assigned to the variable. So, if an integer variable is declared as “int days = 100″, then when we look at the memory location of “days”, “100” would be stored there just as expected.
However, a variable of a class type(Non Primitive) only stores the memory address of where the object is located – not the values inside the object. So, if we have a class called “learn selenium”, when we create an object(say obj) and if check the this object in memory, we will see that it does not store any of the variables that belong to that object in memory.
Instead, the variable “obj” just stores an address of another place in memory.This means that the object named by the variable is stored in some other location in memory and the variable contains only the memory address of where the object is stored. This memory address is called a reference to the object.
str = new String(“example of reference”), it would work like :-
1.Evaluate the expression , new String(“example of reference”)
this expression creates an new object, which creates an reference to an object which decriobes its location in memory.
2.Store the value in the variable (str)
str= The reference to string created
Now whenever the program needs to refer to the object it uses the variable str.And str contains the reference of memory location where string exists.
How to get variable type in Java?
Non Primitive Type:-
We can directly get it by using getClass() and getSimpleName() methods.
We need to create the object of Datatype to use the above methods.