Javadatatpes
Javadatatpes
Based on the data type of a variable, the operating system allocates memory
and decides what can be stored in the reserved memory. Therefore, by
assigning different data types to variables, you can store integers, decimals,
or characters in these variables.
The Java data types are categorized into two main categories −
byte
short
int
long
float
double
boolean
The default value of a byte variable is 0, which is used to save space in large
arrays, which is mainly beneficial in integers since a byte is four times
smaller than an integer.
Example
byte a = 100;
byte b = -50;
Learn Java in-depth with real-world projects through our Java certification
course. Enroll and become a certified expert to boost your career.
short Data Type
The short data type is a 16-bit signed two's complement integer, which
provides a range of values from -32,768 (-215) to 32,767 (inclusive) (215 -1).
Like the byte data type, the short data type is also beneficial for saving
memory, as it occupies less space compared to an integer, being only half
the size.
Example
short s = 10000;
short r = -20000;
Example
int a = 100000;
int b = -200000;
Example
long a = 100000L;
long b = -200000L;
Example
float f1 = 234.5f;
Example
double d1 = 123.4;
Example
Example
Open Compiler
public class JavaTester {
public static void main(String args[]) {
byte byteValue1 = 2;
byte byteValue2 = 4;
byte byteResult = (byte)(byteValue1 + byteValue2);
short shortValue1 = 2;
short shortValue2 = 4;
short shortResult = (short)(shortValue1 + shortValue2);
int intValue1 = 2;
int intValue2 = 4;
int intResult = intValue1 + intValue2;
Output
Byte: 6
Short: 6
Int: 6
Long: 6
Float: 6.0
Double: 6.0
Boolean: true
Char: A
Example
The following example demonstrates the reference (or, object) data types.