Site icon C1CTech

Java Methods

<p>In this article&comma; you’ll learn about Java methods&comma; how to define a Java method and use them in your program with the help of examples&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Methods<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>A Java <strong><span style&equals;"color&colon; &num;008000&semi;">method<&sol;span><&sol;strong> is a collection of statements that are grouped together to perform an operation&period;<&sol;p>&NewLine;<p>Methods allow us to reuse the code without retyping the code i&period;e&period; you can write a method once&comma; and use it multiple times&period; It also makes code more readable and easier to debug&period;<&sol;p>&NewLine;<p>As Java is an <span style&equals;"color&colon; &num;008000&semi;"><strong>Object Oriented Programming<&sol;strong><&sol;span> language&comma; every method must be a part of some class which defines the behavior of a class&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Types of Methods<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>In Java&comma; depending on whether a method is defined by the user or available in the standard library&comma; methods are of two types&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>Standard Library Methods<&sol;li>&NewLine;<li>User-defined Methods<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Standard Library Methods<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>The Standard library methods are built-in methods in Java that are ready to use&period;<&sol;p>&NewLine;<p>Examples of Standard library methods are&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>print&lpar;&rpar;<&sol;strong><&sol;span>&colon; method comes under<span style&equals;"color&colon; &num;008000&semi;"><strong> java&period;io&period;PrintSteam<&sol;strong><&sol;span> &comma; prints the string written within the quotation&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">sqrt&lpar;&rpar;<&sol;span><&sol;strong>&colon; method of <strong><span style&equals;"color&colon; &num;008000&semi;">Math<&sol;span><&sol;strong> class that returns the square root of a specific number&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">For example&colon;<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>public class Number &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;max&lpar;&rpar; of Math class will print maximum number out of 16&comma;8<&sol;strong><&sol;span>&NewLine; System&period;out&period;print&lpar;"Maximum number is&colon; " &plus; Math&period;max&lpar;16&comma;8&rpar;&rpar;&semi;&NewLine;&NewLine; &rcub;<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output&colon;<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>Maximum number is&colon; 16<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>User-defined Methods<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>You can also define your own methods inside a class and such methods are called user-defined methods&period;<&sol;p>&NewLine;<p>To work with user-defined methods&comma; firstly we have to define a method then call it or use it&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Defining a method<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>Before you can use a method &lpar;call a method&rpar;&comma; you need to define it&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Syntax&colon;<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>The syntax of defining a method&colon;<&sol;p>&NewLine;<pre>modifier returnType nameOfMethod&lpar;Parameter list&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Method body<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<p>More generally&comma; method declarations have the following components&comma; in order&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>modifier<&sol;strong><&sol;span>-&colon; defines access type of the method i&period;e&period; whether this method will be called or will be visible to any other class or package or not&period; In Java&comma; access modifiers are of four types&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">public<&sol;span><&sol;strong>&colon; accessible within the class&comma; within the package and outside the package&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">protected<&sol;span><&sol;strong>&colon; accessible within the class&comma; within the package in which this class is defined and also outside the package but through inheritance only&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">private<&sol;span><&sol;strong>&colon; accessible only within the class in which it is defined&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">default<&sol;span><span style&equals;"color&colon; &num;008000&semi;"> &lpar;defined without using any modifier&rpar;<&sol;span><&sol;strong>&colon; accessible within the class and within the package only&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>returnType<&sol;strong><&sol;span>&colon; defines the data type of the value returned by the method or <span style&equals;"color&colon; &num;008000&semi;"><strong>void<&sol;strong><&sol;span> if a method does not return a value&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>nameOfMethod<&sol;strong><&sol;span>&colon; You can give any name to a method&period; But it&&num;8217&semi;s more conventional to name it upon the task it performs&period; As per the naming convention&comma; a method should start with lower case and if it contains more than one word then every word starting should be of upper case&period;For example&colon; calculateAddition&comma; displayStudentDetails etc&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Parameter list<&sol;span><&sol;strong>&colon; Parameters are the values passed to a method&period; You can pass any no of arguments to a method&period; If there are no parameters&comma; you must use empty parentheses &lpar;&rpar;&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Method body<&sol;span><&sol;strong> &colon; The code inside curly braces &lbrace; &rcub; is the body of the method&period; It defines what the method actually does&comma; how the parameters are manipulated with statements and what values are returned&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>public int max&lpar;int x&comma; int y&rpar; &lbrace;&NewLine; if &lpar;x &gt&semi; y&rpar; &NewLine; return x&semi;&NewLine; else &NewLine; return y&semi; &NewLine;&rcub;<&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Calling a method<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>Now we have defined a method&comma; we need to use it&period; For that&comma; we have to call the method to use its functionality&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Syntax&colon;<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>Syntax of calling  a method&colon;<&sol;p>&NewLine;<pre>nameOfMethod&lpar;Parameter list&rpar;&semi;<&sol;pre>&NewLine;<p>When calling a method it consists of the method name and a parameter list &lpar;number of parameters&comma; type of the parameters and order of the parameters&rpar;&period;<&sol;p>&NewLine;<p>The way of calling the<span style&equals;"color&colon; &num;008000&semi;"><strong> max&lpar;&rpar;<&sol;strong><&sol;span> method which we have defined above&colon;<&sol;p>&NewLine;<pre>max&lpar;5&comma; 10&rpar;&semi;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>public class Number &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;calling max&lpar;&rpar; method<&sol;span><&sol;strong>&NewLine; int result &equals; max&lpar;8&comma; 7&rpar;&semi;&NewLine; System&period;out&period;print&lpar;"Greater no&colon; " &plus; result&rpar;&NewLine; &rcub;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;defining max&lpar;&rpar; method<&sol;span><&sol;strong>&NewLine; public static int max&lpar;int x&comma; int y&rpar; &lbrace;&NewLine;&NewLine; if &lpar;x &gt&semi; y&rpar; &NewLine; return x&semi;&NewLine; else &NewLine; return y&semi; &NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>Greater no&colon; 8<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Example&colon; Java Methods<&sol;strong><&sol;span><&sol;h3>&NewLine;<pre>class Addition &lbrace;&NewLine;&NewLine; int result&semi;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;addTwoNumbers&lpar;&rpar; definition&NewLine; &sol;&sol;method with two parameters and&NewLine; &sol;&sol;returning integer value&period;therefore&comma;returnType is int<&sol;span><&sol;strong>&NewLine; public int addTwoNumbers&lpar;int a&comma; int b&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; adding two values&period;<&sol;span><&sol;strong>&NewLine; result &equals; a &plus; b&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;calling display&lpar;&rpar; method within the same class<&sol;span><&sol;strong>&NewLine; this&period;display&lpar;&rpar;&semi; &NewLine; &NewLine; return result&semi;&NewLine; &rcub;&NewLine; &NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;display&lpar;&rpar; definition &NewLine; &sol;&sol;method with no parameter and&NewLine; &sol;&sol;returning nothing&period;therefore&comma;returnType is void<&sol;strong><&sol;span>&NewLine; public void display&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"display&lpar;&rpar; in Addition class calling inside addTwoNumbers&lpar;&rpar;"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;public class ExampleDemo &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; creating an instance of Addition class<&sol;span><&sol;strong>&NewLine; Addition addition &equals; new Addition&lpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; calling addTwoNumbers&lpar;&rpar; method to add two values<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; using instance created in above step&period;<&sol;span><&sol;strong>&NewLine; int result &equals; addition&period;addTwoNumbers&lpar;10&comma; 5&rpar;&semi;&NewLine; System&period;out&period;println&lpar;"Addition of two Numbers &colon;" &plus; result&rpar;&semi;&NewLine; &rcub; &NewLine;&rcub;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>display&lpar;&rpar; in Addition class calling inside addTwoNumbers&lpar;&rpar;&NewLine;Addition of two Numbers &colon;15<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Control flow of above program&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1787" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;04&sol;method&lowbar;img&period;png" alt&equals;"method&lowbar;img" width&equals;"865" height&equals;"617" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version