{"id":3797,"date":"2017-07-30T16:26:34","date_gmt":"2017-07-30T16:26:34","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=3797"},"modified":"2024-09-04T07:22:33","modified_gmt":"2024-09-04T07:22:33","slug":"how-to-pass-an-array-to-a-method-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-pass-an-array-to-a-method-in-java\/","title":{"rendered":"How to pass an array to a method in Java"},"content":{"rendered":"<h2>How to pass an array to a method in Java<\/h2>\n<p>In Java, methods are used to break the Java program into small modules. These methods are used to call from other methods. The caller method can pass data to the methods as parameters. The data that is being passed from the caller or invoking method to the method. The method can return data to the caller method using the return value.<\/p>\n<h3>Passing Array to Method in Java<\/h3>\n<p>An array can be passed to a method similar to how we pass primitive data type arguments. The primitive data type arguments are those like <strong>int<\/strong> or <strong>double<\/strong> which are passed into methods by values.<\/p>\n<p>The method prototype must match with the argument of the array.<\/p>\n<p><strong>public &lt;return_type&gt; methodName(int[] passedArray);<\/strong><\/p>\n<p>Example:<\/p>\n<p><strong>public static void printArray(int[] printArray) {<\/strong><\/p>\n<p><strong>&#8230;\/\/ method body<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p>The main method invokes the method using the statement:<\/p>\n<p><strong>printArray(marks);<\/strong><\/p>\n<p>To pass an array as an argument to a method is to pass the name of the array without the square brackets.<\/p>\n<h3>Code Listing<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.testingdocs.tutorial;\r\n\/\/ Pass an array to a method demo.\r\n\/\/ www.TestingDocs.com\r\npublic class PassAnArraytoMethodDemo {\r\n\r\n  public static void main(String[] args) {\r\n    \/\/declaring an array \r\n    int marks[]= {65,70,58,90};\r\n    System.out.println(\"Original Array:=\");\r\n    printArray(marks);\r\n    \/\/Every element should be incremented by 5\r\n    System.out.println(\"Modified Array:=\");\r\n    int modifiedArray[] = methodToModifyArray(marks);\r\n    printArray(modifiedArray);\r\n  }\r\n\r\n  \/\/method to modify the array.\r\n  public static int[] methodToModifyArray(int[] marksArray) {\r\n    for(int i=0; i&lt;marksArray.length; i++)\r\n    {\r\n      \/\/just to modify the array inside the method.\r\n      marksArray[i]=marksArray[i] + 5;\r\n    }\r\n    return marksArray;\r\n  }\r\n\r\n  \/\/method to print array.\r\n  public static void printArray(int[] printArray) {\r\n    for(int i=0; i&lt;printArray.length; i++)\r\n    {\r\n      \/\/print the modified array\r\n      System.out.println(printArray[i]);\r\n    }\r\n  }\r\n\r\n}\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3811\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Pass-an-array-to-Method-Java.png\" alt=\"Pass an array to Method Java\" width=\"1730\" height=\"1008\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Pass-an-array-to-Method-Java.png 1730w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Pass-an-array-to-Method-Java-300x175.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Pass-an-array-to-Method-Java-1024x597.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Pass-an-array-to-Method-Java-768x447.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Pass-an-array-to-Method-Java-1536x895.png 1536w\" sizes=\"auto, (max-width: 1730px) 100vw, 1730px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3>Program output<\/h3>\n<p>Original Array:=<br \/>\n65<br \/>\n70<br \/>\n58<br \/>\n90<br \/>\nModified Array:=<br \/>\n70<br \/>\n75<br \/>\n63<br \/>\n95<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to pass an array to a method in Java In Java, methods are used to break the Java program into small modules. These methods are used to call from other methods. The caller method can pass data to the methods as parameters. The data that is being passed from the caller or invoking method [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-3797","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=3797"}],"version-history":[{"count":5,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3797\/revisions"}],"predecessor-version":[{"id":24072,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3797\/revisions\/24072"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=3797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=3797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=3797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}