Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

When does Java programming need parameters in a method

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "when Java programming needs parameters in the method", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "when Java programming needs parameters in the method" this article.

When does Java need parameters in the method

There may be many people in the process of learning Java, when building methods always do not know when to write parameters and when not to write, then let's write the simplest example to illustrate the importance of parameters.

As in the example in the figure, if we want to calculate the values of b and c, we just need to replace b and c with the number you want to calculate when calling the add () method. Other methods can be analogous.

There is no parameter for a class in Java. The method with parameters focuses on summarizing the method of the class and defining some behavior (or function) of the class.

Step 1: define the method name and return value type

Step 2: write the method body

The method that defines the class:

Public returns the return value of the value type method name () {/ / method body} method (in both cases)

1. If the method has a return value, the keyword return must be used in the method to return the value, and the return value type is the type of the return value

Public class Student {String name = "Zhang San"; public String getName () {return name;} / /. }

Syntax: return expression

Function: jump out of the method and return the result

two。 If the method does not return a value, the return value type is void

Public class Student {String name = "Zhang San"; public void getName () {} / /. }

Method calls: execute statements contained in a method

Object name. Method name ()

Methods are allowed to call each other, do not need to know the specific implementation of the method, achieve reuse and improve efficiency

A case example is the method a () of the Student class

Call the method b () of the Student class

Call public void a () {directly

B (); / / call b ()

} method a () of the Student class

Call the method b () of the Teacher class

First create a class object, and then use "." Call public void a () {

Teacher t = new Teacher ()

T.B (); / / call b () of the Teacher class

} member variables and local variables

The location of the variable declaration determines the variable scope

The variable scope determines the area in the program that can access the variable by its name.

The difference between member variables and local variables

1. The scope is different

The scope of a local variable is limited to the methods that define it

The scope of the member variable is visible within the entire class

two。 Initial values are different

Java will give the member variable an initial value

Java does not assign initial values to local variables

Note:

1. Local variables with the same name are not allowed in the same method; in different methods, there can be local variables with the same name

two。 In the same class, when the member variable and the local variable have the same name, the local variable has a higher priority.

Define the method return type with parameters () {/ / the body of the method}

Access modifier: this method allows access to the scope of permissions called; return type: type of value returned by the method; formal parameter list: list of formal parameters passed to the method

Public class StudentsBiz {String [] names = new String [30]; public void addName (String name) {(no return value) / / add student name (a formal parameter)} public void showNames () {/ / Show all student names}} call the method with parameters

Object name. Method name (parameter 1, parameter 2, …... , parameter n) (argument list)

Public static void main (String [] args) {StudentsBiz st = new StudentsBiz (); (instantiate the object first, and then use the method) Scanner input = new Scanner (System.in); for (int ionomy.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report