Java Primitive types are the most basic data types.
The eight primitive data types in Java are byte
, short
, int
, long
, float
, double
, boolean
, and char
.
each primitive type occupies a different amount of memory.
byte, short, int, long, float, double
public class Primitive {
public static void main(String[] args) {
// byte
System.out.println("Byte Minimum: " + Byte.MIN_VALUE);
System.out.println("Byte Maximum: " + Byte.MAX_VALUE);
// short
System.out.println("Short Minimum: " + Short.MIN_VALUE);
System.out.println("Short Maximum: " + Short.MAX_VALUE);
// integer
System.out.println("Integer Minimum: " + Integer.MIN_VALUE);
System.out.println("Integer Maximum: " + Integer.MAX_VALUE);
// ;ong
System.out.println("Long Minimum: " + Long.MIN_VALUE);
System.out.println("Long Maximum: " + Long.MAX_VALUE);
// float
System.out.println("float Minimum: " + Float.MIN_VALUE);
System.out.println("float Maximum: " + Float.MAX_VALUE);
// double
System.out.println("Double Minimum: " + Double.MIN_VALUE);
System.out.println("Double Maximum: " + Double.MAX_VALUE);
}
}
Byte Minimum: -128
Byte Maximum: 127
Short Minimum: -32768
Short Maximum: 32767
Integer Minimum: -2147483648
Integer Maximum: 2147483647
Long Minimum: -9223372036854775808
Long Maximum: 9223372036854775807
float Minimum: 1.4E-45
float Maximum: 3.4028235E38
Double Minimum: 4.9E-324
Double Maximum: 1.7976931348623157E308
- A byte occupies 8 bits.
- A short occupies 16bits
- A int occupies 32 bits
- A long occupies 64bits
- Single precision number: float
- A float occupies 32bits
- Double precision number: double
- A double occupies 64bits
char, boolean
A char
occupies two bytes of memory, or 16 bits. The reason it is not just a single byte is that it allows you to store Unicode characters.
- Unicode is an international encoding standard for use with different languages and scripts, by which each letter, digit, or symbol is assigned a unique numeric value that applies across different platforms and programs.
public class Unicode {
public static void main(String[] args) {
char myChar = 'D';
char myUnicodeChar = '\u0044';
System.out.println(myChar);
System.out.println(myUnicodeChar);
}
}
D
D
A boolean
value allows for two choices True or False, 1 or 0. In Java terms we have a boolean primitive type and it can be set to two values only. true
or false
.
public class Boolean {
public static void main(String[] args) {
boolean trueValue = true;
boolean falseValue = false;
if(!falseValue){
System.out.println(trueValue);
}
}
}
true