What is Datatype?
A data type is an attribute of data/value that tells the type of data held by a variable. Each data-type requires a different amount of memory and has some specific operations that can be performed through it.
Types of data-types in Java
- Primitive data-type
- Non-primitive data-type
Primitive data-type
Primitive types are the most basic and predefined data types available in Java. Once a primitive data type has been declared its type can never change, although in most cases its value can change.
There are 8 primitive data types:
Data Type | Default Value | Default size |
---|---|---|
byte | 0 | 1 byte |
short | 0 | 2 bytes |
int | 0 | 4 bytes |
long | 0L | 8 bytes |
float | 0.0f | 4 bytes |
double | 0.0d | 8 bytes |
boolean | false | 1 bit |
char | ‘\u0000’ | 2 bytes |
See also – Java program to print size of different datatypes
Java data types example
public class DataTypeExample { public static void main(String[] args) { // Assign value char c = 'A'; boolean valid = true; byte b = 123; short s = 12345; int i = 1234567890; long l = 12345678910L; float f = 12345.6F; double d = 1234567890.12345D; } }