Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

What do we mean by immutable?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When we say arrays and Strings are immutable, what does it actually mean?

Thank You
 
author & internet detective
Posts: 40720
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Immutable means the values in the object can't be changed after it is instantiated.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raul saini wrote:When we say arrays and Strings are immutable, what does it actually mean?

Thank You



It means that they can't change their state after creation. Remember that Arrays and Strings are objects. A typical example would be 'String' (which is immutable) and 'StringBuffer or StringBuilder' (which are mutable).

Practical eg:

String s1 = "Joe";
String s2 = "Blog";

s1 += s2;

System.out.println(s1); // The s1 reference variable still retains its value as "Joe". In other to effectively add the s2 to s1, another String object needs to be created
//in other to effectively add the two objects together.

String s3 = s1 + s2; //which now contains the value: "Joe Blogs".

This is different to the other more specialised String classes such as StringBuilder and StringBuffer - which are mutable. (Use StringBuffer for Thread-safe operations).

Example:

StringBuilder sb = nee StringBuilder("Joe");

sb.append("Blogs"); // At this stage, the value of sb is "Joe Blogs".


 
Marshal
Posts: 73752
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Tom Hass
 
Tom Hass
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell. Obviously, my only post, can't be compared to your over 15,000 posts so far.
 
Campbell Ritchie
Marshal
Posts: 73752
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

Of course about half my posts are "You're welcome" and "Welcome to the Ranch."
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raul saini wrote:When we say arrays and Strings are immutable, what does it actually mean?

Thank You



Nobody seems to have mentioned yet that arrays are not immutable. Their size can't be changed but their contents certainly can.
 
Oh the stink of it! Smell my tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic