Introduction to Primitive Data Types

Create a file named PrimitiveVariables.java. Open it in IDE or text-editor application of your choice and add the following lines:

public class PrimitiveVariables {

public static void main(String... args) {
    boolean iAmBoolean = true;
    char iAmChar = 'c';
    byte iAmByte = 1;
    short iAmShort = 12;
    int iAmInteger = 123;
    long iAmLong = 1000L;
    float iAmFloat = 1.1f;
    double iAmDouble = 123.123;
}
}

There are eight primitive types in Java:

boolean

boolean is a primitive type that has exactly two possible values – true and false. Generally it’s used in logical expressions. It’s size in memory is 1 byte.

char

char can represent any single character. It can be used in operations with text search, creation and modification. It’s size in memory is 1 byte. You can create char variable in three different ways – 1) initializing it with exact character 2) using value from ASCII table 3) using unicode value:

char iAmChar1 = 'c';
//ASCII
char iAmChar2 = 99;
//Unicode
char iAmChar3 = '\u0063';

byte

byte represents 1 single byte of data (8 bits), so it can contain a number between -128 to 127 (-27 to 27-1). Generally it’s used to represent data from files. Also it can be used to represent numbers, but in most of cases you should prefer int or long for that purposes. It’s size in memory is (you’ll never guess 🙂 ) 1 byte.

short

short stays for integer number between -32768 to 32767 (-215 to 215-1). It can be used to represent numbers, but in most of cases you should prefer int or long. It’s size in memory is 2 bytes.

int

int stays for integer number between -2147483648 to 2147483647 (-231 to 231-1). You should prefer it for storing numbers and using in arithmetic equations. If it’s size doesn’t suits you consider using long or even BigInteger. It’s size in memory is 4 bytes.

long

long stays for integer number between -9223372036854775808 to 9223372036854775807 (-263 to 263-1). You should prefer it for storing numbers and using in arithmetic equations. If it’s size doesn’t suits you consider using BigInteger. It’s size in memory is 8 bytes. To declare long you should add ‘L’ or ‘l’ after the number (‘L’ is more preferable cause it’s easier to read):

long iAmLong1 = 1000000000000L;
long iAmLong2 = 1000000000000l;

float

float stays for decimal number with precision up to 6-7 decimal points. You should prefer it for storing decimal numbers and NOT PRECISE calculations. If it’s size doesn’t suits you consider using double. For PRECISE calculations use BigDecimal. It’s size in memory is 4 bytes. To declare float you should add ‘F’ or ‘f’ after the number.

float iAmFloat1 = 1000000000000.123F;
float iAmFloat2 = 1000000000000.123f;

double

double stays for decimal number with precision up to 15-16 decimal points. You should prefer it for storing decimal numbers and NOT PRECISE calculations. For PRECISE calculations use BigDecimal. It’s size in memory is 8 bytes. Any decimal number is considered as double by Java, but you also could add ‘D’ or ‘D’ after the number to do it explicitly.

double iAmDouble1 = 99999999.999999;
double iAmDouble2 = 99999999.999999D;
double iAmDouble3 = 99999999.999999d;
Scroll to Top