Primitives and Objects in Memory

In Java there are seven primitive types; byte, short, int, char, float, double and boolean. Any type which is not in these seven is an Object type. Object types are constructed and stored differently compared to the primitive types. The literals (values) of primitive variables are stored in the location which the variable points to -

To understand this concept better, let us take the example program which declares a variable of byte type: byte a = 10

See the figure below which shows the sample program which uses this statement.

In this statement you have a variable 'a' declared and is initialized with the literal value of 10. When this statement is executed, the OS finds a memory location to store this value. Let us say that the OS found an empty location at the address value @8a7d67c and saves the binary value of 10 which is 00001010 in this location. Then this memory location is mapped to variable 'a'

Whenever a reference is made to this variable 'a', the program fetches the value 10 stored by using the memory location mapping.

image alt text

In case of objects however, the memory location does not contain the literals that the object hold. Instead it contains the memory location of the object's literal values. In the example above an array for holding two integers is declared and initialized with values as shown below:

bylte[] myInts = {1,2}

Arrays are objects in Java. From the above figure you see that the memory address - @9a7d59b is mapped to the variable myInts. However the value that this memory location contains is the value of another memory location @69b8b810 and if you look into what is stored in this memory location, you will see the binary representation of literals 1 and 2.

Note however that the memory addresses that are shown here are only for representation and in reality these values will be different for different machines and you cannot know which memory address is assigned to which variable before hand. Once assigned however, you can see the hashcode of the object which we have used as memory address of the object. This hashcode is calculated with many variables including the actual memory address of the object using the API method as detailed here: https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#identityHashCode-java.lang.Object-

When you print 'a' you see the literal value 10 printed out. However when you print myInts you will see the hashcode of the Array object which we have used as memory address in our example for simplicity.

In case of an Array, all the literal values of the array are stored in a contiguous memory and the hashcode that is printed out is used by us to represent the starting address of the contiguous memory.

What is a hash code/

A hashcode is a 32 bit signed integer representing the object. It is printed out in hexadecimal representation. Typically this hashcode is calculated using the memory location, however, in Java programming language it is not required. https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()

But to keep things simple we have used this hashcode to represent memory in our above example.

results matching ""

    No results matching ""