Skip to main content

Lest's Start JAVA Programming - [Part 05]

 

Lest's Start JAVA Programming

Part 5

How to Store Data in Computer Memory?

Computer memory have made with bits and it have lot of bits combine together to make the memory. Data is storing inside those by electricity system. Because of that bite have only 2 stages, bite can have electricity in it or no electricity in it.

Let's see some examples.

byte b;    --------------------> Create variable call b.
b = 10;    --------------------> Assign value 10 to variable b.
System.out.println(b);    --> Print variable b value.

When we assign value 10 variable. Computer can't store it as it in memory. because as explain before memory have bits and bits have only two thing electricity on or electricity off.
Then to store this our OS will convert this value 10 to a binary value. the way it happens is 10 will divided by 2 until it can't divide more.


How it's store in the computer memory.

Positive integer number

Example
byte b;    --------------------> Create variable call b.
b = 10;    --------------------> Assign value 10 to variable b.

Like i mention above, to store this value 10 in memory first it will convert to Base 10 (decimal) value by dividing 2.
After that conversion value 10 is equal to 1010 in decimal.
Then it store in the memory, like this.

What is Signed bit?

When a memory space get create for assign a value, first bite will be reserved to indicate is the number is positive or negative. If signed bit is 0 that means number is positive, if signed bite is 1 that means number is negative.

Let's take a look at other example
byte h;    --------------------> Create variable call h.
h = 100;    --------------------> Assign value 10 to variable h.

Like before 100 is convert to base 10 (decimal) value.
After conversion 100 is equals to 1100100

Now when it store in memory because 100 is positive number signed bit will be 0.

Negative integer number

Like i mention above by signed bit can indicate number is positive or negative.

Let's check an example
How to store -10
convert number 10 to decimal value and store in memory variable by adding 1 as signed bit.


This way is completely wrong, for negative number it will not just put 1 as signed bit and use positive number decimal value. This way only can use for -0 and +0 but there is no numbers like -0 or +0 so this method is wrong.

There is a way how negative integer numbers are store in memory.

Let's take -10 again as the example 

To get correct negative bite pattern,
  1. write down +10 bit pattern.
  2. then write down opposite of that bit pattern (replacing 0 with 1 and 1 with 0)
  3. add 1 to that opposite bit pattern.
  4. then will get the correct -10 bit pattern.

Bit pattern with max and min values can store for each integer types.


Floating point numbers

double b;    --------------------> Create double variable call b.
b = 123000000000.0;    --------------------> Assign value 123000000000.0 to variable b.

To store double value need to follow these steps.
  1. bring . to the front of the number by writing down value as power of 10.
  2. then convert value after . and power to decimal values.
  3. then store in memory.


Characters

When we assign characters to variables they are not store as characters in memory. because memory can only store bits and bits only have two values 0 or 1. We can't store 'A' in one bit. so that means there is some conversions Java compiler doing when storing characters. And most important part is there is no signed bit in characters.

Let's take look at an example

Let's create char variable call ch and assign value 'A' to it.
char ch = 'A';
In order to store this value 'A' in memory there is two steps need follow.
  1. Every character have ASCII value to represent that. So we need to get ASCII value of the character.
  2. Then convert that ASCII value to decimal number.
  3. Then store in memory.




Comments

Popular Posts

What is a Computer Program ?

Let's Start JAVA Programming - [Part 01]

Lest's Start JAVA Programming - [Part 04]