Methods in Java.

In this article,Methods in Java is explained in detail with examples.
A Java technique is a gathering of Statements that are assembled together to implement an operation. When you call the System.out.println() technique, for example, the system really executes a few testimony with a specific end goal to show a message on the console.
Presently you will figure out how to make your own strategies with or without return values, request a technique with or without parameters, and apply strategy in the program plan.In this article, we are going to learn about the methods in java with examples. (From topic Methods in Java)
In Java, methods are nothing but functions in C and C++. A methods in Java is a sub-program which is used to perform a specific task in your main program.
Methods are of two types.

Predefined.
These Methods are library methods,which come with java software as API.These methods are available in different classes/different packages. (From topic Methods in Java)
Ex
Println(),print(),read(),readline(),next(),parseint().

User-defined.
These methods are defined by the user/programmer according to his requirements in a class.
Syntax.
(return-type) method-name (parameters,...)
{-----
-------
-------
body;
-------
------
return value;
}

Method-name: It represents work name, it is an identifier in a program.
Parameters: It represents input values/data given to your method.
Body: It represents task/work is supposed to be done in a method.
Return Value: It represents what is the reply/response given back after completing the work.
Return type: It represents data type of return value, which is used as a response. (From topic Methods in Java)
Note
1.If a method does not return any value,we designate it as void.
Ex
void sum(----)
{----
------
}
Void means nothing i.e "empty dataset"/no type of data".
*2.If a method in Java does not take any input parameters then leave it empty i.e no void.
Ex
void display() //empty space between bracket says null/void
{------
-------
}                (From topic Methods in Java)

Advantages of Methods.
  • Write once,call any number of times.
  • Re-usability.
  • The size of the program is reduced.
  • It makes our task simple and easy.
  • It saves our time and energy.
Programs using methods.             (From topic Methods in Java)
Ex:1
//Programs using Methods in Java.
class example
{
int sum(int a,int b,int c) //formal parameters.
{
int total;
total=a+b+c;
return total;
}
public static void main(String args[])
{
int t;
example obj=new example();
t=obj.sum(10,20,30); //actual arguments
System.out.println("sum is"+t);
t=obj.sum(100,200,300);
System.out.println("sum is"+t);
System.out.println("sum is"+obj.sum(1000,2000,3000));
}
}
Output
sum is60
sum is600
sum is6000

Method example-javaform
                            To get the code file use me.

 Continue to next topic Categories of Java and Program using two classes.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabad, surprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprises,surprise planners Hyderabad.

Hi, Friends. Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required