Data Types in JAVA
- It is a type of data which is used in the program.
- There are many predefined data types in java library like int,char,float etc.
There are two types of data types
1 . Primitive Data type
2. Non- Primitive Data Type
Primitive Data Type
- The data type which is predefined in the library is called primitive data type.
- It is named by a keyword for example int,float,char etc.
- There are eight primitive data types in java.
byte, short, int, long, float, double, boolean and char.
Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte
, short
, int
and long
. Which type you should use, depends on the numeric value.
Floating point types represents numbers with a fractional part, containing one or more decimals. There are two types: float
and double
.
Integer Type
Type | Size in bytes | Range |
byte | 1 | -128 to 127 |
short | 2 | -32,768 to 32,767 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
long | 8 | -9,223,372,036,854,775,808 to 9,223.372,036,854,775,807 |
Float Type
You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515.
The float
and double
data types can store fractional numbers. Note that you should end the value with an "f" for floats and "d" for doubles
Type | Size in bytes | |
float | 4 | |
double | 8 |
Boolean
Very often in programming, you will need a data type that can only have one of two values, like:
- YES / NO
- ON / OFF
- TRUE / FALSE
For this, Java has a boolean
data type, which can only take the values true
or false
Keyword | boolean |
Syntax | boolean x; |
Value | True/False |
Default | False |
Character
The char
data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':
Keyword | char |
Syntax | char x; |
Value | 'a','@','9' |
Non-primitive Data Type
- The data type which is created by a programmer is called non-primitive data type.
- Non-primitive types can be used to call methods to perform certain operations.
- A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
- Example:class,interface,Arrays,String etc
0 Comments