The Java programming language is statically typed, the variable should be initialized in the program before using it.
The data type of the variable determines the type of value the variable is allowed to hold
Primitive Datatype:
A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values.
The eight primitive data types supported by the Java programming language are:
1.byte
- The byte
data type is an 8-bit signed two's complement integer
- It has a minimum value of -128 and a maximum value of 127 (inclusive)
- The byte
data type can be useful for saving memory in large arrays, where the memory savings actually matters
-The default value of byte is 0
Example: byte a = 126
2. short:
- The short
data type is a 16-bit signed two's complement integer.
- It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive)
- Like byte you can use a short
to save memory in large arrays, in situations where the memory savings actually matters
- The default value of short is 0
Example: short = 56;
3. int:
- int
data type is a 32-bit signed two's complement integer
- It has a minimum value of -231 and a maximum value of 231-1
- In Java SE 8 and later, you can use the int
data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1.
- The default value is 0
Example: int i=89;
4. Long:
- The long
data type is a 64-bit two's complement integer.
- The signed long has a minimum value of -263 and a maximum value of 263-1
- In Java SE 8 and later, you can use the long
data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1
- Default value is 0L
Example: long a = 100000L
5. float:
- The float
data type is a single-precision 32-bit IEEE 754 floating point
- float
can be used instead of double
if you need to save memory in large arrays of floating-point numbers
- It should not be used for precise values, such as currency.
- The default value is 0.0f
Example: float f = 4.7333434f;
6. double:
- The double
data type is a double-precision 64-bit IEEE 754 floating point.
- It is highly recommended to store decimal values
- Like float, this data type should never be used for precise values, such as currency
- Default value is 0.0d
Example: double d = 4.355453532
7. Boolean:
- boolean data type represents one bit of information
- The boolean
data type has only two possible values: true
and false
- It is used to
track simple flag true/false conditions
- Default value is false
Example: boolean myBool = true;
8. char:
- The char
data type is a single 16-bit Unicode character
- It has a minimum value of '\u0000'
(or 0) and a maximum value of '\uffff'
(or 65,535 inclusive)
- Char data type is used to store any character
Example: char myText= 'D';
Usecase:
Output:
Literals:
A literal is used to assign a fixed value in the code. It is represented directly in your code without any computation
Integer Literal
Integer literals are assigned to the variables of data type byte, short, int, and long.
Example:
byte b = 100;
short s = 200;
int num = 13313131;
long l = 928389283L;
Float Literals
Float literals are assigned to the variables of data type float and double.
double num1 = 24.4;
float num2 = 22.4f;
Char and String Literal
Char and String's literals are assigned to the variables of data type char and String type.
char ch = 'S';
String str = "JAVA";