{"id":2671,"date":"2017-06-24T05:40:35","date_gmt":"2017-06-24T05:40:35","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2671"},"modified":"2021-03-21T06:34:03","modified_gmt":"2021-03-21T06:34:03","slug":"example-java-method-which-return-values-to-the-caller","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/example-java-method-which-return-values-to-the-caller\/","title":{"rendered":"Example Java method that return values"},"content":{"rendered":"<p>In this tutorial, we will learn how to write Java methods that return values to the caller method i.e main method.\u00a0 Below is an example of java code with methods that return double datatypes.<\/p>\n<h3>Example Java Method with void<\/h3>\n<p>public void readRadius() {<br \/>\nScanner sc = new Scanner(System.in);<br \/>\nSystem.out.print(&#8220;Enter radius =:&#8221;);<br \/>\nradius=sc.nextFloat();<br \/>\n}<\/p>\n<h3>\u00a0Java method with return type<\/h3>\n<p>public double getPerimeter() {<br \/>\nperimeter = 2*Math.PI*radius;<br \/>\nreturn perimeter;<br \/>\n}<\/p>\n<h3>Java Program<\/h3>\n<p>The complete java program with the Java methods.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.*; \r\n\r\npublic class JavaMethodsDemo {\r\n  private double radius=00.0;\r\n  private double area=0.0; \r\n  private double perimeter=0.0;\r\n\r\n\r\n  \/***********************************\r\n   * This is an example of java method that doesn't\r\n   * return anything i.e void\r\n   ***********************************\/\r\n  public void readRadius() {\r\n    Scanner sc = new Scanner(System.in); \r\n    System.out.print(\"Enter radius =:\"); \r\n    radius=sc.nextFloat();\r\n  } \r\n\r\n\r\n  \/***********************************\r\n   * Java method that returns area\r\n   * to the caller of the data type double.\r\n   ***********************************\/\r\n  public double getArea() { \r\n    area= Math.PI*radius*radius; \r\n    return area;\r\n  } \r\n\r\n  \/***********************************\r\n   * Java method that returns perimeter\r\n   * to the caller of the data type double.\r\n   ***********************************\/\r\n  public double getPerimeter() {\r\n    perimeter = 2*Math.PI*radius; \r\n    return perimeter; \r\n  }\r\n\r\n\r\n  public static void main(String []args) { \r\n    JavaMethodsDemo area=new JavaMethodsDemo(); \r\n    area.readRadius(); \r\n    System.out.println(\"Area of circle :\" +area.getArea()); \r\n    System.out.println(\"Perimeter of circle :\" + area.getPerimeter());\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>Program Output<\/h3>\n<p>Enter radius =:5.0<br \/>\nArea of circle :78.53981633974483<br \/>\nPerimeter of circle :31.41592653589793<\/p>\n<h3>Screenshot<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2680\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-methods-with-return-types.png\" alt=\"Java methods with return values\" width=\"1710\" height=\"978\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-methods-with-return-types.png 1710w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-methods-with-return-types-300x172.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-methods-with-return-types-1024x586.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-methods-with-return-types-768x439.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-methods-with-return-types-1536x878.png 1536w\" sizes=\"auto, (max-width: 1710px) 100vw, 1710px\" \/><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-2671\" data-postid=\"2671\" class=\"themify_builder_content themify_builder_content-2671 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn how to write Java methods that return values to the caller method i.e main method.\u00a0 Below is an example of java code with methods that return double datatypes. Example Java Method with void public void readRadius() { Scanner sc = new Scanner(System.in); System.out.print(&#8220;Enter radius =:&#8221;); radius=sc.nextFloat(); } \u00a0Java 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-2671","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\/2671","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=2671"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2671\/revisions"}],"predecessor-version":[{"id":19735,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2671\/revisions\/19735"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}