Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Void | Methods
Java Extended

Void

How to return nothing?

You already know how to use methods and how to create them. You also know that methods can return different types of values. These can be primitive data types or even classes that you have written. We will cover how to write your own classes and use them later in this course.

However, there is a special case when the return type is void. When the return type is void, it means that we are not returning anything from our method. It simply performs operations and does not return a value. Such a method can still have parameters. Let's take a look at an example of using a void method:

java

Main.java

We have written and used a method that does not return anything. This void method takes a parameter of type String and prints it to the screen. We call it in the main method, and it works fine.

A void method can also perform more complex operations without returning anything, such as printing an array. It will be much cleaner when we print an array using just one method in the main method. Let's take a look at an example code:

java

Main.java

As you can see, we saved lines of code by writing a separate method for printing the array. Instead of repeatedly writing a new for-each loop to print the array, we simply call this method and pass the array as a parameter.

In this way, we can say that methods greatly improve our code. They make it more readable and easier to edit. As you are already writing more complex programs and larger codebases, I recommend using methods more often to avoid confusion. Practicing with methods will make you a truly proficient programmer.

Note

In practice, void methods are not commonly used because they are difficult to test and interpret correctly. However, knowledge of void methods is still important because this type of method can come in handy in challenging situations.

Other return types

You can use any data type as a return value. In previous chapters, we have already returned an int type from a method. You can also return String, long, double, or any arrays. We can even return a user-defined type (class) created by us. In the upcoming chapters, you will learn how to do this and practice it.

Everything was clear?

Section 2. Chapter 4

Void

How to return nothing?

You already know how to use methods and how to create them. You also know that methods can return different types of values. These can be primitive data types or even classes that you have written. We will cover how to write your own classes and use them later in this course.

However, there is a special case when the return type is void. When the return type is void, it means that we are not returning anything from our method. It simply performs operations and does not return a value. Such a method can still have parameters. Let's take a look at an example of using a void method:

java

Main.java

We have written and used a method that does not return anything. This void method takes a parameter of type String and prints it to the screen. We call it in the main method, and it works fine.

A void method can also perform more complex operations without returning anything, such as printing an array. It will be much cleaner when we print an array using just one method in the main method. Let's take a look at an example code:

java

Main.java

As you can see, we saved lines of code by writing a separate method for printing the array. Instead of repeatedly writing a new for-each loop to print the array, we simply call this method and pass the array as a parameter.

In this way, we can say that methods greatly improve our code. They make it more readable and easier to edit. As you are already writing more complex programs and larger codebases, I recommend using methods more often to avoid confusion. Practicing with methods will make you a truly proficient programmer.

Note

In practice, void methods are not commonly used because they are difficult to test and interpret correctly. However, knowledge of void methods is still important because this type of method can come in handy in challenging situations.

Other return types

You can use any data type as a return value. In previous chapters, we have already returned an int type from a method. You can also return String, long, double, or any arrays. We can even return a user-defined type (class) created by us. In the upcoming chapters, you will learn how to do this and practice it.

Everything was clear?

Section 2. Chapter 4
some-alt