Data Types In Java
Data types
A data type is nothing but simply it’s a type of data . That means data type is a data storage format that can contain a specific type or range of values.
We are also known that , Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory.Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.Whenever we store some value in variable , then the one which defines the type of that variable is called the data type. Data type defines the size and value a variable can hold.
There are two types of data type in java
- Primitive data
- Non primitive data type
Primitive
data types are predefined types of data, which are supported by the programming
language. For example, integer, character, and string are
all primitive data types.
There are 8 primitive
types: byte, short, int, long, char, float, double, and boolean , Integer data types
byte (1 byte)
short (2 bytes)
int (4 bytes)
long (8 bytes)
Non-primitive data
types are not defined by the programming language, but are instead created by
the programmer. They are sometimes called “reference variables,” or “object
references,” since they reference a memory location, which stores the data.
Primitive datatypes are predefined by the language and named by a keyword. Let us now look into the eight primitive data types in detail.
byte:
- This can hold whole number between -128 and 127. Mostly used to save memory and when you are certain that the numbers would be in the limit specified by byte data type.
- Default size of this data type: 1 byte.
- Default value: 0
short:
· Short data type is a 16-bit
signed two's complement integer. Most rarely used datatype in java
is “Short” .
- This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767.
- Default size of this data type: 2 byte.
int
- Int data type is a 32-bit signed two's complement integer.
- Minimum value is - 2,147,483,648 (-2^31)
- Maximum value is 2,147,483,647(inclusive) (2^31 -1)
- The default value is 0
- Example: int a = 100000, int b = -200000
long
- Long data
type is a 64-bit signed two's complement integer
- Minimum
value is -9,223,372,036,854,775,808(-2^63)
- Maximum
value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
- This type
is used when a wider range than int is needed
- Default
value is 0L
- Example:
long a = 100000L, long b = -200000L
Floating Point Data type
FLOAT
|
DOUBLE
|
Size : 4 bytes (32 bits)
|
Size :
8 bytes (64 bits)
|
Range : -3.4e38 to
3.4e38
|
Range : -1.7e308 to
1.7e308
|
Float data type follows single-precision.
|
Double follows
double-precision.
|
If we want to 5 to 6 decimal places
of accuracy then we should go for
float.
|
If we want to 14 to 15 decimal places of
accuracy then we should go for float.
|
boolean
- boolean
data type represents one bit of information
- There are
only two possible values: true and false
- This data
type is used for simple flags that track true/false conditions
- Default
value is false
- Example:
boolean one = true
char
- char data
type is a single 16-bit Unicode character
- Minimum
value is '\u0000' (or 0)
- Maximum
value is '\uffff' (or 65,535 inclusive)
- Char data
type is used to store any character
- Example:
char letterA = 'A'
Comments