Блог пользователя _i.ayush_

Автор _i.ayush_, 3 года назад, По-английски

Void:

Void is return type of the function main. Now what does this return type means? Before that we first need to understand returned value or return value.

Return Value:

Every function in any programing language returns some value. A value that it has computed, which function was meant for. Let's take an example to explain about value retuned and function computation.

In the following code (see code below), we've a class sum that has a public method called sumof2() that computes the sum of two values of a variables 'a' and 'b' passed as an argument/parameters.

So when we say return value we meant the line "return C". As you can notice from main function we've passed values 2 and 3 that were stored in 'a' and 'b' in sumof2 method. Sum of two method computed a+b and stored it in C and returned value 5. This 5 is k/a value returned.

And all the lines before return line are k/a computation.

class sum{
  public int sumof2(int a, int b){
    int c = a+b; 
    return c;  
  }
} 

public class Main {
    public static void main(String[] args) {
        System.out.print("Sum of two var a and b is: ");
        sum obj new sum(); 
        int x=obj.sumof2(2,3);
        System.out.println(x);
    }

}

But, What is return type?

"Every value returned, in java, is categorized under some primitive class or wrapper class. The category of value is known as return type."

Now why is it important to know what type of value is returned?

Suppose your function sumof2 (see code below) computed two values a=50 and b=47 and stored there sum in c=97. Now how will compiler get to know if you are talking about 5, 5.0 or Ascci value of 97. What value among these do you want. To resolve this dilemma of compiler we mention return type before every function definition. See the following code for more understanding:

Here in the class sum, we've 3 functions with same parameters (a = 50 and b= 47 )but different return value. But when we call these different functions and print there returned value we get different values on our screen. We get 97, 97.0 and a.

But why so?

Because return type of all these functions is different and compiler treats the returned value of
the category ( class ) that you've mentioned in RETURN TYPE.

class sum{
    public int sum_int(int a, int b){
        return a+b;
    }

    public double sum_double(int a, int b){
        return a+b;
    }

    public char sum_char(int a, int b){
        return a+b;
    }
}

For your more hands on experiments, we've attached the source code link down bellow...... CLICK HERE TO ACCESS

So the return type of main function is void and void means to return no value. Since main function is not called by any other function from any other class that's why its return type has to be VOID only.

  • Проголосовать: нравится
  • -23
  • Проголосовать: не нравится