首页 > Java > java教程 > 正文

Java程序示例:一个没有参数和返回类型的方法

WBOY
发布: 2023-09-17 11:13:02
转载
1304人浏览过

java程序示例:一个没有参数和返回类型的方法

首先,让我们熟悉一下语法、示例,然后最后再进行实现。

Java中的方法非常重要,因为它允许重复使用相同的代码,减少代码中需要编写的语句数量。

有三个主要部分的方法,以使其可执行。

方法调用是最后一步,而其他两个步骤可以互换。唯一需要记住的是,在调用方法之前必须先声明它。

序列猴子开放平台
序列猴子开放平台

具有长序列、多模态、单模型、大数据等特点的超大规模语言模型

序列猴子开放平台 0
查看详情 序列猴子开放平台

Syntax

To create a method without any parameter and return type, the following syntax is considered.

Class class_name{
   function _name() {
      Statement 1;
      Statement 2;
      .
      .
      Statement n;
      //an optional return 
      return;
   }
   Main function() {
      // invoking the above function
      function_name();
   }
}
登录后复制

在一个类中创建一个空参数列表的方法。方法内部写入语句,可能会在最后加上一个空的返回语句。然后在主方法中调用这个方法。

Example

下面的程序是为了展示如何创建一个既没有参数也没有返回类型的方法。

A class named Wish is created within which, a method named wish() with return type void is created denoting it does not return any value, also it does not consist of any parameter. A statement is written within the wish() method and is displayed by invoking this method in the main method.

// Java Program to demonstrate a method without Parameters and Return Type
public class Wish {
	// Declaration and Definition of the method
	public static void wish(){
		System.out.println("Good Morning! Have a nice day");
	}
	public static void main(String args[]){
		// Calling the method without any parameters
		wish ();
	}
}
登录后复制

输出

Good Morning! Have a nice day
登录后复制

Example

下面的程序是为了展示如何创建一个既没有参数也没有返回类型的方法。

A class named Wish is created within which, a method named wish() with return type void is created denoting it does not return any value, also it does not consist of any parameter. The statements written inside the wish() method are displayed by invoking the method in the main method.

// Java Program to demonstrate a method without Parameters and Return Type
public class Wish {
	// Declaration and Definition of the method
	public static void wish(){
		System.out.println("Congratulations! Have a great professional life");
			//It is optional to use a return statement here.
			return;
	}
	public static void main(String args[]){
		// Calling the method without any parameters
		wish();
	}
}
登录后复制

输出

Congratulations! Have a great professional life
登录后复制

结论

本文阐述了如何在Java中定义一个没有参数和返回值的方法。我们从语法开始,进一步看到了一个示例和两个Java程序,以便清楚地了解这个主题。

以上就是Java程序示例:一个没有参数和返回类型的方法的详细内容,更多请关注php中文网其它相关文章!

相关标签:
java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号