Java Primitive Data Types

Java Primitive Data Types

A data type informs the compiler what type of data a variable is permitted to store. Data type declaration should be done when the variable is declared. It is required in Java as Java is a strongly typed language. The data type indicates what possible values that can be assigned to variable and possible operations that can be performed. The language system allocates memory to the variable on the type of data it can hold like an integer or double etc.

String in Java is not a data type. String is a class from java.lang package. String is not a keyword; for that matter no class is a keyword. Strings are used very often in coding like data types.

Java supports 8 data types that can be divided into 4 broad categories.

All data types are keywords. As a rule, all keywords should be written in lowercase. Java does not support unsigned data types; every data type is implicitly signed (except char). "unsigned" is not a keyword of Java. Note that null, true and false are also not keywords.

  1. Whole numbers – byte, short, int, long
  2. Floating-point numbers – float, double
  3. Character values – char
  4. Boolean values – boolean

Data type Memory it takes Range of values accepted
byte 1 byte (8 bits) – 128 to +127 (27 to 27-1)
short 2 bytes (16 bits) -32,768 to + 32,767 (215 to 215-1)
int 4 bytes (32 bits) -2,147,483,648 to +2,147,483,647 (231 to 231-1)
long 8 bytes (64 bits) -9.223372036854776E18 to +9.223372036854775E18 (263 to 263-1)
float 4 bytes (32 bits) single-precision. 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative)
double 8 bytes (64 bits) double-precision. 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative)
char 2 bytes (16 bits) 0 to 65,535 (0 to 216-1)
boolean 1 byte (8 bits) true or false
1. Whole Numbers

In this division, 4 data types exist.

  1. byte
  2. short
  3. int
  4. long

Following explains the above 4 whole numbers.

byte: It occupies a memory of 1 byte (8-bits). It takes a value within the range of -128 to +127 (both inclusive). Byte data type is mostly used in arrays as byte array.

short: It takes 2 bytes (16 bits) memory. It takes a value within the range of -32,768 to + 32,767 (both inclusive). It is advised to go for short instead of int when the value to store fits into short, to save memory.

int: It holds a memory of 4 bytes (32 bits). The value that can be assigned should with the range of -2,147,483,648 to +2,147,483,647 (both inclusive). This data type is mostly used to store a whole number in Java.

long: It takes a memory of 8 bytes (64 bits). The range of values is -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. To declare a long data type suffix the literal with l or L; but it is not mandatory in regular coding.

Examples:

byte price = 10;
short price = 10;
int price = 10;
long price = 10L;

As L is not mandatory, it can also assigned as

long price = 10;

Observe, all values are 10 only. To store a 10 value, it is wise to choose byte instead of long.

2. Floating-point Numbers

There exist two data types in this category – float and double.

float: This data type gives single precision of 32-bit as specified by IEEE 754. The float value must be suffixed with either F or f and is mandatory.

Double: This data type gives double precision of 64-bit as specified by IEEE 754. It is preferred over float where the most precision is required as in currency calculations.

Examples:

double price = 10.5;
float price = 10.5F;
or
float price = 10.5f;

Writing without f, raises compilation error (as in float price = 10.5;)

3. char Data Type

The char data type is implicitly unsigned as a character in any language is positive by default; there is no negative character. A char value should be put within single quotes (like a string value in double quotes).

char: It occupies 16 bits (2 bytes) of memory. The range of values it can take are 0 to (Unicode value '\u0000') to 65,535 (Unicode value '\uffff').

Examples:

char ch1 = 'A';
char ch2 = 'a';

4. boolean Data type

The boolean data type is strange for C/C++ people. The same work of boolean is done in C/C++ using flags. Boolan avoids flags. This data type is used in control structures for validation in if etc. It takes only two values of true or false. A boolean value cannot be casted to any other data type. We say, boolean is incompatible for conversion. Even though, internally boolean takes only one bit of memory, it is expressed as 1 byte as byte is the minimum measurable unit in memory.

Usage of boolean in control structures

Following program uses boolean variable raining in if structure.

A Java programmer can find out himself the minimum and maximum value a data type takes. Following program illustrates.


Java Primitive Data Types
Output screen of MinMaxRange.java of Java Primitive Data Types

The above program uses Byte, Short, Integer, Long, Float and Double classes. These are known as wrapper classes. MIN_VALUE and MAX_VALUE are defined as public, static, final variables in wrapper classes. These variables represent the minimum and maximum values a data type can hold.

17 thoughts on “Java Primitive Data Types

    1. S. Nageswara Rao, Corporate Trainer Post author

      Java char size is two bytes ranging from 0 to 65,535. This range is called Unicode where as ASCII is one byte ranging from 0 to 255. Unicode accommodates ASCII and from 256 onwards adds its own. That why we say, ASCII code is a subset of Unicode. From 256 onwards, Java adds other language (other than English) characters,

  1. upender

    Hi Sir,
    Your Material is very good. In the topic “JAVA INTRODUCTION” ,”Java Primitive Data Types ” I noticed a Mistake.
    In Datatype table for “char” datatype you have written as (0 to 2^8-1) but it is (0 to 2^16-1) .

  2. Ganesh Dutt

    Respected Sir,

    There is confusion in the range of boolean data type. As well known that boolean represents the two logical values true or false i.e. 1(true) or 0(false).This is indicating that the number of bits required to store their range is only one.So range of boolean data type should be 1 bit(max). [I am beginner,might be wrong]
    But,some books(e.g.-Khalid Mughal) write that width for boolean is ‘not applicable’ but do not give proper reasoning.

    Please provide clearance about this.

    Thanks.

  3. p.pavan

    Hello sir,
    I have a query regarding byte range in java. I know that byte takes 8
    bits for storage.
    2^8=256, then why the range is limited to 127? I know it must be due
    to representation of negative numbers.
    but, I would like to know the logic behind differentiating between
    negative and positive numbers and how they are stored in 8-bit form.
    Thanks.

  4. sai krishna madamshetty

    hi sir,

    am one of ur student.I did not listen to ur classes while u r teaching core java.But after completion of my graduation i came to know the use of java language.Now am learning from way2java.com.Thank u sir :)

    Yours sincerely,
    Sai Krishna.M

Leave a Reply

Your email address will not be published. Required fields are marked *