Immutable objects are simply objects whose state (object data) cannot change after construction. Examples of immutable objects from the JDK include String and Integer.
For example: (dot is mutable and immutable string)
Point myPoint = new Point( 0, 0 ); System.out.println( myPoint ); myPoint.setLocation( 1.0, 0.0 ); System.out.println( myPoint ); String myString = new String( "old String" ); System.out.println( myString ); myString.replaceAll( "old", "new" ); System.out.println( myString );
Output:
java.awt.Point[0.0, 0.0] java.awt.Point[1.0, 0.0] old String old String
user536158 Jan 11 2018-11-11T00: 00Z
source share