Course Content‎ > ‎Session 2‎ > ‎

Primitive Data Types

Integer Data Types

Integer data types are used to represent integer numbers (i.e. numbers without any decimal part). Integer data types represent signed integers i.e. both positive and negative integers. Java does not support the concept of unsigned types (unsigned type means only positive numbers) and therefore, in Java all the number values are of signed type i.e. they can be positive or negative. There are four integer data types:

Type   Size       Range of Values
byte     1 byte       -128 to 127
short    2 bytes      -32768 to 32767
int      4 bytes      -2147483648 to 2147483647
long     8 bytes      -9,223,372,036,854,775,808L to 9,223,372,036,854,775,807L
 
 
Declaring integer variables
 
By default integer literals are stored as type int. But we can make integers of type long by appending the letter l or L at the end of the number. An int in Java is always a 32-bit integer.  In programming languages like C or C++, an int can be 16 or 32-bit integer or any other size, depending on the type of the computer system or operating system. A C program that runs well on one computer system, may exhibit integer overflow when it runs on some other computer system. Integer overflow means that the number assigned to the integer variable is not within the specified range. In Java, the sizes of all numeric types are platform independent, which makes Java a highly portable language. Let's look at some examples to declare integer variables:
 
    int count, i;

    short ans;

    long magnitude;

    byte red, green, blue;


Floating Point Data Types

Floating point data types are used to represent real numbers i.e. numbers with decimal parts. There are two floating point types:

Type       Size    Precision
float      4 bytes    single-precision i.e. precision up to 6-7 digits after the decimal point
double       8 bytes    double-precision i.e. precision upto 15 digits after the decimal point

 

Declaration of floating-point data types

    float temperature;

    double speed, temperature;

By default decimal type numbers are of type double in Java. You must suffix f or F to the decimal numbers, if you want that they are treated as type float by the compiler. For example, 

    float num = 29.2f;

    or

    float num = 29.2F;

Boolean Data Type
 
The boolean data type is used to store values with one of the two states: true or false. Boolean value is a 1-bit logical quantity. 
 
Declaration of boolean data type

    boolean Flag;

The variable Flag is a boolean variable, which can either be true or false.

Character Data Type

char data type represents the characters in the Unicode encoding scheme. (Please refer to appendix1 to find out what is Unicode encoding scheme). Unicode was designed to handle essentially all the characters in all the written languages in the world. Unicode character set is composed of  2-byte code, which allows 65,536 characters. ASCII/ANSI code-set is a 1-byte code, which means only 255 characters are allowed. char data type is useful for storing only single character. Single quotes are used to denote a char type variables. For example 'H' is a character where as "H" is a string containing single character.

Declaration of character variables
 
    char answer;

All the primitive data types i.e. int, char, float are in lower case only. If you write Float instead of float, it will  mean a different thing to the compiler. There are classes with the same name as primitive data types but they their first letter is in uppercase, like Char, Float, Double, Boolean etc. 


String Data Type

Java does not have any built-in string data type. But standard Java library contains a predefined class called as String. Each quoted string is an instance of  the String class.

    String st1="";     

    String  today = "Tuesday";

When we declare a variable, according to the data type of the variable, those many  adjacent bytes are allocated to the variable in the memory. For example, 

    int a;

Since a is an int type of variable, it will be allocated 4 adjacent bytes in the memory of  the computer. The memory address of the variable a will be the address of the 1st byte of the 4 adjacent bytes allocated to it.
Comments