logo codesdope

Data Types in Java

What if you are asked what is 2? Some of you will say that it is a number, some will say it is an integer and some will say that it is a digit.

This is exactly what we are going to learn in this section i.e. what type of data (data-type) Java uses.

Most commonly used data types in Java are int (integer), char (character), float (number having decimal), double (number having decimal), String (collection of characters) and boolean (true or false).

First, let's see the following code.

int n;
n = 4;

As discussed earlier, here int n; allocates some space in the memory to the variable 'n'.

n = 4 assigns a value 4 to the variable 'n'.

So, int n is declaring that variable 'n' is an int and allocates some space in the memory to 'n'.
Then n = 4 assigns 4 to 'n'. So, now 'n' is 4.

Thus, if you want to give a character value to 'n', then use char before 'n' (instead of int). Similarly, if we want to give a float or double value to 'x', then use float or double before 'x' respectively.

The following table states different data types along with the maximum and minimum value they can take.

Data Type Maximum Value Minimum Value
int 2,147,483,647 - 2,147,483,648
float 3.4028235E38 1.4E-45
double 1.7976931348623157E308 4.9E-324
char 65,535 0
short 32767 -32768
long 9223372036854775807 -9223372036854775808
10E5 means 105 i.e. 100000.

Let's see an example of float, double, char and boolean values.

class D1{
  public static void main(String[] args){
    double b = 123.43555;
    char c = 'e';
    boolean d = true;
    System.out.println("Double :" + b);
    System.out.println("Character :" + c);
    System.out.println("Boolean :" + d);
Double :123.43555
Character :e
Boolean :true

We had used + to join two strings in the last chapter. Here, we are joining a string and one another data-type. So, both will be combined and printed on the screen.

One thing to note here that b in the first 'println' is not inside " ". So, its value will be printed. Same is for c and d. Next example is over this.

So, Double : will be printed and then the value of b i.e., 123.43555 will be printed, thus giving the output Double :123.43555.

class D{
  public static void main(String[] args){
    int x = 1, y = 5;

As we have seen, things written inside " " got printed as it is, without evaluating and things which were not inside " " got evaluated first and then their values got printed. E.g. "x"+"y" got printed as xy ( without evaluation ) but x+y got evaluated first as 1+5 i.e., 6 and then 6 got printed.

We will learn about String later in the topic Characters and Strings.

Primitive and Non-Primitive Data types

Using data types is so easy, isn't it ?

All the data types are broadly classified into primitive and non-primitive.

Primitive Data type

These are predefined (already defined) data types in Java.

There are eight predefined data types in Java which are

int   float   double   short   long   char   boolean   byte

Non-Primitive Data type

Non-primitive data type refers to an object.

At present, no need to go into its details as we will learn about objects later.

Type Casting

Type Casting is the conversion of a variable from one data type to another data type. For example, if we want to convert a char value to an int value.

Type Conversions are of two types - implicit and explicit.

Implicit Conversion

Suppose we are adding two numbers. The first number is of type int and the second number is of type float. Since we cannot add an int and a float, so both the numbers have to be of the same data type i.e. either both are int or both are float. Since float is a larger data type than int, therefore int variable gets converted into float and then both the float variables add up.

All the character variables get converted to integers while performing arithmetic operations or in any such other expression.
class D2{
  public static void main(String[] args){
    int a = 10;
    char ch = 'h';
    int sum = a + ch;

In the above code, when 'a' and 'ch' are added, the integer value of 'ch' (ASCII value) i.e. 104 is added to the integer value of 'a' to produce a sum of 114. Every character has an ASCII value. You can get ASCII chart from here.

Explicit Conversion

We can also convert values from one data type to another as follows:

( data-type ) expression ;

Consider an example.

class D3{
  public static void main(String[] args){
    int sum = 23;
    int n = 7;
    double avg;
    avg = (double)sum/n;
    System.out.println("Average = " + avg);
Average = 3.2857142857142856

In this example, since avg is declared of type double, therefore we are converting the sum to type double by writing (double)sum/n ( as int/int will give int in Java ). You will understand the use of doing so in the next chapter.

You will understand many things only by practicing. So, solve questions from the practice section.

Without practice, your knowledge is poison.

Doubt? Ask question

Welcome.please sign up.


Welcome.please login.