{"id":1182,"date":"2022-02-24T20:02:30","date_gmt":"2022-02-24T20:02:30","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1182"},"modified":"2022-02-24T20:02:30","modified_gmt":"2022-02-24T20:02:30","slug":"java-methods","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/java-methods\/","title":{"rendered":"Java Methods"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java methods, how to define methods, and how to use methods in Java programs with the help of examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"introduction\">Java Methods<\/h2>\n\n\n\n<p>A method is a block of code that performs a specific task.<\/p>\n\n\n\n<p>Suppose you need to create a program to create a circle and color it. You can create two methods to solve this problem:<\/p>\n\n\n\n<ul><li>a method to draw the circle<\/li><li>a method to color the circle<\/li><\/ul>\n\n\n\n<p>Dividing a complex problem into smaller chunks makes your program easy to understand and reusable.<\/p>\n\n\n\n<p>In Java, there are two types of methods:<\/p>\n\n\n\n<ul><li><strong>User-defined Methods<\/strong>: We can create our own method based on our requirements.<\/li><li><strong>Standard Library Methods<\/strong>: These are built-in methods in Java that are available to use.<\/li><\/ul>\n\n\n\n<p>Let&#8217;s first learn about user-defined methods.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"declare-method\">Declaring a Java Method<\/h2>\n\n\n\n<p>The syntax to declare a method is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>returnType methodName() {\n  \/\/ method body\n}<\/code><\/pre>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ul><li><strong>returnType<\/strong>\u00a0&#8211; It specifies what type of value a method returns For example if a method has an\u00a0<code>int<\/code>\u00a0return type then it returns an integer value.<br><br>If the method does not return a value, its return type is\u00a0<code>void<\/code>.<\/li><li><strong>methodName<\/strong>\u00a0&#8211; It is an\u00a0identifier\u00a0that is used to refer to the particular method in a program.<\/li><li><strong>method body<\/strong>\u00a0&#8211; It includes the programming statements that are used to perform some tasks. The method body is enclosed inside the curly braces\u00a0<code>{ }<\/code>.<\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int addNumbers() {\n\/\/ code\n}<\/code><\/pre>\n\n\n\n<p>In the above example, the name of the method is&nbsp;<code>adddNumbers()<\/code>. And, the return type is&nbsp;<code>int<\/code>. We will learn more about return types later in this tutorial.<\/p>\n\n\n\n<p>This is the simple syntax of declaring a method. However, the complete syntax of declaring a method is<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>modifier static returnType nameOfMethod (parameter1, parameter2, ...) {\n  \/\/ method body\n}<\/code><\/pre>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ul><li><strong>modifier<\/strong>\u00a0&#8211; It defines access types whether the method is public, private, and so on. To learn more, visit\u00a0Java Access Specifier.<\/li><li><strong>static<\/strong>\u00a0&#8211; If we use the\u00a0<code>static<\/code>\u00a0keyword, it can be accessed without creating objects.<br><br>For example, the\u00a0<code>sqrt()<\/code>\u00a0method of standard\u00a0Math class\u00a0is static. Hence, we can directly call\u00a0<code>Math.sqrt()<\/code>\u00a0without creating an instance of\u00a0<code>Math<\/code>\u00a0class.<\/li><li><strong>parameter1\/parameter2<\/strong>\u00a0&#8211; These are values passed to a method. We can pass any number of arguments to a method.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"call-method\">Calling a Method in Java<\/h2>\n\n\n\n<p>In the above example, we have declared a method named&nbsp;<code>addNumbers()<\/code>. Now, to use the method, we need to call it.<\/p>\n\n\n\n<p>Here&#8217;s is how we can call the&nbsp;<code>addNumbers()<\/code>&nbsp;method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ calls the method\naddNumbers();<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-method-call.png\" alt=\"Call a method in Java using the name the method followed by a parenthesis\" title=\"Working of Java Method Call\"\/><figcaption>Working of Java Method Call<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Example 1: Java Methods<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\n  \/\/ create a method\n  public int addNumbers(int a, int b) {\n    int sum = a + b;\n    \/\/ return value\n    return sum;\n  }\n\n  public static void main(String&#91;] args) {\n    \n    int num1 = 25;\n    int num2 = 15;\n\n    \/\/ create an object of Main\n    Main obj = new Main();\n    \/\/ calling method\n    int result = obj.addNumbers(num1, num2);\n    System.out.println(\"Sum is: \" + result);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Sum is: 40<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a method named&nbsp;<code>addNumbers()<\/code>. The method takes two parameters&nbsp;<var>a<\/var>&nbsp;and&nbsp;<var>b<\/var>. Notice the line,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int result = obj.addNumbers(num1, num2);<\/code><\/pre>\n\n\n\n<p>Here, we have called the method by passing two arguments&nbsp;<var>num1<\/var>&nbsp;and&nbsp;<var>num2<\/var>. Since the method is returning some value, we have stored the value in the&nbsp;<var>result<\/var>&nbsp;variable.<\/p>\n\n\n\n<p><strong>Note<\/strong>: The method is not static. Hence, we are calling the method using the object of the class.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"return-type\">Java Method Return Type<\/h2>\n\n\n\n<p>A Java method may or may not return a value to the function call. We use the&nbsp;<strong>return statement<\/strong>&nbsp;to return any value. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int addNumbers() {\n...\nreturn sum;\n}<\/code><\/pre>\n\n\n\n<p>Here, we are returning the variable&nbsp;<var>sum<\/var>. Since the return type of the function is&nbsp;<code>int<\/code>. The sum variable should be of&nbsp;<code>int<\/code>&nbsp;type. Otherwise, it will generate an error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Method Return Type<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\n\/\/ create a method\n  public static int square(int num) {\n\n    \/\/ return statement\n    return num * num;\n  }\n\n  public static void main(String&#91;] args) {\n    int result;\n\n    \/\/ call the method\n    \/\/ store returned value to result\n    result = square(10);\n\n    System.out.println(\"Squared value of 10 is: \" + result);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Squared value of 10 is: 100<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, we have created a method named&nbsp;<code>square()<\/code>. The method takes a number as its parameter and returns the square of the number.<\/p>\n\n\n\n<p>Here, we have mentioned the return type of the method as&nbsp;<code>int<\/code>. Hence, the method should always return an integer value.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-method-return-value.png\" alt=\"Java method returns a value to the method call\" title=\"Representation of the Java method returning a value\"\/><figcaption>Representation of the Java method returning a value<\/figcaption><\/figure>\n\n\n\n<p><strong>Note<\/strong>: If the method does not return any value, we use the void keyword as the return type of the method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void square(int a) {\n  int square = a * a;\n  System.out.println(\"Square is: \" + a);\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"method-parameters\">Method Parameters in Java<\/h2>\n\n\n\n<p>A method parameter is a value accepted by the method. As mentioned earlier, a method can also have any number of parameters. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ method with two parameters\nint addNumbers(int a, int b) {\n  \/\/ code\n}\n\n\/\/ method with no parameter\nint addNumbers(){\n  \/\/ code\n}<\/code><\/pre>\n\n\n\n<p>If a method is created with parameters, we need to pass the corresponding values while calling the method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ calling the method with two parameters\naddNumbers(25, 15);\n\n\/\/ calling the method with no parameters\naddNumbers()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Method Parameters<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\n  \/\/ method with no parameter\n  public void display1() {\n    System.out.println(\"Method without parameter\");\n  }\n\n  \/\/ method with single parameter\n  public void display2(int a) {\n    System.out.println(\"Method with a single parameter: \" + a);\n  }\n\n  public static void main(String&#91;] args) {\n    \n    \/\/ create an object of Main\n    Main obj = new Main();\n\n    \/\/ calling method with no parameter\n    obj.display1();\n    \n    \/\/ calling method with the single parameter\n    obj.display2(24);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Method without parameter\nMethod with a single parameter: 24<\/samp><\/code><\/pre>\n\n\n\n<p>Here, the parameter of the method is&nbsp;<code>int<\/code>. Hence, if we pass any other data type instead of&nbsp;<code>int<\/code>, the compiler will throw an error. It is because Java is a strongly typed language.<\/p>\n\n\n\n<p><strong>Note<\/strong>: The argument&nbsp;<var>24<\/var>&nbsp;passed to the&nbsp;<code>display2()<\/code>&nbsp;method during the method call is called the actual argument.<\/p>\n\n\n\n<p>The parameter&nbsp;<var>num<\/var>&nbsp;accepted by the method definition is known as a formal argument. We need to specify the type of formal arguments. And, the type of actual arguments and formal arguments should always match.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"standard-library\">Standard Library Methods<\/h2>\n\n\n\n<p>The standard library methods are built-in methods in Java that are readily available for use. These standard libraries come along with the Java Class Library (JCL) in a Java archive (*.jar) file with JVM and JRE.<\/p>\n\n\n\n<p>For example,<\/p>\n\n\n\n<ul><li><code>print()<\/code>&nbsp;is a method of&nbsp;<code>java.io.PrintSteam<\/code>. The&nbsp;<code>print(\"...\")<\/code>&nbsp;method prints the string inside quotation marks.<\/li><li><code>sqrt()<\/code>&nbsp;is a method of&nbsp;<code>Math<\/code>&nbsp;class. It returns the square root of a number.<\/li><\/ul>\n\n\n\n<p>Here&#8217;s a working example:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4: Java Standard Library Method<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Main {\n  public static void main(String&#91;] args) {\n    \n    \/\/ using the sqrt() method\n    System.out.print(\"Square root of 4 is: \" + Math.sqrt(4));\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Square root of 4 is: 2.0<\/samp><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages\">What are the advantages of using methods?<\/h2>\n\n\n\n<p><strong>1.<\/strong>&nbsp;The main advantage is&nbsp;<strong>code reusability<\/strong>. We can write a method once, and use it multiple times. We do not have to rewrite the entire code each time. Think of it as, &#8220;write once, reuse multiple times&#8221;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 5: Java Method for Code Reusability<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Main {\n\n  \/\/ method defined\n  private static int getSquare(int x){\n    return x * x;\n  }\n\n  public static void main(String&#91;] args) {\n    for (int i = 1; i &lt;= 5; i++) {\n\n      \/\/ method call\n      int result = getSquare(i);\n      System.out.println(\"Square of \" + i + \" is: \" + result);\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Square of 1 is: 1\nSquare of 2 is: 4\nSquare of 3 is: 9\nSquare of 4 is: 16\nSquare of 5 is: 25<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, we have created the method named&nbsp;<code>getSquare()<\/code>&nbsp;to calculate the square of a number. Here, the method is used to calculate the square of numbers less than&nbsp;<strong>6<\/strong>.<\/p>\n\n\n\n<p>Hence, the same method is used again and again.<\/p>\n\n\n\n<p><strong>2.<\/strong>&nbsp;Methods make code more&nbsp;<strong>readable and easier<\/strong>&nbsp;to debug. Here, the&nbsp;<code>getSquare()<\/code>&nbsp;method keeps the code to compute the square in a block. Hence, makes it more readable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java methods, how to define methods, and how to use methods in Java programs with the help of examples. Java Methods A method is a block of code that performs a specific task. Suppose you need to create a program to create a circle and color it. You [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[484],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1182"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1182"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1182\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}