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

Custom View by use of xib in swift Custom View by use of xib in swift We can create the custom and common view by the use xib in swift 3.1 and xcode 8.3.2 . Custom views are common and...
Strings String can be represented as a single-dimensional character type array. Strings in C are group of characters, digits, and symbols enclosed in quotatio...
How to install flutter | Flutter tutorial Flutter is the google’s mobile app SDK for develop the high-quality native app for iOS and android. We can develop the app by use of DART and javascri...
First Java Program First Java Program NOTE: You need to have java installed on your system. Step 1: Open a text editor, like Notepad on windows and TextEdit on Mac. W...
New Tools for 64-bit Support in Xcode 9.3   New Tools for 64-bit Support in Xcode 9.3 The last macOS release to support 32-bit apps without compromise is macOS High Sierra. Make sure fu...
Compile C/C++ program (Dev-C/C++) what is Dev-C/C++? Dev-C/C++, developed by Bloodshed Software, is a fully featured graphical IDE(Integrated Development Environment),which ...

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.