Different Java data types explained with Examples

 

Introduction to Java data types

Java is a statically-typed language that means that all variables must be declared before they can be used. Each variable that we define in java has a special data type that specifies the different sizes and values that can be stored in the variable. In Java, there are major two types of data types; Primitive data type and non-primitive data type. In this tutorial, we will have a look at java data types.

We will learn about primitive data types and non-primitive data types by taking various examples. Moreover, we will also cover the different types of these variables and their sizes as well. All in all, this tutorial is going to have all the details about Java data types.

Advertisement

 

Getting started with Java data types

Variables are nothing but reserved memory locations to store values. This means that when we create a variable in Java, we actually reserve some space in the memory. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, we are able to store integers, decimals, or characters in these variables.

Now the Java Data types define the values that a variable can take, for example, if a variable has a string data type, it can only take string values. In java, we have two categories of data types; Primitive and non-primitive data types. See the following diagram which shows the different types of these java data types.

java data types

 

In the following sections, we will cover each of these data types along with taking various examples.

 

Primitive Java data types

The Java programming language is statically typed, which means that all variables must first be declared before they can be used. This involves declaring and initializing

int data = 10;

Doing this tells the java program that a variable named "data" exists and that holds a numeric value of 10. In the java program, the variable's data type determines the values it may contain and the operations that may be performed on it. Moreover, java programming also supports other primitive data types as well.

In the following section, we will see each of these primitive data types in more detail.

Advertisement

 

Example of Java char data type

The char data type in Java is used to store a single character and it must be quoted inside single quotation marks. In Java, a character is represented by a 16-bit Unicode. The following is the simple syntax to define java char data type.

char VariableName = 'B';

The single character can be a digit, symbol, or any alphabet. The default value is 0 if we will not initialize the value. The range of char data type is form '\u0000' (0) to '\uffff' (65535).  Now let us take an example and see how we can create and use char data type in java programming. See the following example.

// java class named Main
public class Main{

   // main method
   public static void main(String args[])
 {

   // declaring char
   char section;

   // initializing char
   section = 'A';

   // printing the char data type
   System.out.println("Your section is : "+section);
 }
}

Output:

Your section is : A

One interesting thing is that we can perform arithmetic operations on char data types as well. See the following example which increments the value and prints out a new value. See the example below.

// java class named Main
public class Main{

   // main method
   public static void main(String args[])
 {
   // declaring char
   char section;

   // initializing char
   section = 'A';

   // incrementing value
   section++;

   // printing the char data type
   System.out.println("Your section is : "+section);
 }
}

Output:

Your section is : B

Interestingly the output is B, not A because we increment the initial value and it is allowed as char is an unsigned 16-bit type.

 

Examples of Java integer data type

Integers data types can store the whole numbers either positive or negative that should not contain any decimal places. Some of the valid integer data types are byte, short, int, and long. The type mainly depends on the value or the range of the variable. Let us now have a look closely at each of these data types by taking examples.

 

Example-1: Java byte data type

The byte data type is mostly used in large arrays which need memory savings. It saves memory as it is 4 times smaller than an integer. The following is the simple syntax of byte data type.

Advertisement
byte data = 10 ;

Its size is one byte which is 8 bits and its range is from -128 to 127. Now let us take an example of the java byte.

// java class named Main
public class Main{

   // main method
   public static void main(String args[])
 {
   // declaring variable;
   byte data = 10;

   // printing the byte data type
   System.out.println("The value is  : "+ data);
 }
}

Output:

The value is : 10

 

Example-2: Java short data type

Similar to the byte data type, another integer data type is the short data type which is also used to save memory in large arrays. The short data type is a 16-bit signed 2’s complement integer. It is half of an int (integer). The following is the simple syntax of short data type.

short data= 10 ;

The short data type is 2 bytes which are 16-bits and its range is from -32768 to 32767. See the following example of a short data type.

// java class named Main
public class Main{

   // main method
   public static void main(String args[])
 {
   // declaring variable;
   short data = 10;

   // printing the short java data types
   System.out.println("The value is  : "+ data);
 }
}

Output:

The value is : 10

 

Example-3: Java int data  type

Now let us take a look at int data type. Generally, we prefer to use int data type for integral values unless if there is no issue with memory. The int data type is a 32-bit signed 2’s complement integer.

The following is simple syntax of Java int data type.

int data = 10;

The int data type is of 4 bytes (32 bits) and has range from –2,147,483,648 to 2,147,483,647. See the example below which uses int data type.

// java class named Main
public class Main{

   // main method
   public static void main(String args[])
 {
   // declaring variable;
   int data = 10;

   // printing the int java data types
   System.out.println("The value is  : "+ data);
 }
}

Output:

The value is : 10

 

Example-4: Java long data type

Now let us have a look at the long data type. The long data type is a 64-bit signed 2’s complement integers. It is used when int data type cannot hold a value. See the following syntax of long data type.

long data = 100;

The range of long data type is from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Now let us take an example using the long data type. See the example below:

// java class named Main
public class Main{

   // main method
   public static void main(String args[])
 {
   // declaring variable;
   long data = 100;
   // printing the long java data types
   System.out.println("The value is  : "+ data);
 }
}

Output:

The value is : 100

 

Example of Java floating data type

Floating data types as their name suggests, store the floating-point values or real numbers, which have floating decimal places. The simple syntax of Java floating data type is as follows;

float data = 10.98;

It is again 4 bytes (32 bits) data types. It can contain real numbers up to 7 decimal digits. Now let us take an example of java float data type. See the example below;

// java class named Main
public class Main{

   // main method
   public static void main(String args[])
 {
   // declaring variable;
   float data = 1;

   // printing the float java data types
   System.out.println("The value is  : "+ data);
 }
}

Output:

The value is : 1.0

Notice that even we assigned a whole number but it give output as a floating number because the data type of variable is float.

 

Example of Java boolean data type

A boolean data type is a double valued data type that is declared with the boolean keyword. It is capable of storing only two possible values; true and false. It is used for flag generations, to check the true or false conditions. Boolean data type stores only 1-bit information and size is not precisely defined.

The syntax of the boolean data type in java is as follows;

boolean data = true;

The default value is False. Now let us take a simple example and see how we can use it. See the example below;

// java class named Main
public class Main{

   // main method
   public static void main(String args[])
 {
   //   declaring variable;
   boolean data = true;
   if (data == true){
       // printing the boolean java data types
   System.out.println("The value is  : "+ data);
   }   
 }
}  

Output:

The value is : true

 

Non-primitive Java data types

Unlike primitive data types, the non-primitive data types are created by programmers. They are not predefined in java like primitive data types. These data types are used to store a group of values or several values. When we define a variable of non-primitive data type, it references a memory location where data is stored in the heap memory. That is, it references a memory where an object is actually placed.

Therefore, a non-primitive data type variable is also called referenced data type in Java or simply object reference variable. Some of the non-primitive data types include strings, classes, and arrays. In this section, we will have a look at these non-primitive data types along with examples.

 

Example of Java strings data type

The String data type is used to store a sequence or array of characters. But in Java, a string is actually an object that represents an array or sequence of characters.  The following is a simple syntax of java string data type.

String name_of_string = "text"

This is the simplest way of creating a String data type. See the following example which creates a string and then prints it out.

// java class named Main
public class Main{
   // main method
   public static void main(String args[])
 {
   //   declaring variable;
   String name = "Bashir";
   // printing the String java data types
   System.out.println("The name is  : "+ name);
 }
}

Output:

The name is : Bashir

 

There is another way to declare and initialize a string using a new key word. The syntax looks like this;

String name_of_string = new String("Text");

Now see the following example which uses this method to create String type.

// java class named Main
public class Main{
   // main method
   public static void main(String args[])
 {
   // creating object;
   String name = new String("Bashir");
   // printing the String java data types
   System.out.println("The name is  : "+ name);
 }
}

Output:

The name is : Bashir

 

Example of Java arrays data type

Java array is an object which contains elements of a similar data type. The elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array. The following is the simple syntax of the java array data type.

data_type  Array_name[] = new data_type[size]

Now let us take an example of java array data type and see how we can use it in our program. See the following example.

// java class named Main
public class Main{
   // main method
   public static void main(String args[])
 {
   // declaring array;
   String names[] = new String[3];
   // initializing array
   names[0] = "bashir";
   names[1] = "alam";
   names[2] = "khan";
   // for loop to iterate
   for (int i=0; i<3; i++){
       // printing the arry java data types
       System.out.println("The name is  : "+ names[i]);
   }
 }
}

Output:

The name is : bashir
The name is : alam
The name is : khan

 

Example of Java class data type

A class is a collection of objects of the same type. It is a user-defined blueprint or prototype which defines the behavior or state of objects. Class and objects are the basic elements of Object-Oriented Programming that represent real-world entities. We have used the class in our previous examples as well. Here is a simple example of a java class.

// java class named Main
public class Main{
   // statements and methods of the class goes here
}

 

Summary

Data types specify the different sizes and values that can be stored in the variable. In the Java programming language, there are basically two main types of data types that are primitive and non-primitive data types. Primitive data types again include four main sub-types including char, integer, float, and boolean. On the other hand, non-primitive data types include strings, arrays, classes, and others.

In this tutorial, we look closely at each of these types. We learn the java syntax of each one along with various examples. All in all, this tutorial covers everything that you need to know in order to get started with java data types.

 

Further Reading

Java data types
java data types documentation
data types

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment