Java – Complete List of immutable JDK classes

immutabilityjava

is there a list of de-facto immutable classes in the jdk?

technically Immutable classes include the obvious Integer, Double etc..

de-facto immutable will include for example java.lang.String – it might technically be mutable but de-facto it is not.

Also, are there Interfaces/Abstract classes which are required (as stated in the javadoc) to be immutable?

if you cannot provide a complete List, i would already be happy if you know a bunch of classes which state immutability in its javadoc..

Best Answer

I try to compile the list as much as I can:

  1. java.lang.String The wrapper classes for the primitive types:
  2. java.lang.Integer
  3. java.lang.Byte
  4. java.lang.Character
  5. java.lang.Short
  6. java.lang.Boolean
  7. java.lang.Long
  8. java.lang.Double
  9. java.lang.Float
  10. java.lang.StackTraceElement (used in building exception stacktraces)
  11. Most of the enum classes
  12. java.math.BigInteger
  13. java.math.BigDecimal
  14. java.io.File
  15. java.awt.Font
  16. java.awt.BasicStroke
  17. java.awt.Color
  18. java.awt.GradientPaint,
  19. java.awt.LinearGradientPaint
  20. java.awt.RadialGradientPaint,
  21. java.awt.Cursor
  22. java.util.Locale
  23. java.util.UUID
  24. java.util.Collections
  25. java.net.URL
  26. java.net.URI
  27. java.net.Inet4Address
  28. java.net.Inet6Address
  29. java.net.InetSocketAddress
  30. most subclasses of java.security.Permission