What are the different data types in Java?

Java offers eight primitive data types and numerous complex data types. These determine which values can be stored and displayed within a variable. Certain data types are assigned to all variables in Java.

What data types are there in Java?

In every programming language, there are various data types that contain specific objects and defined operations. Although the data type options offered by different languages often resemble each other, sometimes, there are significant differences. For example, if you compare Python and Java and their data types, you‘ll notice similarities between the two, but also numerous differences that make the languages suitable for different types of tasks.

Java employs two different kinds of data types: primitive and complex. Complex data types are also called reference types. Primitive and complex data types differ in terms of their size and determine which values can be stored in a variable. While primitive data types can only store simple values, reference types are used to create more complex structures and organise and manipulate large amounts of data.

If you want to learn how to code, it’s important not only to know the different data types, but also how to get the most out of each of them. Understanding how to best use data types is not only a question of understanding their functionality but also how much memory they use (or conserve).

Web Hosting
Secure, reliable hosting for your website
  • 99.9% uptime and super-fast loading
  • Advanced security features
  • Domain and email included

What are primitive data types in Java?

There are a total of eight different primitive data types in Java, which contain firmly defined value ranges. They can be divided into four categories: logical data types, integral data types, floating-point data types and character data types. They have no additional special capabilities and are also referred to as elementary data types. Each data type has a fixed number of bits. We’ll go over primitive data types in more detail below. Java uses the following primitive types:

  • boolean
  • byte
  • short
  • int or integer
  • long
  • float
  • double
  • char

boolean

Java’s boolean data type is not a numeric type. It only offers two possible values: true and false. It can be used to determine whether a condition applies (true) or not (false). It is a logical data type and its default value is false. It’s made up of 1 bit and has the following syntax:

boolean booleanVar;
java

byte

byte is an integral data type represented as a two’s complement value with a size of 8 bits or 1 byte. It is typically used to conserve memory in larger arrays. Its range of values spans from -128 to 127, and its default value is 0. Here’s its syntax:

byte byteVar;
java

short

short is also one of the integral data types in Java and is mainly used to save memory in larger arrays. The two’s complement value itself is 16 bits or 2 bytes in size and its value range extends from -32768 up to and including 32767. By default, its value is 0. This is how short is used:

short shortVar
java

int or integer

int or integer is also a two’s complement value and an integral data type with a size of 4 bytes or 32 bits. Its value range includes integers between -2147483648 and 2147483647 and its default value is 0. Its syntax is as follows:

int intVar
java

long

The largest integral data type is long. This applies firstly to its value range, which is between -9223372036854775808 and 9223372036854775807, and secondly to its own size, which is 8 bytes or 64 bits. The two’s complement value is therefore used if the other integral data types are not sufficient. However, it also requires by far the most memory. Its default value is 0. This is its syntax:

lomg longVar
java

float

float is a floating-point data type and is used to store real numbers. It has a size of 32 bits and complies with the IEEE 754 standard, which defines the standard representation of binary and decimal floating-point numbers in computers. The standard value of float is 0.0 and up to seven decimal places can be represented in its value range. Compared to double, however, float is not as precise and should therefore not be used for values where precision plays a decisive role. However, If this is not the case, you can use float to save space. The syntax for float is as follows:

float floatVar;
java

double

double is about twice as precise as float and fulfills a similar purpose. It is 64 bits in size and can represent up to 16 decimal places. Its default value is also 0.0. If you need values that are more precise, you should always use double, although this data type isn’t that precise either. If you want to work with absolutely exact values, you should use the BigDecimal class instead. The syntax of double is as follows:

double doubleVar;
java

char

char is a character data type. It stores characters based on the Unicode character set, enabling portability to numerous different platforms. Each character requires 2 bytes of memory. Its value range corresponds to ASCII (American Standard Code for Information Interchange) and is located between ‘\u0000’ (i.e., 0) and ‘\uffff’ (corresponds to 65535). The default value of char is ‘\u0000’ and its syntax looks like this:

char charVar
java

What are complex data types in Java?

The second type of data types in Java are the reference types, also referred to as complex data types. They are called reference types because they refer to objects. In contrast to primitive data types, they are not normally predefined, but are determined by the programmer (an exception to this is string). They are able to use methods and can also have the value 0 (in the sense of empty). While primitive data types start with a lowercase letter, reference types start with an uppercase letter. Here, we’ll take a look at the most important reference types.

Strings

String is a class that can be used to represent a sequence of characters, distinguishing this complex data type from the primitive data type char as well as other data types. A string exists as an object in the java.lang class. The various methods of the String class allow you to examine individual characters in the string, compare strings with each other, search strings and copy strings. Strings are identified by quotation marks. The syntax of this reference type looks like this:

<string_type> <string_variable> = "<string_sequence>";
java

Here’s an example of how strings work:

// How to create a string without a new operator
String a = "This is your new string";
/ / How to create a string with a new operator
String a1 = new string ("This is your new string");
java

Arrays

Arrays are used to store several values within a variable instead of creating different variables for each individual value. They are created using square brackets. The stored values are placed in curly brackets and separated by commas. Here is the syntax for arrays:

dataType[] arrayName = {value1, value2, value3,…};
java

If you want to create an array with strings, you can do so like this:

String[] colors = {"blue", "red", "yellow", "purple"};
java

Here’s how to create an array with integers:

int[] figures = {5, 10, 15, 20};
java

Classes

In Java, classes are data types that serve as a template for creating objects. They contain various components, including the name of the class, modifiers and a body enclosed in curly brackets. Here’s an example of what a class looks like in Java:

public class Main {
	int a = 10;
}
java

Interfaces

In Java, an interface is an abstract class. It acts as an interface, which a class can access various functions through. To do this, however, it must first implement them. Interfaces only contain constants and abstract methods. Their syntax looks like this:

interface {
	abstract methods
}
java

We’ll do a simple example here to show how interfaces work:

interface Pizza {
	public void ingredientsList();
	public void preparation();
}
class Mushroom implements Pizza {
	public void ingredientsList() {
		System.out.println("mushrooms, tomato sauce, oregano, mozzarella");
}
public void preparation() {
	System.out.println("The pizza will be ready in 15 minutes.");
}
}
class Main {
	public static void main(String[] args) {
		Mushroom myMushroom = new Mushroom();
		myMushroom.ingredientsList();
		myMushroom.preparation();
	}
}
java

The corresponding output from the Java command System.out.println looks like this:

mushrooms, tomato sauce, oregano, mozzarella
The pizza will be ready in 15 minutes.
java

Objects

In Java, objects are also a complex data type. Objects are instances of classes that can then interact with each other using methods. In the following example, we’re going to create several objects in a Main class:

public class Main {
	int a = 10;
public static void main(String[] args) {
	Main myObj1 = new Main();
	Main myObj2 = new Main();
	System.out.println(myObj1.a);
	System.out.println(myObj2.a);
	}
}
java

The output looks like this:

10
10
java

Enums

Enums are a special class that allow you to incorporate unchangeable constants into your code. These variables are assigned fixed values that cannot be changed. This data type is particularly useful if you need variables that should only have a few possible states. The syntax of an enum looks like this:

enum NameOfTheClass {
	VALUE1,
	VALUE2,
	VALUE3
}
java
Was this article helpful?
Page top