In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the methods of zero basic learning Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
01. What is the method in Java?
Method is used to achieve code reusability, we write the method once and use it many times. By adding or removing part of the code in the method, you can improve the readability of the overall code.
A method executes only when it is called. The most famous method in Java is the main () method. Click the link below to read more about the main () method.
02. How to declare the method?
The declaration of the method reflects some information about the method, such as visibility, return type, method name, and parameters. This is shown in the following figure.
Access: it specifies the visibility of the method. Java provides four access modifiers:
Public: this method can be accessed by all classes.
Private: this method can only be accessed in the class in which it is defined.
Protected: this method can be accessed by classes in the same package or by subclasses in different packages.
Default: if this method does not use any access modifiers, Java defaults to using the default modifier, which is visible only to classes in the same package.
Return type: the data type returned by the method, which can be a basic data type, object, and collection. If you do not need to return data, use the void keyword.
Method name: the method name best reflects the function of the method. For example, if we want to create a method that subtracts two numbers, then the method name had better be subtract.
The method name is preferably a verb and begins with a lowercase letter. If the method name contains more than two words, the first word is preferably a verb, followed by an adjective or noun, and named in a hump style. For example:
Method name of one word: sum ()
Multi-word method name: stringComparision ()
A method may have the same name as another method in the same class, which is called method overloading.
Parameters: parameters are placed in parentheses, and if there are multiple parameters, they can be separated by commas. The parameter consists of two parts, the parameter type and the parameter name. If the method has no parameters, the parentheses are empty.
Method signature: each method has a signature, including the method name and parameters.
Method body: the method body is placed in a pair of curly braces and some code is put together to perform specific tasks.
02. What are the methods?
Methods can be divided into two types, one is called predefined method, and the other is called user-defined method.
1) predefined method
Java provides a large number of predefined methods for us to call, also known as standard class library methods, or built-in methods. For example, the length (), equals (), compare () methods of the String class, and the println () method we use most often in the beginner Java phase, are used to print information on the console.
/ * * @ author Wechat searches for "Silent King II" and replies with the keywords PDF * / public class PredefinedMethodDemo {public static void main (String [] args) {System.out.println ("Silent King II, an interesting programmer");}}
In the above code, we use two predefined methods, the main () method is the entry to the program, and the println () method is a method of the PrintStream class. These methods are defined in advance, so we can use them directly.
We can view the method signature of the predefined method through the integrated development tool, and when we hover the mouse over the println () method, the following figure is displayed:
The access modifier for the println () method is public, the return type is void, the method name is println, the parameter is String x, and Javadoc (what does the method do).
Predefined methods make programming easier, and we just need to call them directly when implementing certain functions, without having to rewrite them.
2) user-defined method
When a predefined method does not meet our requirements, we need to customize some methods, for example, to define a method to check whether the number is even or odd.
Public static void findEvenOdd (int num) {if (num% 2 = = 0) {System.out.println (num + "is even");} else {System.out.println (num + "is odd");}}
The method is called findEvenOdd, the access modifier is public, and it is static (static), the return type is void, and the parameter has an int num. There is an if else statement in the body of the method. If num is divisible by 2, print that the number is even, otherwise it is odd.
After the method is defined, how can it be called?
/ * * @ author Wechat search for "Silent King II" and reply to the keywords PDF * / public class EvenOddDemo {public static void main (String [] args) {findEvenOdd (10); findEvenOdd (11);} public static void findEvenOdd (int num) {if (num% 2 = = 0) {System.out.println (num + "is even") } else {System.out.println (num + "is odd");}
The main () method is the entry to the program and is static, so you can directly call findEvenOdd (), which is also a static method.
A method is a static method when it is modified by the static keyword. In other words, static methods belong to the class, not to the class instance (you don't need to create an object through the new keyword, you can call it directly through the class name).
03. What is the instance method?
Methods declared in the class are called instance methods without static keyword modification, and an object of the class must be created before the instance method can be called.
/ * * @ author Wechat search for "Silent King II" and reply to the keywords PDF * / public class InstanceMethodExample {public static void main (String [] args) {InstanceMethodExample instanceMethodExample = new InstanceMethodExample (); System.out.println (instanceMethodExample.add (1,2));} public int add (int a, int b) {return a + b;}}
The add () method is an instance method that needs to be accessed by creating an InstanceMethodExample object.
There are two special types of instance methods:
Getter method
Setter method
The getter method is used to get the value of the private variable (the private-decorated field), and the setter method is used to set the value of the private variable.
/ * * @ author Silent Wang er, an interesting programmer * / public class Person {private String name; private int age; private int sex; public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age } public void setAge (int age) {this.age = age;} public int getSex () {return sex;} public void setSex (int sex) {this.sex = sex;}}
The getter method starts with get, and the setter method starts with set.
What is an abstract method?
A method without a method body is called an abstract method, and it is always declared in an abstract class. This means that if a class has abstract methods, the class must be abstract. You can create abstract methods and abstract classes using the atstract keyword.
/ * * @ author Wechat search for "Silent King II" and reply to the keyword PDF * / abstract class AbstractDemo {abstract void display ();}
When a class inherits an abstract class, it must override the abstract method:
/ * * @ author Wechat searches for "Silent King II" and replies with the keywords PDF * / public class MyAbstractDemo extends AbstractDemo {@ Override void display () {System.out.println ("abstract method overridden");} public static void main (String [] args) {MyAbstractDemo myAbstractDemo = new MyAbstractDemo (); myAbstractDemo.display ();}}
The output is as follows:
This is the end of the rewrite of the abstract method "what are the methods of Zero basic Java". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.