Java Tutorial: Data types, Variables and Arrays

Like any other programming language, Java also has the fundamental elements like data types, variable and arrays. Java is a strictly typed language which means that every variables and expression used has a type defined and all the assignments are strictly checked for type compatibility.

To understand data type we must first know what is a variable? Variables are nothing but the basic storage units in a program. And the type of data a variable can hold is what we call the data type.

The various data types that Java supports are:

  • Integers – these are signed whole valued numbers. Data types and their size for integers are :
Java Tutorial for beginners| Data Types

Data Types

  • Floating-points – these represent a number with decimal point or factional part. Data types under this group are float (32 bits) and double (64 bits).
  • Characters – under this comes the char data type which is a Unicode to represent a character and is 16 bits long.
  • Boolean – used to store the logical values, true and false.

Thus there are total eight basic data types in Java.

Java being a strongly typed language does not allow any type mismatch or automatic type conversion. If a floating-point number or character is assigned to it, compilation error occurs.

Variable declaration:

As we know from the previous discussion that variables are storing units of the program. But before using a variable it must be defined with an identifier and the data type it will hold. This is called variable declaration. The basic format of declaring a variable is:

type identifier [=value]

type refers to the variable data type, identifier refers to the name of the variable and the value part is optional. More than one variable can be declared in the same line using just one data type.

Here are some examples of variable declaration:

int a, b, c;  // declares three ints, a, b, and c.

Byte x = 22;  // initializes z.

int d = 10, e, f = 25;  // declares three more ints, initializing d and f.

char z= ‘a’;  // the variable z has the value ‘a’.

double pi = 3.14159; // declares an approximation of pi.

Automatic type conversion occurs only when the destination type is larger than the source type. For example a byte type value can easily be assigned to an integer type variable.

 int a ;

byte b;


The problem arises when the destination type is smaller than the source type. For example if we want to assign an int value to a byte variable it becomes incompatible. Here we have to type cast in order to create a conversion between incompatible types. Casting is nothing but explicitly changing the type by writing the desired type in a first bracket.

(target-type) value

For example:

int a;

byte b;

b = (byte) a; // since here ‘b’ is a byte which is smaller than ‘a’( an int), ‘a’ has to be explicitly made byte (casting).


Array is a group of similar type variables referred to by a common name. Each variable in an array is denoted by an index number starting from zero. Arrays are declared as follows: 

type variable-name[]; 

By this we just declare the name of the array and the type of variables it will hold. But no space is actually allocated in the memory. We need to use the new keyword and mention the size of the array in order to actually create the array.

Array-variable= new type[size]; 

 For example if we want to create an integer type one-dimensional array of four elements we will write:

int arr[];

arr = new int[4];

Each element of the above array created is denoted as arr[0], arr[1], arr[2], arr[3]. Or the values can be directly given within the curly braces separated by commas. The second method is called array initializer and new keyword is not needed here.

int arr[]= {10,20,30,40};

Two-dimensional array is an array within an array. It is declared as:

int arrTwo[][]= new int[4][6];

This will create a matrix having 4 rows and 6 columns.

 Similarly arrays within arrays create multidimensional arrays.

An example to illustrate the concept of arrays:

Java tutorial for beginners | Java Variables and Arrays

Java Variables and Arrays


Second element of arr1 is: 8

Third element of arr2 is: 10

It must be remembered always that the index counting of array element starts with zero.


  1. According to Digital Millennium Copyright Act Title 17 Chapter 512 (c) (3)  has exclusive DIGITAL RIGHT over this format and content. Please read the legal Notice here…
  2. Reproduction or re-publication of this content is prohibited without written permission from
  3. While every attempt has been made to verify the information provided here; however assumes no responsibility for errors, omissions, results or contradictory interpretation of the content.
  4. Also, you should use this information as you see fit. Your particular situation may not be exactly suited to the information illustrated here.
  5. This tutorial is for informational and education purpose only. will not encourage any FINANCIAL terms and conditions from any party. We believe that every industry experts are doing the tutorials with us with consciousness of their individual social responsibilities.
  6. This article is about Java tutorial on Data types, Variables and arrays.

Related Posts