13 Different Data Types in Java with Examples

One of the main ideas you ll need to understand when learning to code in Java is types.html’ >data types. A data type is a classification that specifies the sort of data that a variable can carry. There are 13 different data types in Java that you must know in order to produce good code.

Why Data Types In Java Are Important

Data types are crucial inJavabecause they control how much memory a variable will take up and the types of operations that can be performed on it. Choosing the right data type for a variable might help you improve your code and avoid problems.

Our goal is to give readers a thorough understanding of Java s 13 data types. We ll go over both primitive and non-primitive data types, including definitions, examples, and possible problems with both. By the end of this article, you ll have a better understanding of the various data types in Java and how to use them effectively when coding.

Data Types (Primitive)

Data types in Java programming are either primitive or non-primitive. Non-primitive data types are more complicated data types that can store several values. Data types (primitive) are simple data types that store a single value. Let s explore Java s eightprimitive data types.

They are fundamentaldata typesthat are a part of the Java language. Data types (primitive), unlike non-primitive data types, have no methods and are therefore not objects.

The 8 Data Types (Primitive) in Java


In Java, this is a data type (primitive) that holds an 8-bit integer value. It possesses a value range of 127 and -128. Large arrays often use bytes to conserve space.

byte myByte = 42;

In the example code above, the output will be 42.


The short data type represents a 16-bit integer value in the two s complement form. It possesses a maximum value of 32,767 and a minimum value of -32,768. We use shorts in array indices and in situations where memory space is at a premium.

short myShort = 1500;
System.out.println("My short value is: " + myShort);

In the above example, we declare a short variable named myShort and assign it a value of 1500. We then print out the value of myShort using the println() method. The output will be:

My short value is: 1500


In Java, the int is for storing whole numbers that can be either positive or negative. It possesses a maximum value of 231-1 and a minimum value of -231. We use integers for whole numbers in Java programming.

int age = 25;
System.out.println("My age is " + age);

In the above example, we declare a variable age of type int and assign it the value 25. The System.out.println() statement will print the message My age is 25 .


The long data type in Java is for storing whole numbers, specifically, 64-bit signed integers using two s complement representation. It possesses a minimum value of -263and a maximum value of 263-1. We use longs when a more extensive scope than int is what we want or while managing large numerical values.

long myNumber = 1234567890L;
System.out.println("The long number is: " + myNumber);

In the example above, we declare a variable myNumber of type long and assign it the value 1234567890L. The L at the end of the number tells Java that we should treat it as a long rather than an int.

The output will be: The long number is 1234567890


This is a number with a decimal point and it is represented in 32 bits. The float is not best for storing exact values like currencies. Floats are best to use for scientific calculations and graphics.

float x = 3.1415f;
System.out.println("The value of x is: " + x);

In the example above, the output will be: The value of x is: 3.1415


The double is a 64-bit floating-point number with double precision according to the IEEE 754 standard. It is frequently utilized for accurate figures like monetary values and intricate mathematical computations.

double num = 3.14159;

The example code above declares a double variable named num and initializes it with the value of pi. It then prints the value of num using the println() method.

The output is: 3.14159


This data type represents a single character using 16 bits of memory and follows the Unicode character encoding standard. It has a maximum value of \uffff and a minimum value of \u0000 (or 0). Characters are used to represent digits, letters, and symbols.

char myChar = 'A';

This will output the character A . The char is used to store a single character. The character A is assigned to the variable myChar.


This is just a true or false value. It is used for flow control and logical operations in Java programming.

boolean isRaining = true;
System.out.println("Is it raining? " + isRaining);

The above code declares a boolean variable isRaining and assigns it a value of true. The println statement then outputs the string Is it raining? true .

Potential Issues

One possible disadvantage of these data types is their restricted range and precision. The incorrect data type for a variable can cause problems or unexpected behavior in your software. Furthermore, some data types, such as floats, might be inaccurate and cause rounding-off mistakes. When working with basic data types, it s critical to select the suitable type for the data at hand and to be aware of any potential difficulties.

Non-Primitive Data Types

Non-primitive data types have a higher level of complexity than data types (primitive). They are frequently used to represent objects or sets of objects. Non-primitive data types, unlike basic data types, are not incorporated into the Java language. They are instead developed employing object-oriented programming ideas.


Anarrayis a group of elements of the same data type that can be accessed using a single name. InJava, arrays are used to hold collections of values like characters, integers, or objects. Arrays can be multi-dimensional or just one-dimensional.

int[] myArray = {1, 3, 3, 6, 5};


Strings are used to represent character sequences. They are represented by the String class in Java and are widely used in programming. Strings are immutable, which means they cannot be modified once we create them.

String greeting = "Hello Sir!";

The output will be: Hello Sir!


In Java, classes are used to represent objects. A class is a template or blueprint that is used tocreate objects. It includes methods and data elements that explain the behavior and characteristics of class objects.

public class Person 
    String name;
    int age;

    public Person(String name, int age) 
       this.name = name;
       this.age = age;

    public void introduce() 
       System.out.println("Hi, my name is " + name + " and I am " + age + " years old.");

In the above example, we create a class called Individual that has two instance variables. The name (of type String) and age (of type int). We also define a constructor method that takes a name and an age parameter and assigns their values to the corresponding instance variables.

Lastly, we define a method with the name introduce() that uses the println() method to print out a message that introduces the individual and their age.

To use this class, we could create a new Person object and call its introduce() method.


An interface is a set of abstract methods that have no implementation. A class in Java can use an interface to achieve a specified behavior. Interfaces define a set of behaviors that various classes can use.

public interface Trumpet 
   void makeSound();

This declares an interface called Trumpet which has a single method called makeSound that doesn t return anything (void). Any class that uses this interface will need to provide an implementation for the makeSound method.


An enum, which stands for enumeration, is a datatypethat has a fixed collection of values. An enum in Java represents a collection of constants. Enums commonly represent weekdays, months of the year, or even the suits in a deck of cards.

public class Main 
     enum WildAnimal 
        public static void main(String[] args) 
           WildAnimal myFavorite = WildAnimal.LION;
           System.out.println("My favorite African wild animal is the " + myFavorite);

In the above example, we define an enum called WildAnimal that has five possible values: LION, ELEPHANT, GIRAFFE, HIPPO, and RHINO. We then create a main method where we assign the LION value to a variable called myFavorite and print out a message that includes the value of myFavorite. The output of this program would be: My favorite African wild animal is the LION

Non-primitive data types have more functionality than basic data types because they construct using object-oriented programming concepts. While they provide greater flexibility and functionality than data types (primitive), they can be more challenging to work with and slow to execute. To select the proper data type for a given programming task, it is critical to fully understand the differences between primitive and non-primitive data types.

Final Thoughts

Data types in Java are critical for writing efficient and effective code. We have covered the 13 different data types in Java, including their definitions, ranges, and examples. We discussed the differences between primitive and non-primitive data types and their unique challenges. Understanding data types can help prevent errors and optimize performance. When working with Java programming, it s essential to have a solid grasp of the data types you re using. Remember to always choose the right data type for the job, be aware of the limitations of each type, and always test your code thoroughly. This could be the difference between writing great code or poor code.

Leave a Comment