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 pro­gram­ming language, there are various data types that contain specific objects and defined op­er­a­tions. Although the data type options offered by different languages often resemble each other, sometimes, there are sig­ni­fic­ant dif­fer­ences. For example, if you compare Python and Java and their data types, you‘ll notice sim­il­ar­it­ies between the two, but also numerous dif­fer­ences 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 struc­tures and organise and ma­nip­u­late 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. Un­der­stand­ing how to best use data types is not only a question of un­der­stand­ing their func­tion­al­ity but also how much memory they use (or conserve).

Web hosting
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB 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 cat­egor­ies: logical data types, integral data types, floating-point data types and character data types. They have no ad­di­tion­al special cap­ab­il­it­ies and are also referred to as ele­ment­ary 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 rep­res­en­ted as a two’s com­ple­ment 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 com­ple­ment 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 com­ple­ment 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 com­ple­ment value is therefore used if the other integral data types are not suf­fi­cient. 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 rep­res­ent­a­tion 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 rep­res­en­ted 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 ab­so­lutely exact values, you should use the Big­Decim­al class instead. The syntax of double is as follows:

double doubleVar;
java

char

char is a character data type. It stores char­ac­ters based on the Unicode character set, enabling port­ab­il­ity to numerous different platforms. Each character requires 2 bytes of memory. Its value range cor­res­ponds to ASCII (American Standard Code for In­form­a­tion In­ter­change) and is located between ‘\u0000’ (i.e., 0) and ‘\uffff’ (cor­res­ponds 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 pre­defined, but are de­term­ined by the pro­gram­mer (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 char­ac­ters, dis­tin­guish­ing 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 in­di­vidu­al char­ac­ters in the string, compare strings with each other, search strings and copy strings. Strings are iden­ti­fied 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 in­di­vidu­al 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 com­pon­ents, 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

In­ter­faces

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. In­ter­faces 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 in­ter­faces 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 cor­res­pond­ing 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 in­cor­por­ate un­change­able constants into your code. These variables are assigned fixed values that cannot be changed. This data type is par­tic­u­larly 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
Go to Main Menu