Skip to toolbar

Basic Java – 4 || More on methods(Return Type and Parameters)

          There are two main key functions for methods. Parameters and Return Type. If we talk about Parameters, these are the inputs for the methods. On the basis of these inputs methods performs any action. Once all the steps get executed mentioned within the block of a method, the output of that method will get returned to the place from where the method has been called in the code. This output can be sent to its native place using return type for that method.      

Methods

                       Return_type method_Name(param1, param2,)

                              int                  sum       ( int a   ,  int b)        

          In the above diagram, there is a method int sum(int a,int b) with name sum, which takes two input parameters of integer type(int a,int b). Also, after execution, it will return an integer value int sum to the Test.Class using return keyword. This returned value can be stored in a local variable int total, which can be used further in the program.

Related Links:

OOPs Concept:

Java Question And Answer:

Key Factors while declaring Methods

    • Return_type: data type of the value which will be returned from a method after execution completed.
    • Method_name: User-defined any logical name to identify method functionality. As per standard, it should start with lowercase, rest words should have the first letter in upper case.
    • Param1, param2: parameters have been divided into two parts data type and variable name. We can define multiple parameters using comma separator. (More on data types and Variables)
    • return: return is an inbuilt keyword which can return only one value to its original place.
    • A method may have zero or more parameters: If a method has no parameters the output of the method will always remain same.
    • A method can have only one return type: A method can only return only one value using return keyword.
    • A method can have void return type: If a method is declared void return type than output cannot be returned to its original place.
    • Multiple methods with the same name: A class can have multiple methods with same name and same parameters. This is called Polymorphism concept in OOPs. The process is called Method Overloading and Method Overriding.

Example Code-Methods:

public class SampleExample 
{
	public static int sum(int a, int b)
	{
		int c=a+b;
		return(c);
	}
	
	public static void main(String[] args) 
	{
		int total;
		total=sum(10,20);
		
		System.out.println("Total = "+total);
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Site Statistics

  • Users online: 0 
  • Visitors today : 0
  • Page views today : 0
  • Total visitors : 49,387
  • Total page view: 67,984

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.