Java

Do I Return in a Void Function in Java?

In Java, a void function (or method) does not return any value. However, you can use the return statement to exit the method early if needed. The return statement in a void method does not return any value, it simply exits the method.

Do I Return in a Void Function in Java? Examples

Here’s an example:

public class Ex1 {
    public static void main(String[] args) {
        // Define items and their weights
         printMessage(true);
        printMessage(false);
    }

    public static void printMessage(boolean condition) {
        if (condition) {
            System.out.println("Condition is true!");
            return; // Exits the method early if condition is true
        }
        System.out.println("Condition is false!");
    }
}

Output

Condition is true!
Condition is false!

 

 

admin

Share
Published by
admin

Recent Posts

Is Java Hard to Learn?

Is Java Hard to Learn is it True? Java is one of the most popular…

1 hour ago

How to generate random with weight java

Generating Random Numbers with Weights in Java Random number generation with weighted probabilities is a…

2 hours ago

Building JSP AJAX CRUD Application

Introduction to JSP AJAX CRUD Applications Building web applications has become more dynamic with the…

2 days ago

Hotel Management System using Laravel 11

Relationships: Hotel ↔ Rooms (One-to-Many) A hotel can have many rooms, but a room belongs…

3 weeks ago

Creating Grocery Inventory App Using React

Introduction to Grocery Inventory Apps Managing grocery inventory can be a daunting task, but with…

1 month ago

Fish Inventory Shop Management System in Angular

This article explain how to make a Fish Inventory Management App in Angular.this app explain…

1 month ago