Return Type Of Method In Java - Tutorials For Selenium WebDriver

We have learnt METHOD IN JAVA software development language and different METHOD ACCESS MODIFIERS in my previous posts. Now let me introduce you with different return types of methods in java software development language. You can use two kind of return types with methods. 1. Void and 2. Any data types. Void means returning nothing and when your method is returning some values like Integer, String, double, etc.., You need to provide return type with method according to returning value's data types in java software development language.
  • int, String, double etc..Data types - If method is returning any value then you need to provide return type with method like int, String, Double etc..
  • void - If method is not returning any value then you can give void as return type. void means method returning nothing.
Let me give you simple example of int, double and void return type.
package Test_Package1;

public class Return_Types {

 static int c;
 static double d; 
 public static void main(String[] args) {
  Mul(2,3);
  Div(7,3);
  System.out.println("Value of c Is "+c);
  System.out.println("Value of d Is "+d);
  Message();
 }
 //This method is returning integer value. It's return type is int.
 public static int Mul(int a, int b){
  c=a*b;
  return c;  
 } 
 //This method is returning double value. It's return type is double.
 public static double Div(double a, double b){
  d=a/b;
  return d;  
 }
 //This method is returning nothing so there is used void return type.
 public static void Message(){
  System.out.println("Test Message");
 }
}
If you see in above example, I have used three methods (Mul, Div and Message) and called all three methods in main method to execute them. 
  • Method Mul is returning multiplied integer value in variable c so I have used int data type as return type with Mul method. 
  • Method Div is returning division of two values in variable d so I have used double data type as return type with Div method.
  • Method Message is returning nothing so I need to use void return type with It in java  software development language.

10 comments:

  1. Please change colour scheme of of all comments used in examples, as it is green currently and too hard to read.
    i.e "//This method is returning nothing so there is used void return type."

    ReplyDelete
  2. People is asking too much. Just take whatever he give...Thanks for your hard work. You should have a button name "Donate".

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. plz send me browser version rel;ated selenium webdriver version

    ReplyDelete
  5. hi...is void compulsory for methods that do not return any value?

    ReplyDelete
  6. Can we have the same above example without using a return type.

    ReplyDelete
  7. What is the difference or pros and cons between using return and using System.out.println() to display the values of c and d? I tried to replace Mul method with the following and it displayed the same results as using a return method:
    public static void Mul(int a, int b){
    c=a*b;
    return c;
    System.out.println(c);

    ReplyDelete