Java programming language is a “strongly” typed language. Every variable declared must have a data type that determines the size of the variable in memory.
Expressions in Java have types. Although data types are strictly defined, there is compatibility between certain data types. This allows type conversion and casting of variables.
The data types are
- Floating-point numbers
|Main Type||Data Type||Size (in bits)||Range|
|-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
-2,147,483,648 to 2,147,483,647
-32,768 to 32767
-128 to 127
|4.9e-324 to 1.8e+308
1.4e-045 to 3.4e+038
|Character||Char||16||0 to 65536|
As per the table above, the
long type is 64-bit signed type with a large range. The
int type is the first choice for an integer type, but the
long type is chosen when the number cannot be stored with
int type is the most common type used as an integer type. The
int type is signed 32-bit type. Here is an example of
The short type is the least used type in Java. It is signed 16-bit type with range -32,768 to 32767/ Here is an example of short type.
The byte type is smallest of the integer type. It is signed 8-bit type suitable to store raw binary data. Other than that byte type can handle a stream of information from network, or file.
double precision is also known as.
double It has a storage of 64-bit. Also, it is faster in some optimized processors when compared to single precision. To maintain the accuracy of mathematical calculations, built-in functions like sin(), cos(), etc returns.
double Here are some examples of.
double radius; double PI;
The float is a single precision that use32-bit of storage. Here are some examples of the.
float length; float height;
The char type is unsigned 16-bit to store Unicode characters. It has a range of 0 to 65536. The character is expressed in many ways.
- Using single quotes.
- Escape sequence
- Unicode escape sequence
Here is the example for each type.
char answer = 'Y'; char day = 'f';
The escape sequence does work for all characters, but only a few. Here is the list.
|‘\r’||a carriage return|
|‘\”‘||a double quote|
|‘\”||a single quote|
char ret = '\r'; char nwline = '\n';
char a1 = '\u0041'; //The code '\u0041' is same as 'A'
The boolean type has two values called boolean literals. Here are some examples of boolean type.
boolean finished = true; boolean off; off = false;