Primitive variables :
Numbers can be stored and retrieved while a program is running if they are given a home. The way that Integers and decimal numbers are stored in the computer by declaring a variable .When you declare a variable, the computer sets aside a space for it in the working memory of the computer (RAM) while the program is running.
Primitive data type:
A variable in Java is designed to hold only one particular type of data; it can legally hold that type of data and no other. The compiler will consider it to be a syntax error if you try to violate this rule by assigning a value of the wrong type to a variable. We say that Java is a strongly typed language because it enforces this rule.
There are eight primitive types built into Java. The primitive types are named
byte, short, int, long, float, double, char, and boolean.
The first four types hold integers
(whole numbers such as 17, -38477, and 0). The four integer types are distinguished by the ranges of integers they can hold.
The float and double types hold real numbers (such as 13.6 and -105.99). Again, the two real types are distinguished by their range and accuracy.
A variable of type char holds a single character from the Unicode character set.
a variable of type boolean holds one of the two logical values true or false
Data value stored in the computer’s memory must be represented as a binary number, that is as a string of zeros and ones. A single zero or one is called a bit . A string of eight bits is called a byte. Memory is usually measured in terms of bytes.The byte data type refers to a single byte of memory.
A variable of type byte holds a string of eight bits, which can represent any of the integers between -128 and 127, inclusive. (There are 256 integers in that range)
- short corresponds to two bytes (16 bits). Variables of type short have values in the range -32768 to 32767.
- int corresponds to four bytes (32 bits). Variables of type int have values in the range -2147483648 to 2147483647.
- long corresponds to eight bytes (64 bits). Variables of type long have values in the range-9223372036854775808 to 9223372036854775807.
The float data type is represented in four bytes of memory, using a standard method for encoding real numbers. The maximum value for a float is about 1038.
A float can have about 7 significant digits.
A double takes up 8 bytes, can range up to about 10308, and has about 15 significant digits.
A variable of type char occupies two bytes in memory. The value of a char variable is a single character such as A, *, x, or a space character.
Values of type char are closely related to integer values, since a character is actually stored as a 16-bit integer code number. In fact, we will see that chars in Java can actually be used like integers in certain situations.
It is important to remember that a primitive type value is represented using only a certain, finite number of bits. So, an int can’t be an arbitrary integer; it can only be an integer in a certain finite range of values. Similarly, float and double variables can only take on certain values.