Java has 8 basic data types, also known as primitive data types. They are divided into 4 groups:
1. Integers: Used to store whole numbers, without decimal points.
- byte: Takes up 1 byte of memory, stores integers from -128 to 127.
- short: Takes up 2 bytes of memory, stores integers from -32,768 to 32,767.
- int: Takes up 4 bytes of memory, stores integers from -2,147,483,648 to 2,147,483,647. This is the most commonly used integer data type.
- long: Takes up 8 bytes of memory, stores integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
2. Floating-Point Numbers: Used to store numbers with decimal points.
- float: Takes up 4 bytes of memory, stores single-precision floating-point numbers.
- double: Takes up 8 bytes of memory, stores double-precision floating-point numbers. This is the most commonly used floating-point data type.
3. Characters:
- char: Takes up 2 bytes of memory, stores a single Unicode character.
4. Boolean:
- boolean: Can only hold two values:
true
orfalse
.
Summary of basic data types in Java:
Data Type | Size (bytes) | Range of Values | Default Value |
---|---|---|---|
byte | 1 | -128 to 127 | 0 |
short | 2 | -32,768 to 32,767 | 0 |
int | 4 | -2,147,483,648 to 2,147,483,647 | 0 |
long | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L |
float | 4 | ±3.4028235E+38 | 0.0f |
double | 8 | ±1.7976931348623157E+308 | 0.0d |
char | 2 | '\u0000' to '\uffff' (0 to 65,535) | '\u0000' |
boolean | 1 bit | true or false | false |
- When declaring a variable with the
long
data type, you need to add the letterL
at the end of the value. For example:long myLong = 10000000L;
- When declaring a variable with the
float
data type, you need to add the letterf
at the end of the value. For example:float myFloat = 3.14f;
Understanding these basic data types is an important first step in learning Java programming
0 Comments
Post a Comment