Exploring Data Type & Variables in Java – User friendly Tech help

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.
n
nExample we cannot use a variable say “xyz” in the code before declaring this like:- String xyz;
n
nWhat is a Variable?
nVariable 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.
nSyntax:-
nVariable Type Variable Name;
nExample:-
nString Learn;
nWhere String = Variable Type , Learn = Variable Name.
n
nVariable Type :- it gives what the variable can and cannot hold, as above variable “Learn” can store “String” data.
nVariable Name:-Unique name to access the variable.
n
nNote:-
n1.We can declare and assign value to a variable in one go or in two steps,
nExample1:– 
nString Learn = “java”;
n or 
nString Learn; Learn = “java”;
nExample2:-
n(Grouping):- 
nString Learn = “java”, Revise = “selenium”; 
nor 
nString Learn,Revise; 
nLearn = “java”;
nRevise = “selenium”
n2.Naming conventions for variable Name :-n

    n

  • Spaces are not allowed in the Name
  • n

  • Characters allowed are A-Z and a-z(small case)
  • n

  • Numbers (0-9)
  • n

  • Special Characters Only $ and _(underscore) are allowed 
  • n

  • Name cannot begin with a number but above special characters are allowed.
  • n

  • Reserved keyword in java are not allowed as variable name like new,for,case,continue…
  • n

n

What is a Data type?
nData type specifies the size and the type of values that can be stored in an variable name.
n
nTypes :- 
nPrimitive & Non primitive.
n
nPrimitive 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.
n
nFollowing 8  data types are marked as primitive.
n

n

n

n

n

n

n

n

n

Primitive Data Type

n

Code:-

n

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.

n


How the range of a Data Type is assigned:-
nLike why byte datatype range starts from -128 to 127.
nIn 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.
n
nComputer 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).

nn

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

n

n

n

n

n

n

n

n

n

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.

n

n

n

n

n

n

n

n

n

So our 8 bits can hold numbers ranging from -128 to 127(including 0) or a total of 256 numbers.
n
nNon primitive (Reference type) :- classes(String Class) , arrays.
n
nPrimitive v/s Reference type:-
nFor 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.
n
nHowever, 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.
n
nInstead, 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.
n
nExample :- 
nstr = new String(“example of reference”), it would work like :-
n1.Evaluate the expression , new String(“example of reference”)
nthis expression creates an new object, which creates an reference to an object which decriobes its location in memory.
n2.Store the value in the variable (str)
nstr= The reference to string created
n
nNow 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.
n
nHow to get variable type in Java?
nNon Primitive Type:- 
nWe can directly get it by using getClass() and getSimpleName() methods.

n

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()); }}

n


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

n

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()); }}

n

n

n

n

n

n

n

n

Output of Int DataType

n

Meaning of Arrays in Java

Was this article helpful?
YesNo

Similar Posts