Java is a strongly typed programming language unlike Vbscript,Perl.In novice terms it means that all the variables in the Java programming language ought to have a particular data type.

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.
Syntax:-
Variable Type Variable Name;
Example:-
String Learn;
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.

Note:-
1.We can declare and assign value to a variable in one go or in two steps,
Example1:– 
String Learn = “java”;
 or 
String Learn; Learn = “java”;
Example2:-
(Grouping):- 
String Learn = “java”, Revise = “selenium”; 
or 
String Learn,Revise; 
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.

Types :- 
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 Java
Primitive Data Type

Code:-

public class DataType {
public static void main (String[] args)
{
System.out.println("Size of byte: " + (Byte.SIZE/8) + " byte.");
System.out.println("Size of char: " + (Character.SIZE) + " bits.");
System.out.println("Size of short: " + (Short.SIZE) + " bits.");
System.out.println("Size of int: " + (Integer.SIZE) + " bits.");
System.out.println("Size of long: " + (Long.SIZE) + " bits.");
System.out.println("Size of float: " + (Float.SIZE) + " bits.");
System.out.println("Size of double: " + (Double.SIZE) + " bits.");
System.out.println("Size of Boolean: -- bits.");
}
}

##Output:-
Size of byte: 1 byte.
Size of char: 16 bits.
Size of short: 16 bits.
Size of int: 32 bits.
Size of long: 64 bits.
Size of float: 32 bits.
Size of double: 64 bits.
Size of Boolean: -- bits.


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).

Memory data cell


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.

Data Range for a Byte in Java

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.

Byte Data Type in JAVA

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.

Example :- 
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.

package Java;
public class DataType {
public static void main(String[] args) {
//We have a variable
String Learn = "Selenium";
//Lets check Class of Variable
System.out.println("Class = "+ Learn.getClass());
//Lets check Type of Variable
System.out.println("Type="+Learn.getClass().getSimpleName());
}
}


Primitive Type
We need to create the object of Datatype to use the above methods.

package Java;
public class DataType {
public static void main(String[] args) {
//We have a variable
int noOfDays = 365;
//Create object
Object obj = noOfDays;
//Lets check Class of Variable
System.out.println("Class = "+ obj.getClass());
//Lets check Type of Variable
System.out.println("Type="+obj.getClass().getSimpleName());
}
}
How to find Datatype of Primitive variables
Output of Int DataType

Meaning of Arrays in Java

Leave a Reply

Your email address will not be published. Required fields are marked *