During programming, you might need an entity to store some value into it for this you can use variables. Each variable has a capacity that is determined by the data type assigned to it during declaration.
The name assigned to a memory location is known as a variable. Java provides two types of variables-
The name we choose for a variable, methods, classes, interfaces, and packages is called an identifier.
The data type determines the maximum value a variable can store. There are two type of data types in Java-
A type of value a variable can store is also determined by the data type.
Following table displays the size, range, and default value of primitive data types.
Data Type | Size | Range | Default Value |
---|---|---|---|
boolean | 1 bit | true or false | false |
byte | 1 byte | -128 to 127 | 0 |
short | 2 byte | -32768 to 32767 | 0 |
char | 2 byte | 0 to 65535 | '\u0000' |
int | 4 byte | -231 to 231 - 1 | 0 |
long | 8byte | -263 to 263 - 1 | 0L |
float | 4 byte | -231 to 231 - 1 | 0.0f |
double | 8 byte | -263 to 263 - 1 | 0.0d |
datatype variable_name;
Example
int numberofballs;
Without initialization, a variable holds default value which is listed above in the table. Also, you can initialize variables at the time of declaration.
int a = 10; //We are doing initialization at the point of declaration. int b; //We are only declaring it. b = 12; //Later we initialize variable b with 12.