Data types
Java defines the eight simple Data types: byte, short, int, long, char, float, double, and boolean. These eight types can be put in four main groups:
- Integers
- Floating
- Characters
- Boolean
The programmer can use these types to construct arrays of class types. The simple types show single values in programming. The simple types are defined to have an explicit range and mathematical behavior in programming languages. Languages such as C++ and C allow the integer size to vary based upon the dictates of the execution environment. Java is different from its portability requirement. all data types have specifically defined a range. For example, an int is always 32 bits (4 bytes), regardless of the particular platform.
Integer’s data types:
The programming language Java defines four integer data types which are a byte, short, int, and long. These four data types are signed with positive and negative values. Java language does not support unsigned, positive integers. Many other programming languages, including C or C++, support both signed and unsigned integers.
byte: byte is the smallest integer type. Its memory size is 8 bit (1 byte). Its values range is -128 to 127. Type byte is especially used in the stream of data from a network or file. Its also used in binary data that may not be directly compatible with Java other built-in data types. Keyword byte is used to declared the byte data types.
Example:
byte a;
byte b;
byte c;
short: short is an integer data type. Its memory size is 16 bit (2 bytes). Its values range is -32,768 to +32,767. Keyword short is used to declare short data types.
Example:
short a;
short b;
short c;
int: data type int most commonly used in programming languages. Its memory size is 32 bit (4 bytes). Its values range is -2,147,483,648 to 2,147,483,647. The data type int is a more efficient and versatile type. Keyword int is used to declare the int data types.
Example:
int a;
int b;
int c;
long: data type long is useful for those conditions when an int type is not enough to store the values. This makes it useful when required large values. Its memory size is 64 bit (8 bytes). Its values range is -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. The keyword long is used to declare the long data types.
Example:
long a;
long b;
long c;
The following table shows the variable size, minimum value, and maximum value.
Size | Minimum value | Maximum value | |
byte | 8 bit(1 byte) | -128(-2^7) | +127(2^7-1) |
short | 16 bit(2 bytes) | -32,768(-2^15) | +32,767(2^15-1) |
int | 32 bit(4 bytes) | -2,147,483,648(-2^31) | +2,147,483,647(2^31-1) |
long | 64 bit(8 bytes) | -9,223,372,036,854,775,808(-2^63) | +9,223,372,036,854,775,807(2^63-1) |
Floating data types:
Floating-point numbers also called real numbers and used when evaluating expressions that require fractional precision. These types used to store the values of sine, cos, tan or floating values. Java supports the standard (IEEE–754) set of floating-point types and operators. There are two further types of floating data types, one is float and the second is double, which shows the single and double precision numbers.
float: the float type specifies the single value. The float data types take half-space in memory as double data types. Its memory size is 32 bit (4 bytes). Its values range is 1.4e−045 to 3.4e+038. The keyword float is used to declare the long data types.
Example:
float a;
float b;
float c;
double: the data type double is used to store long fractional or floating-point values which are best to maintain the accuracy over many iterative calculations. The double data type’s memory size is 64 bit (8 bytes). Its values range is 4.9e–324 to 1.8e+308. The keyword double is used to declare double data types.
Example:
double a;
double b;
double c;
The following table shows the variable size, minimum value, and maximum value.
Size | Minimum value | Maximum value | |
Float | 32 bit(4 bytes) | 1.4e−045 | 3.4e+038 |
double | 64 bit(8 bytes) | 4.9e–324 | 1.8e+308 |
Characters data types:
In Java programming language the data type used to store characters is char. Java uses the Unicode to show the characters. The Unicode supports the fully international characters set which can show all of the characters found in human languages. The data type char is used to store a single character. The char data type’s memory size is 16 bit (2 bytes). Its values range is ‘u0000’ (or 0) to ‘uffff’ (or 65,535 inclusive). The keyword char is used to declare char data types.
The following table shows the variable size, minimum value, and maximum value.
Size | Minimum value | Maximum value | |
char | 16 bit(2 bytes) | ‘u0000’(or 0) | ‘uffff’(or 65,535 inclusive) |
Boolean data types:
Java has one smallest data type which is called boolean. It can have only one state which is true or false. This type is used to store the results of relational operators, such as x > y. It can also use to store the results of the conditional expression. The boolean data type’s memory size is 1 bit. Its values range is true or false. The keyword Boolean is used to declare the Boolean data types.
The following table shows the variable size, minimum value, and maximum value.
Size | Minimum value | Maximum value | |
boolean | 1 bit | false | true |