知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

      • Theory:Scanning the input
      • Theory:Arithmetic operations
      • Theory:Integer types and operations
      • Theory:Increment and decrement
      • Theory:Relational operators
      • Theory:Ternary operator
      • Theory:The while and do-while loops
      • Theory:Branching statements
      • Theory:Characters
      • Theory:String
      • Theory:Boolean and logical operators
      • Theory:Sizes and ranges
      • Theory:Switch statement
      • Theory:Declaring a method
        • The syntax of the method
        • Modifiers
        • Method parameters
        • Body and return statement
        • What happens when we invoke a method
        • Conclusion
      • Theory:The main method
      • Theory:Type casting
      • Theory:Primitive and reference types
      • Theory:Array
      • Theory:Arrays as parameters
      • Theory:Iterating over arrays
      • Theory:Multidimensional array
      • Theory:Final variables
    • Java OOP

    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Java basic
Jim
2022-08-09
目录

Theory:Declaring a method

  • Built-in methods are a real deal when you manage routine tasks. Still, they are not a cure-all solution since it's impossible to provide a standard method for each specific task. For example, you may need to convert dollars to euros, count your monthly spendings, or even calculate a daily portion of seeds for your parrot multiple times. That's when you create your own method to avoid repeating tons of code!

    In contrast to built-in methods, user-defined methods are created by the programmer. It is a common practice to create a customized subprogram for a specific purpose.

    But how to create it? Let's figure it out.

    # The syntax of the method

    Technically, a method is just a structured part of code with a few components. In Java, a method is always located inside a class. Let's take a closer look at a method that calculates a daily portion of seeds for a parrot:

    img

    A method contains a set of modifiers, a type of the return value, a name, a list of parameters in parentheses () , and a body in curly brackets {}. The combination of the name of the method and the list of its parameters is known as a method signature. In our example, the signature is countSeeds(int, int).

    Some methods also have a list of exceptions — they define the method's behavior in case of a mistake in the program. If during an execution of a method one of its exceptions is thrown, the exception is handled outside the method. For now, we'll consider simple methods without exceptions.

    Let's focus on the main components that we need to write simple methods from scratch.

    # Modifiers

    The first words are so-called modifiers. There are two types of modifiers in Java: access modifiers and non-access modifiers.

    Access modifiers define the visibility of the method. For now, we're using a public access modifier, which means there are no restrictions for invoking the method even from other classes.

    Non-access modifiers provide information about the behavior of methods to JVM. The modifier static means that the method belongs to the class and it can be accessed without creating any object. This type of method is called a static method.

    If the method is declared without the static modifier, it means that the method can be invoked only through or with an object or instance of this class. Such methods are called instance methods.

    Remember that there is a recommended order for the modifiers that you can find in the Java Language Specification (opens new window). In our case, it is public static.

    # Method parameters

    In parentheses after the method name, we define the type, number, and order of the parameters. This reflects how they will be passed to the method when it is invoked. Take a look at these signatures:

    convertEurosToDollars(double dlrRate, long eur);
    
    countMonthlySpendings(long food, long rent, long fun); 
    
    replace(char a, char b);
    
    1
    2
    3
    4
    5

    As you know, there are also methods that don't have values passed to them. These methods are known as non-parameterized.

    # Body and return statement

    Before a method completes its execution and exits, it returns a value known as a return value. The result of your method execution can be a value of primitive types like int, float, double, boolean, or reference types like String. Take a look at our countSeeds method with the return type int:

    public static int countSeeds(int parrotWeight, int parrotAge) {   
        return parrotWeight / 5 + parrotAge; // it returns an int
    }
    
    1
    2
    3

    What you see inside the curly brackets is known as the body of the method. The body holds the logic we want to implement by our method: a set of statements to perform with the passed values to obtain the result. Our method countSeeds takes two provided values, performs certain calculations, and returns the result within a return statement.

    Methods do not necessarily have to return a value. If we want a method not to return a value but to perform operations only, the keyword void is used as a return type:

    public static void countSeeds(int parrotWeight, int parrotAge) {
        System.out.println("give your parrot " + (parrotWeight / 5 + parrotAge) + 
        "g of seeds per day");
    }
    // this method just prints the line, so it returns no value
    
    1
    2
    3
    4
    5

    This method prints the line with recommendations on feeding the parrot and does not allow us to keep a value. Note that there is no return statement.

    Though methods with a void return type return nothing, you still may use a return word to exit the method. Usually, this applies to methods with conditions. Take a look:

    public static void isPositive(int num) {
        if (num > 0) {
            System.out.println("the number is positive");
        } else {
            return;
        }
    }
    
    1
    2
    3
    4
    5
    6
    7

    Remember, that if you try to return a value from a method with a void return type, a compile error will be thrown.

    # What happens when we invoke a method

    When invoking a method, we can write the returned value to some variable, print it out, or pass it on to another method. This is how it looks in a program:

    int myParrotWeight = 100;
    int myParrotAge = 3;
    
    int myParrotPortion = countSeeds(myParrotWeight, myParrotAge); 
    // now myParrotPortion equals 23
    
    1
    2
    3
    4
    5

    There's another important thing to remember. When you pass a variable of a primitive type to a method, a copy of this variable is created with the same value. Inside a method, only this copy is processed. Any changes to this copy will not affect the variable that was passed.

    Take a look:

    public static void main(String[] args) {
        int portion = 100;   
        addSeeds(portion); // try to change portion 
    
        // now let's print a portion
        System.out.println(portion);
        // 100, because the method didn't change portion, only its copy
    }
    
    /**
     * The method increases the portion of seeds by 50 
     * and prints the resulting value
     */ 
    public static void addSeeds(int portion) {
        portion += 50;
        System.out.println("The increased portion is " + portion); 
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17

    The output will look like:

    The increased portion is 150
    100
    
    1
    2

    Although the addSeeds method changes the passed argument, it happens to a different variable with its own value, leaving the value of the portion we've created intact.

    # Conclusion

    As you see, a method is a block of code that contains a set of modifiers, a return type, a name, a list of its parameters, and a body. A method may return a value or return nothing, which is indicated with the void keyword.

    If you know the syntax of methods, you can create your own and adjust them for your needs. This practice will make your code more structured and reusable.

编辑 (opens new window)
#Java basic
上次更新: 2022/09/26, 16:55:15
Theory:Switch statement
Theory:The main method

← Theory:Switch statement Theory:The main method→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式