Pure Method in Java

Rumman Ansari   Software Engineer   2024-07-25 07:53:18   106  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Pure Method

A pure method is a method that adheres to the following principles:

  1. No Side Effects: A pure method does not alter any external state or variables. It only operates on its input parameters and returns a result based solely on those inputs.

  2. Deterministic: For the same set of input values, a pure method always produces the same output. The output is predictable and consistent.

  3. No External Dependencies: It does not rely on or affect any external state, class variables, or other methods. The behavior of a pure method is entirely contained within itself.

Example:


public int add(int a, int b) {
    return a + b; // Only depends on input parameters
}

  • No Side Effects: The method does not modify any variables outside of its scope.
  • Deterministic: For the same values of a and b, the method will always return the same result.
  • No External Dependencies: It only depends on its parameters.

MCQ Available

There are 1 MCQs available for this topic.

1 MCQ