javajavascriptfunctional-programmingimmutability

Can anyone explain me what is state and mutable data?


In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data.

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

Can anyone explain me what is state and mutable data? Can anyone give me examples in either JAVA or JavaScript.


Solution

  • mutable suggest anything that can change, i.e. an int

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

    In java a string is immutable. you cannot change the string value only its reference.

    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 something which an instance of a class will have (an Object).

    If an Object has certain values for its attributes its in a diffrent state then another Object of the same class with diffrent attribute values