Basic Data Types

Introduction to Data Types

Here we will be discussing about the basic data types that are used in Java. But, before start discussing that in details, I would like to give you a small example regarding how to declare and initialize a variable in Java and how we use data types while using declaring a variable.

Note: We will discuss “variable types” in Java later in a separate unit in more details.

Example of declaring a variable in Java

int a ;

float b;

double c;

Example of initializing a variable in Java

int a = 10;

char c=’a’;

While declaring a variable, a reference is created in the stack. But, when we assign a value to it, object is allocated for that particular variable in the heap.

Types of Data types in Java

What are Primitive Data Types?

There are some variables which are predefined by Java and Java reserves keywords for them. They are called Primitive Data Types in Java.


Types of Primitive Data Types

There are 8 primitive data types in Java – byte, short, int, long, char, float, double and boolean.

  • byte, int, short and long are Integers.
  • float and double represents floating point numbers with fraction values.
  • char represents Characters in Java.
  • boolean is special type of data type which represents only 2 values: {true, false}.

What are Object/Reference Data Types?

Object/reference data types are those which depends on the class and not on the primitive data types.

Few point to remember regarding Object/Reference Data types:

1. Example:

Integer i = new Integer(10);

2. Reference variable are created using default constructors of the class.

3. If no value is assigned to it after declaring the variable, the default value for reference variable will be null.

4. Array (yet to discuss) and Class objects comes under this type of variables.

So, it’s time to redraw the tree structure for Data Types in Java as follows:

Assigning Values to Variables directly using Java Literals

We can assign constants (i.e. fix values) using Java literals. We can use this method to assign values to all of the primitive data types, and in few Object/Reference data types also. But, definitely, the types of representation of Java literals can differ depending of which kind of data types we are assigning value to.

Assigning Value to Primitive variables:

Java Numbers 

byte b = 1;

int  i = 10;

short  s = 23;

long  l = 23456;

double  d = 23.67;

float f = 23.67f;

Assigning the Number only on right hand side

Java Characters 

char c = 't'; Assigning the value in '' literals

Boolean variables

boolean bool = false; Assigning true/false value

Assigning Value to Object/Reference variables

Java Strings

String s = "Test String"; Assigning the value in "" literals

Java Numbers in Object/Reference Variable

Byte b = 1;

Integer i = 10;

Short s = 23;                         

Long l = 23456L;

Double d = 23.67;

Float f = 23.67f;

Assigning the Number only on right hand side

Boolean variables

Boolean bool = true; Assigning true/false value

Java Characters

Character c = ‘t’; Assigning the value in '' literals

Assigning Values to Variables without using Java Literals

We can also assign value to the primitive variables or Object/Reference variable by creating objects.

For example

char c = new Character('a');

But how and why we can assign a Character Object in char primitive data type char, which we will discuss later. For your reference, this property is called "Autoboxing".

Next > < Prev
Scroll to Top