{"id":2643,"date":"2016-06-23T04:12:29","date_gmt":"2016-06-23T04:12:29","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2643"},"modified":"2021-03-21T10:00:31","modified_gmt":"2021-03-21T10:00:31","slug":"how-to-pass-string-variable-to-a-java-method","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-pass-string-variable-to-a-java-method\/","title":{"rendered":"How to pass String variable to a Java method"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>In this tutorial, we will learn how to pass a String variable to a java method. We will define a method that takes a String parameter and we will invoke the method from the main method.<\/p>\n<h3><strong>Method signature<\/strong><\/h3>\n<p>public static void myMethod(String fname) ;<\/p>\n<p>The method takes the String parameter and then appends additional text to the string and then outputs the value to the console. The method is invoked from the method by passing some sample strings with male names.<\/p>\n<h3>Java Program<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/*********************************************************** \r\n * * MethodStringsDemo.java \r\n * * @program : Java program using method for strings \r\n * * @web : www.testingdocs.com \r\n * * @version : 1.0 \r\n * ************************************************************\/ \r\n\r\npublic class MethodStringsDemo { \r\n  public static void myMethod(String fname) {\r\n    System.out.println(\"The value passed to the method is =: \" + fname); \r\n  } \r\n\r\n  public static void main(String args[]) {\r\n    myMethod(\"John\"); \r\n    myMethod(\"Williams\");\r\n    myMethod(\"David\"); \r\n  } \r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Program Output<\/h3>\n<p>The value passed to the method is =: John<br \/>\nThe value passed to the method is =: Williams<br \/>\nThe value passed to the method is =: David<\/p>\n<h3>Screenshot<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2651\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Passing-String-to-Java-method.png\" alt=\"Passing String to Java Method\" width=\"1728\" height=\"1031\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Passing-String-to-Java-method.png 1728w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Passing-String-to-Java-method-300x179.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Passing-String-to-Java-method-1024x611.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Passing-String-to-Java-method-768x458.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Passing-String-to-Java-method-1536x916.png 1536w\" sizes=\"auto, (max-width: 1728px) 100vw, 1728px\" \/><\/p>\n<p>&nbsp;<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-2643\" data-postid=\"2643\" class=\"themify_builder_content themify_builder_content-2643 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this tutorial, we will learn how to pass a String variable to a java method. We will define a method that takes a String parameter and we will invoke the method from the main method. Method signature public static void myMethod(String fname) ; The method takes the String parameter and then appends additional [&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-2643","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\/2643","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=2643"}],"version-history":[{"count":4,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2643\/revisions"}],"predecessor-version":[{"id":19849,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2643\/revisions\/19849"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}