Results 1 to 3 of 3
  1. #1
    girlycoder is offline Member
    Join Date
    May 2016
    Rep Power

    Default Java immutable issue

    Hi there,
    People are saying immutable in java is great.
    But most of the tutorials I found doesn't answer all of the questions-

    -How I can use it?
    -Where should and where it should not be used?
    -Disadvantages of immutable?

    Please let me know if you guys have those answers


  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: Java immutable issue

    Make a field immutable when it should only be set one time and not changed.
    For example: Integer ONE = new Integer(1);
    If the value of ONE was changeable, then a use of ONE expecting a value of 1 would be confusing if the value was 99.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Java immutable issue

    I tend to favor immutability as much as possible. The less an object can change, the less like problems
    will arise due to change in state. If you are going to use an object as a key to a hash map then it should
    be immutable. Otherwise, if you get careless and alter an existing key's state, then you can't retrieve the
    associated value. And hashCodes can be cached in immutable objects since they need only be calculated
    once. For more on this topic, here is a link to the Java tutorials.

    Immutable objects

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. is String immutable in Java
    By james12 in forum New To Java
    Replies: 2
    Last Post: 07-02-2014, 08:48 AM
  2. Why String is immutable in java?
    By harikrishanyogi in forum New To Java
    Replies: 2
    Last Post: 03-07-2012, 07:20 PM
  3. Why string is immutable in java?
    By jessie143143 in forum Reviews / Advertising
    Replies: 0
    Last Post: 10-15-2011, 02:47 PM
  4. String Immutable
    By jomypgeorge in forum New To Java
    Replies: 18
    Last Post: 12-17-2010, 11:54 AM
  5. What is an Immutable Class
    By maheshkanda in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 08:12 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts