Side effects of the introduction to Programming (Java) 3.1.2 Method

Source: Internet
Author: User

4. Side effects

In some languages such as Pascal, subroutines are divided into two types: functions and processes. [1] Although Java does not require that methods be differentiated into commands and functions, this distinction is of great help in designing programs well.

First, a concept is described: side effects (side effect). Side effects are generally for operations (expressions), an action/expression with a "side effect" means that when the expression is evaluated, one or more of the data of the program is changed, so that when the expression is evaluated again, different results may be obtained. In fact, Java's 4 expression statements, such as assignment, self-increment, method invocation, and object creation, can have side effects.

the side effects of the method are discussed here. The execution of a method, which causes some external "state" to change in addition to returning a value, is said to have a side effect. Here the so-called "state" changes, either the instance domain or the static variable is modified, the method's arguments are modified (this does not happen in Java.) However, when the argument is a reference, the object it points to may be modified to produce side effects), the data is passed to the monitor, the printer, or the file is saved, and so on.

Of course, the expression inside the method also has side effects, and if it only affects local variables without affecting the external state, then the method has no side effects. Define two terms based on the concept of side effects:

2 methods with return values and no side effects are called functions.

2 A method without a return value must have side effects unless its method body is empty or the method has no meaning. Therefore, methods that do not return a value, have a return value, but have side effects are called Procedures (procedure) or commands. In short, a method with side effects is called a process.

The concept of a function is defined so strictly because the function makes the state of the system stable and the behavior of the function easily predictable. Further, if the function is purely a function (pure function, purely functional )-its output value relies on and relies only on its input, always returns the same value for the same input (due to pure function pure and no side effects) A call to a pure function can be replaced by a value (or, in other words, a method as a value), which is known as the referential Transparency (referentialtransparency) feature in functional programming languages.

Routine 3?3 Pure function Package Semantics.method;public class sideeffectsdemo{    private static int x = 0;        Pure function public    static int times (int i,int j) {        return  i * j;    }    Non-pure function public    static int m (int i,int j) {        return  I * j +x;}    }


In functional programming languages (Functionalprogramming language) such as haskell[2], it is particularly important to avoid side effects. Of course, programming languages that do not produce any side effects are of no use, such as the need for side effects such as displaying data and depositing files, and emphasizing the Haskell language without side effects, using a technique to separate them and execute them in a safe way.

Java is an imperative object-oriented language, but it can draw on the advantages of functional language, and also look forward to the addition of important functional language features in Java . With the release of Java 8, the introduction of the lambda expression (lambda Expressions) shows that Java is beginning to introduce the features of functional languages. (They enable you to treat functionality as a method argument, or code as data.)


Exercise 3-5: What is a method, function, or pure function? Explain the implications of side effects.

Exercise 3-6: Implement the method, ask F (x) =x^3 + 3x+1. Note: X^3 in the book means x*x*x.


[1] Reference richardmitchell,jim Mackim, Meng Yan translation, Design by contract principles and practices.

[2] Http://www.haskell.org/learning.html



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Side effects of the introduction to Programming (Java) 3.1.2 Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.