Data Types in Java

Data Types in Java

Data types represent the different values to be stored in the variable, for example if a variable has int data type, it can only take integer values.
In java, there are two types of data types:

  1. Primitive Data Types
  2. Non-Primitive Data Types

Primitive Data Types

In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and double.
Java developers included these data types to maintain the portability of java as the size of these primitive data types do not change from one operating system to another.

1) byte:

This can hold whole number between -128 and 127. Mostly used to save memory and when you are certain that the numbers would be in the limit specified by byte data type.

Example:

1
2
3
4
5
6
7
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JavaExample</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>{
        <span class="hljs-keyword">byte</span> num;
        num=<span class="hljs-number">123</span>;
        System.out.println(num);
    }
}
Output:

113

2) short:

This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767.

Example:

1
2
3
4
5
6
7
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JavaExample</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-keyword">short</span> num;
        num = <span class="hljs-number">150</span>;
        System.out.println(num);
    }
}
Output

150

3) int:

‘int’ is used when short is not large enough to hold the number, it has a wider range: -2,147,483,648 to 2,147,483,647.

Example:

1
2
3
4
5
6
7
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JavaExample</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-keyword">int</span> num;
        num = <span class="hljs-number">1245638</span>;
        System.out.println(num);
    }
}
Output

1245638

4) long:

‘long’ when int is not large enough to hold the value, it has wider range than int data type, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Example:

1
2
3
4
5
6
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JavaExample</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-keyword">long</span> num = -<span class="hljs-number">12332252626L</span>;
        System.out.println(num);
    }
}
Output:

-12332252626

5) double:

‘double’ is sufficient for holding 15 decimal digits.

Example:

1
2
3
4
5
6
7
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JavaExample</span> </span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
       
       <span class="hljs-keyword">double</span> num = -<span class="hljs-number">42937737.9</span>d;
       System.out.println(num);
   }
}
Output

-4.29377379E7

6) float:

‘float’ is sufficient for holding 6 to 7 decimal digits.

Example:

1
2
3
4
5
6
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JavaExample</span> </span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
       <span class="hljs-keyword">float</span> num = <span class="hljs-number">19.98f</span>;
       System.out.println(num);
   }
}
Output:

19.98

7) boolean:

‘boolean’ holds only two values either true of false.

Example:

1
2
3
4
5
6
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JavaExample</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
      <span class="hljs-keyword">boolean</span> b = <span class="hljs-keyword">false</span>;
      System.out.println(b);
  }
}
Output:

false

8) char:

‘char’ holds only characters values.

Example:

1
2
3
4
5
6
7
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JavaExample</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
     
      <span class="hljs-keyword">char</span> ch = <span class="hljs-string">'Z'</span>;
      System.out.println(ch);
  }
}
Output:

Z

Non-Primitive Data Types:

Arrays and Strings are non-primitive data types, we will discuss them later in the coming tutorials.

Literals in Java

A literal is any constant value that we assign to a variable in a Program.


1
<span class="hljs-keyword">int</span> num=<span class="hljs-number">10</span>;

Here value 10 is a Integer literal that is assigned to the variable ‘num’.


1
<span class="hljs-keyword">char</span> ch =A;

Here A is a char literal assigned to ‘ch’.

Integer Literals:

Integer literals are assigned to the variables of data type byte, short, int and long.


1
2
3
4
<span class="hljs-keyword">byte</span> b = <span class="hljs-number">100</span>;
<span class="hljs-keyword">short</span> s = <span class="hljs-number">200</span>;
<span class="hljs-keyword">int</span> num = <span class="hljs-number">13313131</span>;
<span class="hljs-keyword">long</span> l = <span class="hljs-number">928389283L</span>;

Float Literals:

Float literals are assigned to the variable of the data type float and double.


1
2
<span class="hljs-keyword">double</span> num1 = <span class="hljs-number">22.4</span>;
<span class="hljs-keyword">float</span> num2 = <span class="hljs-number">22.4f</span>;
NOTE: Always suffix float value with the “f” else compiler will consider it as double.

Char and String Literal:

These literals are used for char and String type variables.


1
2
<span class="hljs-keyword">char</span> ch = <span class="hljs-string">'Z'</span>;
String str = <span class="hljs-string">"BeginnersBook"</span>;

Related Post

Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Create UITableView (Table View) By the use of stor... How to use UITableView in Above IOS 8 and Swift (UITableView) In this tutorial we are study how to use the TableView in Swift. One of the most common...
Switch Case Statement Switch Case Statement The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of c...
New features in XCode 10 Supported Configurations Xcode 10 beta requires a Mac running macOS 10.13.4 or later Xcode 10 beta includes SDKs for iOS 12, watchOS 5, macOS ...
Setting Class Path for Java Setting Class Path for Java In this tutorial we are study how we can set the java JDK class path. Java is an open source software, it is is freely...
Continue Statement in JAVA Continue Statement in JAVA Suppose you are working with loops. Sometime it is desirable terminate the loop or skip some statement inside the loop wit...

Leave a Reply

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

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.