Back Java Void Methods VS. Value Returning Methods - Learn Java Programming - Appficial

3-5
Created By: Appficial
Published At: 2017-10-16
Search Query: Java Class Methods,Java Method Parameters,Java Methods
Tags: appficial java java void method void method void void vs return method return methods java methods methods java functions functions void vs non void non void methods return values method return return statement Math.pow hello world java programming java tutorial java examples java example java class learn java java program how to program java code programming coding java api jre jdk jvm java 8
More videos coming soon, please SUBSCRIBE!


A void method simply performs a task and terminates
For example: System.out.println(“Hello world”);

A value-returning method performs a task and then also returns (or sends) a value back to the code where it was called
For Example: int answer = Math.pow(2,3);

Please SUBSCRIBE to Appficial, and check out more Java Programming Tutorials! http://www.appficial.com

Java Methods Intro
https://youtu.be/m2CZ8ZcW7Rg

Java Method Modifiers
https://youtu.be/h_UBnO7qJgc

Java Void Methods VS. Value Returning Methods
https://youtu.be/gGIr9kvg6V4
logo Ask VidTutorAssistant
Comment
or
Question
Your name
Comments with generated responses
@CooCoo212
2018-11-05

3>thank you so much

@Appficial

You're welcome!

VidTutorAssistant (GPT-4)

You're welcome @CooCoo212! We're happy you found the video helpful. Our goal is to make complex topics like Java methods easier to understand. Feel free to share any other queries or topics you'd like us to cover. Stay tuned for more!


@farhadhabibullah548
2021-10-20

Just did my 6 months boot camp, and didn't really know about the difference between void and return method, you just made everything crystal clear in 3min! you are a freakin legend!

@Appficial

Thanks Farhad!

VidTutorAssistant (GPT-4)

Thank you so much @farhadhabibullah548! Your comment means a lot to me! I'm genuinely glad that I could make those distinctions clear for you. Don't worry, we all were beginners once and it's only through continuous learning that we grow. Keep up the great work and let me know if there's any other topic you'd like me to cover! Happy coding!


@josefranciscojimenez7016
2020-07-01

oh my god I finally get i can't believe it i love you!!!!

@Appficial

Thanks Jose đź‘Ť

VidTutorAssistant (GPT-4)

Hey @josefranciscojimenez7016, that's what we love to hear! I'm so glad that the distinction between void and value-returning methods in Java now makes sense to you. Remember, keep practicing and it'll soon become second nature. Don't hesitate to revisit the video if you ever need a brush up. Keep coding!


@ajaymore373
2020-06-07

If both method execute void and return type then it may show error.Void type not allowed can you ans for this type of error?

@Appficial

You cannot have a return statement and a void return type. Replace void with the data type of what is being returned

VidTutorAssistant (GPT-4)

Hi @ajaymore373, great question! You're correct, trying to have a method both execute as a void and have a return type would indeed create an error. You see, a method in Java can only have one return type, or none at all (void). When a method's set to "void", it means that it is not expected to return any value after it's done executing its task. On the other hand, methods defined with a return type (like int, double, etc.) must return a value of that specified type at the end of their execution. If you try to mix these two by defining a void method and asking it to return a value, Java will show an error like the one you described: 'Void type not allowed'. It's important to understand that void and return types have different purposes in your code and you must choose between one or the other depending on your specific needs. I hope this clears up your question, keep coding!


@ajaybrandy2045
2020-03-30

Is one better than the other? When and why would we use one or the other?

@Appficial

Let's say you had a method like add() that adds 2 numbers. Use a void method if you just wanted to add and maybe print the sum. Use a value return method if you want the sum returned back to you. In this situation, a value return method makes more sense and more useful

VidTutorAssistant (GPT-4)

Great question @ajaybrandy2045! Neither void methods nor value-returning methods are inherently better than the other. The decision between using one or the other comes down to the needs of your specific scenario.

Void methods are handy when we need to perform a routine task or operation and there is no need to return any value; for instance, printing a message on the screen or logging some data.

On the other hand, value-returning methods are used when we perform calculations or processing and we need the result back for further steps. As I demonstrated in the video, the add method returns the sum of two numbers, which can be used in further operations or printed on the screen.

Void methods are all about carrying out tasks, while value-returning methods are about computing and passing back results so that we can utilize these results in different parts of our program. You choose based on what you want to achieve in your code!

I hope this answers your question. Keep programming!


@cartman444444
2019-11-14

1:31 how'd you made the comment out shortcut by selecting the text then // it?

@Appficial

highlight everything you want to comment and then click ctrl-/ (forward slash), or command-/ on a mac

VidTutorAssistant (GPT-4)

Hi @cartman444444, great question! In many IDEs, including popular ones for Java programming like IntelliJ IDEA and Eclipse, there's a handy shortcut for commenting or uncommenting lines of code. In IntelliJ, you just press Ctrl + / (slash) for line comments i.e. //, and Ctrl + Shift + / for block comments i.e. /* */. On a Mac, you would replace Ctrl with Cmd. Don't forget, you can easily select multiple lines to comment out at once too by clicking and dragging your cursor over the text. Hope this helps, happy coding!


@pkasdovi
2019-02-02

i have been searching for this like for months still without getting it..but with this video every thing is clear now..thanks man.

@Appficial

No problem, glad to help!

VidTutorAssistant (GPT-4)

I'm thrilled to hear that this video helped you understand the difference between void methods and value-returning methods, @pkasdovi! It's exactly why I made it. Thanks for taking the time to watch and for your kind words. Always remember, in programming, perseverance is key. Stick around for more videos like this!


@pradipacharya3168
2022-11-26

Just a quick question. What type of method would I need to create if I wanted to do a task and return a given value back to the main method? Rather than passing values from the main to another class and returning something different back. I’m new to programming so sorry if what I said doesn’t make sense. I just don’t understand when to use “public static, static void, public static void” or how each of them are different from each other

@Appficial

You can call a method from anywhere, so if you need to return the value in main then just call the method in your main.
public means you can access the method from anywhere. Static means you do not need to create an object first and can call the method directly. Void means it doesn't return a value. If you need to return an integer, then change void to int, such as

public static int methName() { }

VidTutorAssistant (GPT-4)

Hey @pradipacharya3168, no need to apologize, we all start somewhere and your question makes perfect sense. If you want a method to complete a task and then return a given value back to the main method, you'd want to use a "value returning method". In Java, this is typically defined with keywords like "public static" followed by a datatype, such as "int" or "double", as per your requirements.

To elaborate a bit:

- If you use "public static void", you're defining a method that does not return a value. For example ("public static void add"), where 'add' is a task that performs a sum operation, and then the task ends.

- If you use 'public static' followed by a datatype (like "int"), this means your method performs a task and then returns a value of that datatype. For instance, "public static int add" calculates a sum but then also returns that sum to wherever it was called from.

In every case, 'public static' is just a way to define a method that can be accessed and used without the need of creating an object of your class.

I hope this makes it clear! Don't hesitate to ask any more questions you might have. Happy coding!


@hakmat1769
2022-10-30

what would happen if u just changed the void into int without changing anithing else like making a new function with return value?

@Appficial

You would get an error unless you had a return statement at the end to return an integer value

VidTutorAssistant (GPT-4)

Hi @hakmat1769, great question! If you change 'void' into 'int' but don't include a return statement in your function, it would result in a compile-time error. The compiler expects an int value to be returned due to 'int' being specified. So, when it doesn't find a return statement, it flags this as an error. That's why when we change 'void' to 'int', we must include a return statement that returns an integer value. I hope this clears up your doubt. Keep coding and exploring!


@V.F.Y.
2020-01-02

Technical subjects need to be explained with minimum " wording ". Clean and simple so that the brain can sponge the essence of the information without rubbish. You have executed this principle beautifully. It is also good to see two similar things side-by-side. I learn much faster this way. Thank you for this.

@Appficial

Thanks Victor, that was my goal. More videos coming soon!

VidTutorAssistant (GPT-4)

Thank you so much for your kind words, @V.F.Y.! I'm really glad you found the explanations simple and clean. It's my goal to make technical concepts accessible and easy to understand, and I'm happy to hear that the side-by-side comparison helped. Stay tuned for more videos, and never hesitate to drop a comment if you have any questions or topics you'd like me to cover. Happy learning!