JavaDevNotes.com

Java Tutorial - Primitive Types

Primitive types are the most basic data types in Java that are used to store simple values. They can be used directly in programs or as a building block to create more complex data types.

If you are unfamiliar with variables, read this previous tutorial first.

Primitive Types

  • byte - 8 bit signed integer
  • short - 16 bit signed integer
  • int - 32 bit signed integer
  • long - 64 bit signed integer
  • float - 32 bit floating point number that can have decimal places
  • double - 64 bit floating point number that can have decimal places
  • boolean - represents logical operation that can either be true or false
  • char - represents a single unicode (16 bit) character.
As you can observe, these types are for storing simple values. These can also be seen in other programming languages.

byte, short, int, long

The first four data types (byte, short, int, and long) are very similar to each other.
  • Represents integer (whole numbers) only. Values can be zero, negative, or positive numbers.
  • Difference between the four data types is the number of bits needed to represent their values.
  • byte uses 8 bit of memory
    • Minimum value is -128
    • Maximum value is 127
    • Ideal for representing small numbers as it consumes the least amount of memory
    • Example:
      byte a = -10;
      byte b = 0;
      byte c = 100;
      
  • short uses 16 bit of memory
    • Minimum value is -32,768
    • Maximum value is 32,767
    • Can be used if byte is not enough for the possible values to be assigned
    • Example:
      short a = -19000;
      short b = 0;
      short c = 21000;
      
  • int uses 32 bit of memory
    • Minimum value is -2,147,483,648
    • Maximum value is 2,147,483,647
    • Most commonly used data type for representing numbers
    • Example:
      int a = -7111555;
      int b = 0;
      int c = 5200300;
      
  • long uses 64 bit of memory
    • Minimum value is -9,223,372,036,854,775,808
    • Maximum value is 9,223,372,036,854,775,807
    • Not commonly used compared with others because it uses more memory. Useful when very large range of values is needed
    • Example:
      long a = -9800900200l;
      long b = 0;
      long c = 5300100700l;
      
      Note that you have to add l at the end of a very large number to tell Java it's a literal long value.
It is possible to assign values from different numeric data types. As long as the result is stored to a variable with enough number of bits. For example:
byte a = 8;
int b = a;
The above is acceptable beause a has 8 bits (byte) while b has 32 bits (int). The variable b here will also have the value of 8.
This however is not acceptable.
int a = 8;
byte b = a;
Becase this is trying to store a variable with more bits (32 bits) to a variable with less bits(8 bits). The resulting variable will not be able to handle it.
You can also mix numeric dat types in calculations. The result will follow the highest number of bits from all the numbers involved. Example:
byte a = 10;
short b = 5;
int c = 200;
int d = a + b + c;
The above code is acceptable. The result of a + b + c is automatically of int type because operand c has the highest number of bits.
The example below is invalid:
byte a = 10;
short b = 5;
int c = 200;
short d = a + b + c;
Because a + b + c is int (32 bits) which can't be assigned to a short (16 bits).

float and double

The data types float and double are also similar to each other.
  • Represents numbers that can have a decimal place (E.g. 9.75)
  • Uses IEEE 754 floating point standard
  • Internal representation is similar to a scientific notation.
    • can have very wide range of values
    • trade off with precision. not ideal when accuracy is very important (E.g. money)
  • float uses 32 bit of memory. More commonly used compared to double. Example:
    float a = -10.15f;
    
    Note that you have to add f at the end of a literal number to denote that it's a literal float.
  • double uses 64 bit of memory. used for computations that needs higher precision than float. Example:
    double a = -200.25d;
    
    Note that you have to add d at the end of a literal number to denote that it's a literal double.
Since float has 32 bits and double has 64 bits, it is allowed to assign float values to a double, but it is not allowed to store double value to a float.

This is valid:

float a = 10;
double b = a;


This is invalid:

double b = 20;
float a = b;


Both float and double can store any of the numeric data types byte, short, int, and long. Example:

byte a = 10;
short b = 2000;
int c = 3000000;
long d = 4000000000l;

float f1 = a; float f2 = b; float f3 = c; float f4 = d;

double d1 = a; double d2 = b; double d3 = c; double d4 = d;

When computations contains either a float or a double operand, the result will automatically be either of the two - which ever has the highest number of bits.


This is valid because a + b will become float

int a = 5;
float b = 10;
float c = a + b;


This is invalid because a + b is float, which is not allowed to be assigned to an int.

int a = 5;
float b = 10;
int c = a + b;


This is valid because a + b will become double.

float a = 5;
double b = 10;
double c = a + b;


This is invalid because a + b is double (64 bits), which can't be assigned to a float (32 bits).

float a = 5;
double b = 10;
float c = a + b;

boolean

Boolean is the smallest data type in Java.
  • Needs only 1 bit to store the value
  • Stores logical information
  • Only two possible values: true an false
  • Useful for storing flags or information that is answerable with yes/no in the real world.
    Example :
    boolean isFullyPaid = false;
    boolean hasDriverLicense = true;
    boolean isMarried = true;
    

char

Char can store a single character.
  • can store any English alphabet
  • can store a number
  • can store a special character. E.g. !, @, #, $, %, ^, &, *, (, ), ¢, £, ¥
  • can store unicode (16 bit) character.
    • Unicode is a computer industry standard for representing text/string data
    • supports most of the writing system in the world.
      This means it can represent a character languages other than English
  • Examples :
    char a = 'x';
    char b = '1';
    char c = '#';
    char d = '£';
    char e = '?';
    
    Note that literal char are enclosed with single quotes.

This statement is not valid because char can't have more than 1 character.

char a = 'ab';

Notes

Future tutorials will show how to use primitive data types to construct more complex structures.
Tags: beginner tutorial, console