Can someone explain to me what state and mutable data are?

In computer science, functional programming is a programming paradigm that considers computing as an estimate of mathematical functions and avoids state and mutable data.

http://en.wikipedia.org/wiki/Functional_programming

Can someone explain to me what state and mutable data are? Can anyone give me examples in JAVA or JavaScript.

+5
source share
5 answers

mutable offers everything that can change, i.e. int

int a = 0;
System.out.prtinln(a); //prints 0
a = 2;
System.out.prtinln(a); //now prints 2, so its mutable

In java, the string is immutable. you cannot change the value of a string only with your link.

String s1 = "Hello";
System.out.println(s1); //prints Hello
String s2 = s1;
s1 = "Hi";
System.out.println(s2); //prints "Hello" and not "Hi"

State is what the class instance (object) will have.

, ,

+6

, , . , BankAccount, .

, + . , , , . , , . .


+ mutable "".
+6

Mutable state - , , , .

Java:

public static double badSqrt(double x) {
    double r = Math.sqrt(x);
    if (System.currentTimeMillis() % 42L == 0L) return r;
    return r + 0.000000001;
}

. , badSqrt , ( ).

, , badSqrt() , . , . , .

, . , () , , ( ).

+3

Java String.

 String s = "ABC";

 s.toLowerCase();

 System.out.println(s);

= ABC

, s . s, :

s = s.toLowerCase();

new. St String, "abc".

+2

, int = 5;

, 5.

, = 7;

now the state of the variable i is such that now it contains the value 7, it replaces 5.

if a change in value is possible, it is called as-mutable, which means here we can change the state.

if changing the value is not possible, then it is called immutable.

+1
source

All Articles