Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

September 29, 2014 Categories: Core Java Basics. No Comments on Java data types

In this article you will learn about Java data types. You will look at what is a data type, different primitive data types in Java and some examples on each primitive type in Java.

Data type: A data type specifies the type of value a variable can store or the type of an expression. Java is a strongly typed language means that you should specify the type of a variable before using it in a Java program.

There are eight primitive types in Java namely: byte, short, long, int, float, double, char and boolean. They can be categorized as shown below:

Java data types

Also every class and interface existing in Java is also a type (predefined). By creating a class or an interface, you are creating a user defined type. Above eight data types are called as primitive in the sense they are not maintained as objects in memory and they can be used to create user defined types like classes.

Java designers has included these eight primitive data types only due to performance reasons (primitive types are faster). The size and range of values for each primitive data type is specified below:

Java data types sizes and ranges

byte: The smallest integer type available in Java is byte. Its size is 8 bits and can store values within the range -128 to 127. byte data type can be useful while working with a stream of data over a network or a file. Declaring variables of the type byte is as shown below:

byte a, b;

short: Perhaps the least used integer type in Java is short. Its size is 16 bits and it can store values within the range -32,768 to 32,767. Declaring variables of the type short is as shown below:

short s;

int: Most widely used type for working with integers in Java programs is int. Its size is 32 bits and it can store values within the range -2 billion to +2 billion. Variables of type int are frequently used in loops and for indexing arrays. Declaring variables of the type int is as shown below:

int i, j;

long: The size of long integer type is 64 bits and can store up to quite a large range of integer values. It is generally used in programs which work with large integer values. Declaring variables of the type long is as shown below:

long a, b;

All the four integer types, byte, short, long and int are signed types. In Java, unsigned types are not supported.

float: The type float is used to specify a single-precision value that uses 32 bits for storage. Single precision is faster on some processors and takes half as much space as double precision. The type float is used to work with fractional values where the precision is not that important. Declaring variables of type float is as shown below:

float f;

double: The type double is used to specify a double-precision value that uses 64 bits of storage. Double precision is faster on most of the modern processors which are optimized for mathematical calculations. The type double is used to work with larger fractional values and when the precision of the fractional value is important. Declaring variables of the type double is as shown below:

double d;

char: The data type which allows us to store characters is the char type. Unlike C and C++, the size of char type in Java is 16 bits. Java uses Unicode to represent characters. Unicode supports most of the international languages, whereas, C and C++ only supports ASCII.

The char type can also be used to store integer values from 0 to 65,535. Operators allowed on integer types are also allowed on char type. Declaring a char type variable is as shown below:

char ch;

boolean: The boolean type in Java is used to store a logical value, either true or false. Size of boolean is 1 bit. Variables of the type boolean are used in control statements extensively. Declaring a variable of the type boolean is as shown below:

boolean b;

 

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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

Scroll Up